Skip to content

Commit ffd36c8

Browse files
author
Aaron England
authored
Add files via upload
1 parent 642e680 commit ffd36c8

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

Chapter 2/Activities/Activity_03.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
# continuing from Exercise 9:
44

5-
# generate predicted probabilities of rain
6-
predicted_prob = model.predict_proba(X_test)[:,1]
7-
85
# generate predicted classes
9-
predicted_class = model.predict(X_test)
6+
predicted_class = model.predict(X_test_scaled)
107

118
# evaluate performance with confusion matrix
129
from sklearn.metrics import confusion_matrix

Chapter 2/Activities/Activity_04.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Activity 4: Prepare data for decision tree classifier pipeline
1+
# Activity 4: Prepare data for decision tree classifier
22

33
# clear environment prior to running this code
44

@@ -23,5 +23,9 @@
2323
from sklearn.model_selection import train_test_split
2424
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
2525

26-
26+
# scale X_train and X_test
27+
from sklearn.preprocessing import StandardScaler
28+
model = StandardScaler()
29+
X_train_scaled = model.fit_transform(X_train)
30+
X_test_scaled = model.fit_transform(X_test)
2731

Chapter 2/Activities/Activity_05.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# continuing from Exercise 11:
44

55
# generate predicted probabilities of rain
6-
predicted_prob = model.predict_proba(X_test)[:,1]
6+
predicted_prob = model.predict_proba(X_test_scaled)[:,1]
77

88
# generate predicted classes
9-
predicted_class = model.predict(X_test)
9+
predicted_class = model.predict(X_test_scaled)
1010

1111
# evaluate performance with confusion matrix
1212
from sklearn.metrics import confusion_matrix

Chapter 2/Activities/Activity_06.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,21 @@
22

33
# continuing from Exercise 12
44

5-
# Set up the steps for a pipeline
6-
from sklearn.preprocessing import StandardScaler
7-
from sklearn.ensemble import RandomForestRegressor
8-
steps = [('scaler', StandardScaler()), ('Forest', RandomForestRegressor(n_estimators=10))]
9-
10-
# Setup the pipeline
11-
from sklearn.pipeline import Pipeline
12-
pipeline = Pipeline(steps)
13-
145
# Specify the hyperparameter space
156
import numpy as np
16-
parameters = {'Forest__criterion': ['mse','mae'],
17-
'Forest__max_features': ['auto', 'sqrt', 'log2', None],
18-
'Forest__min_impurity_decrease': np.linspace(0.0, 1.0, 10),
19-
'Forest__bootstrap': [True, False],
20-
'Forest__warm_start': [True, False]}
7+
grid = {'criterion': ['mse','mae'],
8+
'max_features': ['auto', 'sqrt', 'log2', None],
9+
'min_impurity_decrease': np.linspace(0.0, 1.0, 10),
10+
'bootstrap': [True, False],
11+
'warm_start': [True, False]}
2112

2213
# Instantiate the GridSearchCV model
2314
from sklearn.model_selection import GridSearchCV
24-
model = GridSearchCV(pipeline, parameters, scoring='explained_variance', cv=5)
15+
from sklearn.ensemble import RandomForestRegressor
16+
model = GridSearchCV(RandomForestRegressor(), grid, scoring='explained_variance', cv=5)
2517

2618
# Fit to the training set
27-
model.fit(X_train, y_train)
19+
model.fit(X_train_scaled, y_train)
2820

2921
# Print the tuned parameters
3022
best_parameters = model.best_params_

0 commit comments

Comments
 (0)