-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.py
49 lines (35 loc) · 1.5 KB
/
test1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pandas as pd
import chardet
import csv
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
file_path = 'RRRegular.csv'
# Detect the encoding of the CSV file
with open(file_path, 'rb') as file:
result = chardet.detect(file.read())
# Read CSV file with pandas
data = pd.read_csv(file_path, encoding=result['encoding'])
# Assuming 'target_column' is the column you want to predict
target_column = 'Pos'
X = data.drop(target_column, axis=1)
y = data[target_column]
numeric_columns = X.select_dtypes(include=['float64', 'int64']).columns
scaler = MinMaxScaler()
X[numeric_columns] = scaler.fit_transform(X[numeric_columns])
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Random Forest Classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# Train the model
clf.fit(X_train, y_train)
# Make predictions on the test set
y_pred = clf.predict(X_test)
# Print the results
print("Random Forest Model Evaluation:")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)