diff --git a/code/optional-py-scripts/ch03.py b/code/optional-py-scripts/ch03.py index 730708ae..3edd740c 100644 --- a/code/optional-py-scripts/ch03.py +++ b/code/optional-py-scripts/ch03.py @@ -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 @@ -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)) @@ -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) @@ -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) @@ -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] diff --git a/code/optional-py-scripts/ch05.py b/code/optional-py-scripts/ch05.py index 71bb99a7..215b0150 100644 --- a/code/optional-py-scripts/ch05.py +++ b/code/optional-py-scripts/ch05.py @@ -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) @@ -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) diff --git a/code/optional-py-scripts/ch08.py b/code/optional-py-scripts/ch08.py index 4f1a0c05..c50145c5 100644 --- a/code/optional-py-scripts/ch08.py +++ b/code/optional-py-scripts/ch08.py @@ -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("This :) is :( a test :-)!") @@ -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')) diff --git a/code/optional-py-scripts/ch10.py b/code/optional-py-scripts/ch10.py index f2306118..17e8eb98 100644 --- a/code/optional-py-scripts/ch10.py +++ b/code/optional-py-scripts/ch10.py @@ -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)') @@ -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: diff --git a/code/optional-py-scripts/ch13.py b/code/optional-py-scripts/ch13.py index a28eebc0..57941805 100644 --- a/code/optional-py-scripts/ch13.py +++ b/code/optional-py-scripts/ch13.py @@ -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), @@ -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]) @@ -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) @@ -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) @@ -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]))