<jupyter_start><jupyter_text>Importing Dependencies<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>dataset_url = 'http://mlr.cs.umass.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'
data = pd.read_csv(dataset_url)
data.head()<jupyter_output><empty_output><jupyter_text>looks like our csv file use semicolon as seprator but it's super simple to fix<jupyter_code>data = pd.read_csv(dataset_url, sep=';')
data.head()<jupyter_output><empty_output><jupyter_text>Let's take a look at data<jupyter_code>data.shape<jupyter_output><empty_output><jupyter_text>We have 1,599 samples and 12 features, including our target feature. We can easily print some summary statistics.<jupyter_code>data.describe()<jupyter_output><empty_output><jupyter_text>List of all features:<jupyter_code>list(data)<jupyter_output><empty_output><jupyter_text>Split Data<jupyter_code>y = data.quality #target
X = data.drop('quality', axis=1) #features
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123, stratify=y)
print("Trainning data shape {}".format(X_train.shape))
print("Test data shape {}".format(X_test.shape))<jupyter_output>Trainning data shape (1279, 11)
Test data shape (320, 11)<jupyter_text>Randomly 20% is for test & rest is for trainning  Standardization<jupyter_code>pipeline = make_pipeline(preprocessing.StandardScaler(), 
                         RandomForestRegressor(n_estimators=100))<jupyter_output><empty_output><jupyter_text>Setting HyperParameters<jupyter_code>hyperparameters = { 'randomforestregressor__max_features' : ['auto', 'sqrt', 'log2'],
                  'randomforestregressor__max_depth': [None, 5, 3, 1]}<jupyter_output><empty_output><jupyter_text>Train & Tune via Cross Validation<jupyter_code>clf = GridSearchCV(pipeline, hyperparameters, cv=10)
clf.fit(X_train, y_train)
clf.best_params_<jupyter_output><empty_output><jupyter_text>Refit to get more accuracy<jupyter_code>clf.refit<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>y_pred = clf.predict(X_test)
print("r2_score: {}".format(r2_score(y_test, y_pred)))
print("mean_squared: {}".format(mean_squared_error(y_test, y_pred)))<jupyter_output>r2_score: 0.46820655003329514
mean_squared: 0.3431521875<jupyter_text>Save Model<jupyter_code>joblib.dump(clf, 'rf_regressor.pkl')
clf2 = joblib.load('rf_regressor.pkl')
clf2.predict(X_test)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup