<jupyter_start><jupyter_text>Pickling sklearn Model  Ingest<jupyter_code>import numpy
from numpy import arange
from matplotlib import pyplot
import seaborn as sns
import pandas as pd
from pandas import read_csv
from pandas import set_option
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.metrics import mean_squared_error
boston_housing = "https://raw.githubusercontent.com/noahgift/boston_housing_pickle/master/housing.csv"
names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
df = read_csv(boston_housing, delim_whitespace=True, names=names)
df.head()<jupyter_output><empty_output><jupyter_text>EDA * CHAS - Charles River dummy variable (1 if tract bounds river; 0 otherwise)* RM - average number of rooms per dwelling* TAX - full-value property-tax rate per $10,000* PTRATIO - pupil-teacher ratio by town* Bk is the proportion of blacks by town* LSTAT - `%` lower status of the population* MEDV - Median value of owner-occupied homes in $1000's<jupyter_code>prices = df['MEDV']
df = df.drop(['CRIM','ZN','INDUS','NOX','AGE','DIS','RAD'], axis = 1)
features = df.drop('MEDV', axis = 1)
df.head()<jupyter_output><empty_output><jupyter_text>Modeling  Split Data<jupyter_code># Split-out validation dataset
array = df.values
X = array[:,0:6]
Y = array[:,6]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=validation_size, random_state=seed)
for sample in list(X_validation)[0:2]:
    print(f"X_validation {sample}")<jupyter_output>X_validation [  1.      6.395 666.     20.2   391.34   13.27 ]
X_validation [  0.      5.895 224.     20.2   394.81   10.56 ]<jupyter_text>Tune scaled GBM<jupyter_code># Test options and evaluation metric using Root Mean Square error method
num_folds = 10
seed = 7
RMS = 'neg_mean_squared_error'
scaler = StandardScaler().fit(X_train)
rescaledX = scaler.transform(X_train)
param_grid = dict(n_estimators=numpy.array([50,100,150,200,250,300,350,400]))
model = GradientBoostingRegressor(random_state=seed)
kfold = KFold(n_splits=num_folds, random_state=seed)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring=RMS, cv=kfold)
grid_result = grid.fit(rescaledX, Y_train)

print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))<jupyter_output>Best: -11.830068 using {'n_estimators': 200}
-12.479635 (6.348297) with: {'n_estimators': 50}
-12.102737 (6.441597) with: {'n_estimators': 100}
-11.843649 (6.631569) with: {'n_estimators': 150}
-11.830068 (6.559724) with: {'n_estimators': 200}
-11.879805 (6.512414) with: {'n_estimators': 250}
-11.895362 (6.487726) with: {'n_estimators': 300}
-12.008611 (6.468623) with: {'n_estimators': 350}
-12.053759 (6.453899) with: {'n_estimators': 400}<jupyter_text>Fit Model<jupyter_code># prepare the model
scaler = StandardScaler().fit(X_train)
rescaledX = scaler.transform(X_train)
model = GradientBoostingRegressor(random_state=seed, n_estimators=400)
model.fit(rescaledX, Y_train)
# transform the validation dataset
rescaledValidationX = scaler.transform(X_validation)
predictions = model.predict(rescaledValidationX)
print("Mean Squared Error: \n")
print(mean_squared_error(Y_validation, predictions))<jupyter_output>Mean Squared Error: 

26.326748591395717<jupyter_text>Evaluate<jupyter_code>predictions=predictions.astype(int)
evaluate = pd.DataFrame({
        "Org House Price": Y_validation,
        "Pred House Price": predictions
    })
evaluate["difference"] = evaluate["Org House Price"]-evaluate["Pred House Price"]
evaluate.head()
evaluate.describe()<jupyter_output><empty_output><jupyter_text>Adhoc Predict<jupyter_code>actual_sample = df.head(1)
actual_sample
adhoc_predict = actual_sample[["CHAS", "RM", "TAX", "PTRATIO", "B", "LSTAT"]]
adhoc_predict.head()<jupyter_output><empty_output><jupyter_text>JSON workflowUseful for debugging Flask apps<jupyter_code>json_payload = adhoc_predict.to_json()
json_payload<jupyter_output><empty_output><jupyter_text>scale input<jupyter_code>scaler = StandardScaler().fit(adhoc_predict)
scaled_adhoc_predict = scaler.transform(adhoc_predict)
scaled_adhoc_predict
list(model.predict(scaled_adhoc_predict))<jupyter_output><empty_output><jupyter_text>Pickling sklearn model<jupyter_code>from sklearn.externals import joblib
joblib.dump(model, 'boston_housing_prediction.joblib')
!ls -l<jupyter_output>total 672
-rw-r--r-- 1 root root 681425 May  5 00:35 boston_housing_prediction.joblib
drwxr-xr-x 1 root root   4096 Apr 29 16:32 sample_data<jupyter_text>Unpickle and predict<jupyter_code>clf = joblib.load('boston_housing_prediction.joblib')<jupyter_output><empty_output><jupyter_text>Adhoc Predict From Pickle<jupyter_code>actual_sample2 = df.head(5)
actual_sample2
adhoc_predict2 = actual_sample[["CHAS", "RM", "TAX", "PTRATIO", "B", "LSTAT"]]
adhoc_predict2.head()<jupyter_output><empty_output><jupyter_text>scale input<jupyter_code>scaler = StandardScaler().fit(adhoc_predict2)
scaled_adhoc_predict2 = scaler.transform(adhoc_predict2)
scaled_adhoc_predict2
# Use pickle loaded model
list(clf.predict(scaled_adhoc_predict2))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11763740771123872, 'fraction_numerical': 0.04905660377358491, 'mean_word_length': 7.5738396624472575, 'pattern_counts': {'":': 2, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '130184', 'license': 'MIT', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2209, 'n_words': 459, 'path': 'src/chap14-MLOps/boston-housing-pickle/boston_housing_pickle.ipynb', 'repo_name': 'vovanduc/python_devops_book', 'size': '6095'}