-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9891f7b
commit d179302
Showing
11 changed files
with
2,965 additions
and
0 deletions.
There are no files selected for viewing
704 changes: 704 additions & 0 deletions
704
Chapter7/ch7-credit-card-def-model-tuning-and-evaluation.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
556 changes: 556 additions & 0 deletions
556
Chapter7/ch7-diamond-prices-model-tuning-and-evaluation.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@author: Alvaro Fuentes | ||
Chapter 9. Hands-On Predictive Analytics with Python | ||
Building a basic static app | ||
""" | ||
## imports | ||
import dash | ||
import dash_core_components as dcc | ||
import dash_html_components as html | ||
import plotly.graph_objs as go | ||
import pandas as pd | ||
import os | ||
|
||
## Importing the dataset | ||
DATA_DIR = '../data' | ||
FILE_NAME = 'diamonds.csv' | ||
data_path = os.path.join(DATA_DIR, FILE_NAME) | ||
diamonds = pd.read_csv(data_path) | ||
|
||
## Creating the app | ||
app = dash.Dash(__name__) | ||
|
||
# Creating a Plotly figure | ||
trace = go.Histogram( | ||
x = diamonds['price'] | ||
) | ||
|
||
layout = go.Layout( | ||
title = 'Diamond Prices', | ||
xaxis = dict(title='Price'), | ||
yaxis = dict(title='Count') | ||
) | ||
|
||
figure = go.Figure( | ||
data = [trace], | ||
layout = layout | ||
) | ||
|
||
app.layout = html.Div([ | ||
html.H1('My first Dash App'), | ||
html.H2('Histogram of diamond prices'), | ||
html.P('This is some normal text, we can use it to describe something about the application.'), | ||
dcc.Graph(id='my-histogram', figure=figure) | ||
]) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run_server(debug=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@author: Alvaro Fuentes | ||
Chapter 9. Hands-On Predictive Analytics with Python | ||
Building a basic interactive app | ||
""" | ||
## imports | ||
import dash | ||
import dash_core_components as dcc | ||
import dash_html_components as html | ||
from dash.dependencies import Input, Output | ||
import plotly.graph_objs as go | ||
import pandas as pd | ||
import os | ||
|
||
## Importing the dataset | ||
DATA_DIR = '../data' | ||
FILE_NAME = 'diamonds.csv' | ||
data_path = os.path.join(DATA_DIR, FILE_NAME) | ||
diamonds = pd.read_csv(data_path) | ||
diamonds = diamonds.sample(n=2000) | ||
|
||
|
||
app = dash.Dash(__name__) | ||
|
||
app.css.append_css({ | ||
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css' | ||
}) | ||
|
||
numerical_features = ['price','carat','depth','table','x','y','z'] | ||
options_dropdown = [{'label':x.upper(), 'value':x} for x in numerical_features] | ||
|
||
dd_x_var = dcc.Dropdown( | ||
id='x-var', | ||
options = options_dropdown, | ||
value = 'carat' | ||
) | ||
|
||
div_x_var = html.Div( | ||
children=[html.H4('Variable for x axis: '), dd_x_var], | ||
className="six columns" | ||
) | ||
|
||
|
||
dd_y_var = dcc.Dropdown( | ||
id='y-var', | ||
options = options_dropdown, | ||
value = 'price' | ||
) | ||
|
||
div_y_var = html.Div( | ||
children=[html.H4('Variable for y axis: '), dd_y_var], | ||
className="six columns" | ||
) | ||
|
||
app.layout = html.Div(children=[ | ||
html.H1('Adding interactive controls'), | ||
html.H2('Interactive scatter plot example'), | ||
html.Div( | ||
children=[div_x_var, div_y_var], | ||
className="row" | ||
), | ||
dcc.Graph(id='scatter') | ||
]) | ||
|
||
|
||
@app.callback( | ||
Output(component_id='scatter', component_property='figure'), | ||
[Input(component_id='x-var', component_property='value'), Input(component_id='y-var', component_property='value')]) | ||
def scatter_plot(x_col, y_col): | ||
trace = go.Scatter( | ||
x = diamonds[x_col], | ||
y = diamonds[y_col], | ||
mode = 'markers' | ||
) | ||
|
||
layout = go.Layout( | ||
title = 'Scatter plot', | ||
xaxis = dict(title = x_col.upper()), | ||
yaxis = dict(title = y_col.upper()) | ||
) | ||
|
||
output_plot = go.Figure( | ||
data = [trace], | ||
layout = layout | ||
) | ||
|
||
return output_plot | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run_server(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@author: Alvaro Fuentes | ||
Chapter 9. Hands-On Predictive Analytics with Python | ||
Producing the predictive model's objects | ||
""" | ||
## Imports | ||
import numpy as np | ||
import pandas as pd | ||
import os | ||
from keras.models import Sequential | ||
from keras.layers import Dense | ||
from sklearn.externals import joblib | ||
|
||
## Loading the dataset | ||
DATA_DIR = '../data' | ||
FILE_NAME = 'diamonds.csv' | ||
data_path = os.path.join(DATA_DIR, FILE_NAME) | ||
diamonds = pd.read_csv(data_path) | ||
|
||
|
||
## Preparing the dataset | ||
diamonds = diamonds.loc[(diamonds['x']>0) | (diamonds['y']>0)] | ||
diamonds.loc[11182, 'x'] = diamonds['x'].median() | ||
diamonds.loc[11182, 'z'] = diamonds['z'].median() | ||
diamonds = diamonds.loc[~((diamonds['y'] > 30) | (diamonds['z'] > 30))] | ||
diamonds = pd.concat([diamonds, pd.get_dummies(diamonds['cut'], prefix='cut', drop_first=True)], axis=1) | ||
diamonds = pd.concat([diamonds, pd.get_dummies(diamonds['color'], prefix='color', drop_first=True)], axis=1) | ||
diamonds = pd.concat([diamonds, pd.get_dummies(diamonds['clarity'], prefix='clarity', drop_first=True)], axis=1) | ||
|
||
## Dimensionality reduction | ||
from sklearn.decomposition import PCA | ||
pca = PCA(n_components=1, random_state=123) | ||
diamonds['dim_index'] = pca.fit_transform(diamonds[['x','y','z']]) | ||
diamonds.drop(['x','y','z'], axis=1, inplace=True) | ||
|
||
## Creating X and y | ||
X = diamonds.drop(['cut','color','clarity','price'], axis=1) | ||
y = np.log(diamonds['price']) | ||
|
||
## Standarization: centering and scaling | ||
numerical_features = ['carat', 'depth', 'table', 'dim_index'] | ||
from sklearn.preprocessing import StandardScaler | ||
scaler = StandardScaler() | ||
X.loc[:, numerical_features] = scaler.fit_transform(X[numerical_features]) | ||
|
||
## Building the neural network | ||
n_input = X.shape[1] | ||
n_hidden1 = 32 | ||
n_hidden2 = 16 | ||
n_hidden3 = 8 | ||
|
||
nn_reg = Sequential() | ||
nn_reg.add(Dense(units=n_hidden1, activation='relu', input_shape=(n_input,))) | ||
nn_reg.add(Dense(units=n_hidden2, activation='relu')) | ||
nn_reg.add(Dense(units=n_hidden3, activation='relu')) | ||
# output layer | ||
nn_reg.add(Dense(units=1, activation=None)) | ||
|
||
## Training the neural network | ||
batch_size = 32 | ||
n_epochs = 40 | ||
nn_reg.compile(loss='mean_absolute_error', optimizer='adam') | ||
nn_reg.fit(X, y, epochs=n_epochs, batch_size=batch_size) | ||
|
||
## Serializing: | ||
# PCA | ||
joblib.dump(pca, './Model/pca.joblib') | ||
|
||
# Scaler | ||
joblib.dump(scaler, './Model/scaler.joblib') | ||
|
||
# Trained model | ||
nn_reg.save("./Model/diamond-prices-model.h5") |
Oops, something went wrong.