| import pandas as pd |
| from sklearn.model_selection import train_test_split |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.metrics import accuracy_score |
|
|
| |
| train_data = pd.read_csv('data.csv') |
| test_data = pd.read_csv('test.csv') |
|
|
| |
| X = train_data.drop(columns=['label']) |
| y = train_data['label'] |
|
|
| |
| X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
| |
| model = RandomForestClassifier(n_estimators=100, random_state=42) |
| model.fit(X_train, y_train) |
|
|
| |
| y_pred = model.predict(X_val) |
| print(f'Accuracy: {accuracy_score(y_val, y_pred)}') |
|
|
| |
| X_test = test_data # Предполагаем, что test.csv имеет те же признаки, что и data.csv, за исключением меток |
| test_predictions = model.predict(X_test) |
|
|
| |
| submission = pd.DataFrame({'Id': test_data.index, 'Label': test_predictions}) |
| submission.to_csv('submission.csv', index=False) |