-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwine_quality_model.py
57 lines (45 loc) · 1.73 KB
/
wine_quality_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
# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
# Read the wine quality dataset
data = pd.read_csv('/content/winequality-red.csv')
# Handle missing values (if any)
data.dropna(inplace=True)
# Create feature matrix (X) and target vector (y)
X = data.drop('quality', axis=1)
y = data['quality']
# 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)
# Define the parameter grid for hyperparameter tuning
param_grid = {'n_estimators': [100, 200, 300],
'max_depth': [5, 10, 15],
'min_samples_leaf': [1, 2, 3]}
# Create a Random Forest Classifier object
rf_model = RandomForestClassifier()
# Perform grid search cross-validation to find optimal hyperparameters
grid_search = GridSearchCV(rf_model, param_grid, cv=5, n_jobs=-1)
grid_search.fit(X_train, y_train)
# Get the best hyperparameters from the grid search
best_params = grid_search.best_params_
print("Best Hyperparameters:", best_params)
# Train the Random Forest Classifier with the best hyperparameters
rf_model.set_params(**best_params)
rf_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = rf_model.predict(X_test)
# Calculate the accuracy score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Plot the prediction graph
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred)
plt.xlabel('Actual Quality')
plt.ylabel('Predicted Quality')
plt.title('Prediction Graph')
plt.show()