-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
63 lines (52 loc) · 2.01 KB
/
model.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score, accuracy_score, precision_score, recall_score
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
import joblib
# Assuming you have the code to load and preprocess the data as well as train the model
def load_data(filename):
data = pd.read_csv(filename)
return data
def clean_data(data):
if 'Unnamed: 6' in data.columns:
data = data.drop(columns='Unnamed: 6')
data = data.fillna(data.median())
data.columns = data.columns.str.lower().str.replace('.', '')
X = data.drop('class', axis=1)
y = data['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
return X_train, X_test, y_train, y_test, scaler
def build_rf_model():
model = RandomForestClassifier(n_estimators=100, random_state=42)
return model
def train_model(model, X_train, y_train):
model.fit(X_train, y_train)
return model
def evaluate_model(model, X_test, y_test):
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
print(f"F1 Score: {f1}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
# Load the dataset
data = load_data('aqua_attributes.csv')
# Clean and preprocess the dataset
X_train, X_test, y_train, y_test, scaler = clean_data(data)
# Build the Random Forest model
model = build_rf_model()
# Train the model
model = train_model(model, X_train, y_train)
# Evaluate the model
evaluate_model(model, X_test, y_test)
# Save the model and scaler using the current version of scikit-learn
joblib.dump(model, 'random_forest_model.joblib')
joblib.dump(scaler, 'scaler.joblib')