└── peremen v chislov /peremen v chislov: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from sklearn.model_selection import train_test_split 4 | from sklearn.ensemble import RandomForestClassifier 5 | from sklearn.metrics import classification_report, confusion_matrix 6 | 7 | # Загрузка данных 8 | df = pd.read_csv('data.csv') 9 | 10 | # Преобразование категориальных переменных в числовые 11 | for cat in df.columns: 12 | if df[cat].dtype == 'O': 13 | df[cat] = df[cat].astype('category').cat.codes 14 | 15 | # Разделение данных на обучающую и тестовую выборки 16 | X = df.drop(['target'], axis=1) 17 | y = df['target'] 18 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 19 | 20 | # Создание и обучение модели 21 | model = RandomForestClassifier(n_estimators=100, max_depth=8, random_state=42) 22 | model.fit(X_train, y_train) 23 | 24 | # Прогнозирование на тестовых данных 25 | y_pred = model.predict(X_test) 26 | 27 | # Оценка производительности модели 28 | print('Confusion matrix:') 29 | print(confusion_matrix(y_test, y_pred)) 30 | print('Classification report:') 31 | print(classification_report(y_test, y_pred)) 32 | --------------------------------------------------------------------------------