Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion code/optional-py-scripts/ch03.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import export_graphviz
# from sklearn.tree import export_graphviz
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import warnings

# for sklearn 0.18's alternative syntax
from distutils.version import LooseVersion as Version
Expand Down Expand Up @@ -108,6 +109,7 @@ def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
marker='o',
s=55, label='test set')


X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))

Expand All @@ -130,6 +132,7 @@ def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))


z = np.arange(-7, 7, 0.1)
phi_z = sigmoid(z)

Expand Down Expand Up @@ -161,6 +164,7 @@ def cost_1(z):
def cost_0(z):
return - np.log(1 - sigmoid(z))


z = np.arange(-10, 10, 0.1)
phi_z = sigmoid(z)

Expand Down Expand Up @@ -331,6 +335,7 @@ def entropy(p):
def error(p):
return 1 - np.max([p, 1 - p])


x = np.arange(0.0, 1.0, 0.01)

ent = [entropy(p) if p != 0 else None for p in x]
Expand Down
2 changes: 2 additions & 0 deletions code/optional-py-scripts/ch05.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def plot_decision_regions(X, y, classifier, resolution=0.02):
alpha=0.8, c=cmap(idx),
marker=markers[idx], label=cl)


lr = LogisticRegression()
lr = lr.fit(X_train_pca, y_train)

Expand Down Expand Up @@ -596,6 +597,7 @@ def project_x(x_new, X, gamma, alphas, lambdas):
k = np.exp(-gamma * pair_dist)
return k.dot(alphas / lambdas)


# projection of the "new" datapoint
x_reproj = project_x(x_new, X, gamma=15, alphas=alphas, lambdas=lambdas)
print('Reprojection x_reproj:', x_reproj)
Expand Down
2 changes: 2 additions & 0 deletions code/optional-py-scripts/ch08.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def preprocessor(text):
' '.join(emoticons).replace('-', '')
return text


print('Preprocessor on Excerpt:\n\n', preprocessor(df.loc[0, 'review'][-50:]))

res = preprocessor("</a>This :) is :( a test :-)!")
Expand Down Expand Up @@ -246,6 +247,7 @@ def stream_docs(path):
text, label = line[:-3], int(line[-2])
yield text, label


next(stream_docs(path='./movie_data.csv'))


Expand Down
4 changes: 3 additions & 1 deletion code/optional-py-scripts/ch10.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def lin_regplot(X, y, model):
plt.plot(X, model.predict(X), color='red', linewidth=2)
return


lin_regplot(X_std, y_std, lr)
plt.xlabel('Average number of rooms [RM] (standardized)')
plt.ylabel('Price in $1000\'s [MEDV] (standardized)')
Expand Down Expand Up @@ -196,7 +197,8 @@ def lin_regplot(X, y, model):
ransac = RANSACRegressor(LinearRegression(),
max_trials=100,
min_samples=50,
residual_metric=lambda x: np.sum(np.abs(x), axis=1),
residual_metric=lambda x: np.sum(
np.abs(x), axis=1),
residual_threshold=5.0,
random_state=0)
else:
Expand Down
5 changes: 5 additions & 0 deletions code/optional-py-scripts/ch13.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def predict_linreg(X, w):
predict = theano.function(inputs=[Xt], givens={w: w}, outputs=net_input)
return predict(X)


plt.scatter(X_train, y_train, marker='s', s=50)
plt.plot(range(X_train.shape[0]),
predict_linreg(X_train, w),
Expand Down Expand Up @@ -229,6 +230,7 @@ def logistic_activation(X, w):
z = net_input(X, w)
return logistic(z)


print('P(y=1|x) = %.3f' % logistic_activation(X, w)[0])


Expand Down Expand Up @@ -274,6 +276,7 @@ def softmax_activation(X, w):
z = net_input(X, w)
return softmax(z)


y_probas = softmax(Z)
print('Probabilities:\n', y_probas)

Expand All @@ -294,6 +297,7 @@ def tanh(z):
e_m = np.exp(-z)
return (e_p - e_m) / (e_p + e_m)


z = np.arange(-5, 5, 0.005)
log_act = logistic(z)
tanh_act = tanh(z)
Expand Down Expand Up @@ -359,6 +363,7 @@ def load_mnist(path, kind='train'):

return images, labels


X_train, y_train = load_mnist('mnist', kind='train')
print('Training rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1]))

Expand Down