-
Notifications
You must be signed in to change notification settings - Fork 0
/
scikit-wine.py
65 lines (49 loc) · 1.87 KB
/
scikit-wine.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
64
65
import sklearn
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, r2_score
#from sklearn.externals import joblib
#import sklearn.external.joblib as extjoblib
import joblib
dataset_url = 'http://mlr.cs.umass.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'
data = pd.read_csv(dataset_url)
#print(data.head())
data = pd.read_csv(dataset_url, sep=';')
#print(data.head())
print (data.shape)
#print(data.describe())
y = data.quality
X = data.drop('quality', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2,random_state=123,stratify=y)
X_train_scaled = preprocessing.scale(X_train)
#print(X_train_scaled)
scaler = preprocessing.StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
#print(X_train_scaled.mean(axis=0))
#print(X_train_scaled.std(axis=0))
X_test_scaled = scaler.transform(X_test)
#print(X_test_scaled.mean(axis=0))
#print(X_test_scaled.std(axis=0))
pipeline = make_pipeline(preprocessing.StandardScaler(),RandomForestRegressor(n_estimators=100))
#print(pipeline.get_params())
hyperparameters = { 'randomforestregressor__max_features' : ['auto', 'sqrt', 'log2'],
'randomforestregressor__max_depth': [None, 5, 3, 1]}
clf = GridSearchCV(pipeline, hyperparameters, cv=10)
# Fit and tune model
clf.fit(X_train, y_train)
#print(clf.best_params_)
#print(clf.refit)
y_pred = clf.predict(X_test)
print(r2_score(y_test, y_pred))
# 0.45044082571584243
print(mean_squared_error(y_test, y_pred))
# 0.35461593750000003
joblib.dump(clf, 'rf_regressor.pkl')
clf2 = joblib.load('rf_regressor.pkl')
# Predict data set using loaded model
clf2.predict(X_test)