Amazing-Python-Scripts
38 строк · 1.4 Кб
1import pandas as pd2from sklearn.ensemble import RandomForestClassifier3from sklearn.model_selection import train_test_split4from sklearn.metrics import classification_report, accuracy_score, confusion_matrix5from imblearn.over_sampling import SMOTE6from sklearn.preprocessing import StandardScaler7
8# Load your historical data (CSV file or any other format) into a pandas DataFrame
9# Replace 'data.csv' with the actual file path containing your historical data
10data = pd.read_csv('data.csv')11
12# Data preprocessing
13X = data.drop('Class', axis=1)14y = data['Class']15
16# Standardize the features
17scaler = StandardScaler()18X = scaler.fit_transform(X)19
20# Split the data into training and testing sets
21X_train, X_test, y_train, y_test = train_test_split(22X, y, test_size=0.2, random_state=42)23
24# Handle class imbalance using SMOTE
25smote = SMOTE(sampling_strategy='auto', random_state=42)26X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train)27
28# Create and train the Random Forest model
29model = RandomForestClassifier(n_estimators=100, random_state=42)30model.fit(X_train_resampled, y_train_resampled)31
32# Make predictions on the test set
33y_pred = model.predict(X_test)34
35# Evaluate the model
36print("Accuracy:", accuracy_score(y_test, y_pred))37print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))38print("Classification Report:\n", classification_report(y_test, y_pred))39