Amazing-Python-Scripts

Форк
0
38 строк · 1.4 Кб
1
import pandas as pd
2
from sklearn.ensemble import RandomForestClassifier
3
from sklearn.model_selection import train_test_split
4
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
5
from imblearn.over_sampling import SMOTE
6
from sklearn.preprocessing import StandardScaler
7

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
10
data = pd.read_csv('data.csv')
11

12
# Data preprocessing
13
X = data.drop('Class', axis=1)
14
y = data['Class']
15

16
# Standardize the features
17
scaler = StandardScaler()
18
X = scaler.fit_transform(X)
19

20
# Split the data into training and testing sets
21
X_train, X_test, y_train, y_test = train_test_split(
22
    X, y, test_size=0.2, random_state=42)
23

24
# Handle class imbalance using SMOTE
25
smote = SMOTE(sampling_strategy='auto', random_state=42)
26
X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train)
27

28
# Create and train the Random Forest model
29
model = RandomForestClassifier(n_estimators=100, random_state=42)
30
model.fit(X_train_resampled, y_train_resampled)
31

32
# Make predictions on the test set
33
y_pred = model.predict(X_test)
34

35
# Evaluate the model
36
print("Accuracy:", accuracy_score(y_test, y_pred))
37
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
38
print("Classification Report:\n", classification_report(y_test, y_pred))
39

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.