Skip to content

Commit 6387821

Browse files
committed
fixes and formatting for examples
1 parent a808621 commit 6387821

12 files changed

+106
-59
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include mla/datasets/data *

examples/__init__.py

Whitespace-only changes.

examples/gbm.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,50 @@
33
from sklearn.datasets import make_classification
44
from sklearn.datasets import make_regression
55
from sklearn.metrics import roc_auc_score
6-
from sklearn.model_selection import train_test_split
6+
try:
7+
from sklearn.model_selection import train_test_split
8+
except ImportError:
9+
from sklearn.cross_validation import train_test_split
710

8-
from mla.ensemble.gbm import *
11+
from mla.ensemble.gbm import GradientBoostingClassifier, GradientBoostingRegressor
912
from mla.metrics.metrics import mean_squared_error
1013

1114
logging.basicConfig(level=logging.DEBUG)
1215

1316

1417
def classification():
1518
# Generate a random binary classification problem.
16-
X, y = make_classification(n_samples=350, n_features=15, n_informative=10, random_state=1111, n_classes=2,
19+
X, y = make_classification(n_samples=350, n_features=15, n_informative=10,
20+
random_state=1111, n_classes=2,
1721
class_sep=1., n_redundant=0)
18-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)
22+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15,
23+
random_state=1111)
1924

20-
model = GradientBoostingClassifier(n_estimators=50,
21-
max_depth=4, max_features=8, learning_rate=0.1)
25+
model = GradientBoostingClassifier(n_estimators=50, max_depth=4,
26+
max_features=8, learning_rate=0.1)
2227
model.fit(X_train, y_train)
2328
predictions = model.predict(X_test)
2429
print(predictions)
2530
print(predictions.min())
2631
print(predictions.max())
27-
print('classification, roc auc score: %s' % roc_auc_score(y_test, predictions))
32+
print('classification, roc auc score: %s'
33+
% roc_auc_score(y_test, predictions))
2834

2935

3036
def regression():
3137
# Generate a random regression problem
32-
X, y = make_regression(n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111,
38+
X, y = make_regression(n_samples=500, n_features=5, n_informative=5,
39+
n_targets=1, noise=0.05, random_state=1111,
3340
bias=0.5)
34-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
41+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
42+
random_state=1111)
3543

36-
model = GradientBoostingRegressor(n_estimators=25, max_depth=5, max_features=3, )
44+
model = GradientBoostingRegressor(n_estimators=25, max_depth=5,
45+
max_features=3, )
3746
model.fit(X_train, y_train)
3847
predictions = model.predict(X_test)
39-
print('regression, mse: %s' % mean_squared_error(y_test.flatten(), predictions.flatten()))
48+
print('regression, mse: %s'
49+
% mean_squared_error(y_test.flatten(), predictions.flatten()))
4050

4151

4252
if __name__ == '__main__':

examples/kmeans.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
2-
31
import numpy as np
42
from sklearn.datasets import make_blobs
53

64
from mla.kmeans import KMeans
75

86

97
def kmeans_example(plot=False):
10-
X, y = make_blobs(centers=4, n_samples=500, n_features=2, shuffle=True, random_state=42)
8+
X, y = make_blobs(centers=4, n_samples=500, n_features=2,
9+
shuffle=True, random_state=42)
1110
clusters = len(np.unique(y))
1211
k = KMeans(K=clusters, max_iters=150, init='++')
1312
k.fit(X)

examples/linear_models.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import logging
22

3-
from sklearn.model_selection import train_test_split
3+
try:
4+
from sklearn.model_selection import train_test_split
5+
except ImportError:
6+
from sklearn.cross_validation import train_test_split
47
from sklearn.datasets import make_classification
58
from sklearn.datasets import make_regression
69

710
from mla.linear_models import LinearRegression, LogisticRegression
8-
from mla.metrics.metrics import *
11+
from mla.metrics.metrics import mean_squared_error, accuracy
912

1013
# Change to DEBUG to see convergence
1114
logging.basicConfig(level=logging.ERROR)
1215

1316

1417
def regression():
1518
# Generate a random regression problem
16-
X, y = make_regression(n_samples=10000, n_features=100, n_informative=75, n_targets=1,
17-
noise=0.05, random_state=1111, bias=0.5)
18-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111)
19+
X, y = make_regression(n_samples=10000, n_features=100,
20+
n_informative=75, n_targets=1, noise=0.05,
21+
random_state=1111, bias=0.5)
22+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
23+
random_state=1111)
1924

2025
model = LinearRegression(lr=0.01, max_iters=2000, penalty='l2', C=0.03)
2126
model.fit(X_train, y_train)
@@ -25,9 +30,11 @@ def regression():
2530

2631
def classification():
2732
# Generate a random binary classification problem.
28-
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
29-
class_sep=2.5, )
30-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
33+
X, y = make_classification(n_samples=1000, n_features=100,
34+
n_informative=75, random_state=1111,
35+
n_classes=2, class_sep=2.5, )
36+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
37+
random_state=1111)
3138

3239
model = LogisticRegression(lr=0.01, max_iters=500, penalty='l1', C=0.01)
3340
model.fit(X_train, y_train)

examples/nnet_mlp.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
import logging
22

3-
from sklearn.model_selection import train_test_split
3+
try:
4+
from sklearn.model_selection import train_test_split
5+
except ImportError:
6+
from sklearn.cross_validation import train_test_split
47
from sklearn.datasets import make_classification
58
from sklearn.datasets import make_regression
69
from sklearn.metrics import roc_auc_score
710

8-
from mla.metrics.metrics import root_mean_squared_log_error, mean_squared_error
11+
from mla.metrics.metrics import mean_squared_error
912
from mla.neuralnet import NeuralNet
10-
from mla.neuralnet.constraints import MaxNorm, UnitNorm
13+
from mla.neuralnet.constraints import MaxNorm
1114
from mla.neuralnet.layers import Activation, Dense, Dropout
12-
from mla.neuralnet.optimizers import SGD, RMSprop, Adagrad, Adadelta, Adam
15+
from mla.neuralnet.optimizers import Adadelta, Adam
1316
from mla.neuralnet.parameters import Parameters
14-
from mla.neuralnet.regularizers import *
17+
from mla.neuralnet.regularizers import L2
1518
from mla.utils import one_hot
1619

1720
logging.basicConfig(level=logging.DEBUG)
1821

1922

2023
def classification():
2124
# Generate a random binary classification problem.
22-
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
23-
class_sep=2.5, )
25+
X, y = make_classification(n_samples=1000, n_features=100,
26+
n_informative=75, random_state=1111,
27+
n_classes=2, class_sep=2.5, )
2428
y = one_hot(y)
25-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)
29+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15,
30+
random_state=1111)
2631

2732
model = NeuralNet(
2833
layers=[
@@ -48,9 +53,11 @@ def classification():
4853

4954
def regression():
5055
# Generate a random regression problem
51-
X, y = make_regression(n_samples=5000, n_features=25, n_informative=25, n_targets=1, random_state=100, noise=0.05)
56+
X, y = make_regression(n_samples=5000, n_features=25, n_informative=25,
57+
n_targets=1, random_state=100, noise=0.05)
5258
y *= 0.01
53-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
59+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
60+
random_state=1111)
5461

5562
model = NeuralNet(
5663
layers=[

examples/nnet_rnn_binary_add.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
from itertools import combinations, islice
33

44
import numpy as np
5-
from sklearn.model_selection import train_test_split
5+
try:
6+
from sklearn.model_selection import train_test_split
7+
except ImportError:
8+
from sklearn.cross_validation import train_test_split
69

710
from mla.metrics import accuracy
811
from mla.neuralnet import NeuralNet

examples/nnet_rnn_text_generation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from mla.datasets import load_nietzsche
1010
from mla.neuralnet import NeuralNet
1111
from mla.neuralnet.constraints import SmallNorm
12-
from mla.neuralnet.layers import Activation, Dense, Parameters
12+
from mla.neuralnet.layers import Activation, Dense
1313
from mla.neuralnet.layers.recurrent import LSTM, RNN
14-
from mla.neuralnet.optimizers import Adam, RMSprop
14+
from mla.neuralnet.optimizers import RMSprop
1515

1616
logging.basicConfig(level=logging.DEBUG)
1717

examples/pca.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from sklearn.model_selection import train_test_split
1+
try:
2+
from sklearn.model_selection import train_test_split
3+
except ImportError:
4+
from sklearn.cross_validation import train_test_split
25
from sklearn.datasets import make_classification
36

47
from mla.linear_models import LogisticRegression
@@ -8,17 +11,19 @@
811
# logging.basicConfig(level=logging.DEBUG)
912

1013
# Generate a random binary classification problem.
11-
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
12-
class_sep=2.5, )
14+
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75,
15+
random_state=1111, n_classes=2, class_sep=2.5, )
1316

1417
for s in ['svd', 'eigen']:
1518
p = PCA(15, solver=s)
1619
p.fit(X)
1720
X = p.transform(X)
1821
print(X.shape)
1922

20-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111)
23+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
24+
random_state=1111)
2125
model = LogisticRegression(lr=0.001, max_iters=2500)
2226
model.fit(X_train, y_train)
2327
predictions = model.predict(X_test)
24-
print('Classification accuracy for %s PCA: %s' % (s, accuracy(y_test, predictions)))
28+
print('Classification accuracy for %s PCA: %s'
29+
% (s, accuracy(y_test, predictions)))

examples/random_forest.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from sklearn.datasets import make_classification
44
from sklearn.datasets import make_regression
55
from sklearn.metrics import roc_auc_score
6-
from sklearn.model_selection import train_test_split
6+
try:
7+
from sklearn.model_selection import train_test_split
8+
except ImportError:
9+
from sklearn.cross_validation import train_test_split
710

811
from mla.ensemble.random_forest import RandomForestClassifier, RandomForestRegressor
912
from mla.metrics.metrics import mean_squared_error
@@ -13,28 +16,34 @@
1316

1417
def classification():
1518
# Generate a random binary classification problem.
16-
X, y = make_classification(n_samples=500, n_features=10, n_informative=10, random_state=1111, n_classes=2,
19+
X, y = make_classification(n_samples=500, n_features=10, n_informative=10,
20+
random_state=1111, n_classes=2,
1721
class_sep=2.5, n_redundant=0)
1822

19-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)
23+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15,
24+
random_state=1111)
2025

2126
model = RandomForestClassifier(n_estimators=10, max_depth=4)
2227
model.fit(X_train, y_train)
2328
predictions = model.predict(X_test)[:, 1]
2429
# print(predictions)
25-
print('classification, roc auc score: %s' % roc_auc_score(y_test, predictions))
30+
print('classification, roc auc score: %s'
31+
% roc_auc_score(y_test, predictions))
2632

2733

2834
def regression():
2935
# Generate a random regression problem
30-
X, y = make_regression(n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111,
36+
X, y = make_regression(n_samples=500, n_features=5, n_informative=5,
37+
n_targets=1, noise=0.05, random_state=1111,
3138
bias=0.5)
32-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
39+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
40+
random_state=1111)
3341

3442
model = RandomForestRegressor(n_estimators=50, max_depth=10, max_features=3, )
3543
model.fit(X_train, y_train)
3644
predictions = model.predict(X_test)
37-
print('regression, mse: %s' % mean_squared_error(y_test.flatten(), predictions.flatten()))
45+
print('regression, mse: %s'
46+
% mean_squared_error(y_test.flatten(), predictions.flatten()))
3847

3948

4049
if __name__ == '__main__':

examples/svm.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
import logging
22

3-
from sklearn.model_selection import train_test_split
3+
try:
4+
from sklearn.model_selection import train_test_split
5+
except ImportError:
6+
from sklearn.cross_validation import train_test_split
47
from sklearn.datasets import make_classification
58

6-
from mla.metrics.metrics import *
7-
from mla.svm.kernerls import *
9+
from mla.metrics.metrics import accuracy
10+
from mla.svm.kernerls import Linear, RBF
811
from mla.svm.svm import SVM
912

1013
logging.basicConfig(level=logging.DEBUG)
1114

1215

1316
def classification():
1417
# Generate a random binary classification problem.
15-
X, y = make_classification(n_samples=1200, n_features=10, n_informative=5, random_state=1111, n_classes=2,
16-
class_sep=1.75, )
18+
X, y = make_classification(n_samples=1200, n_features=10, n_informative=5,
19+
random_state=1111, n_classes=2, class_sep=1.75,)
1720
# Convert y to {-1, 1}
1821
y = (y * 2) - 1
19-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1111)
22+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
23+
random_state=1111)
2024

2125
for kernel in [RBF(gamma=0.1), Linear()]:
2226
model = SVM(max_iter=500, kernel=kernel, C=0.6)
2327
model.fit(X_train, y_train)
2428
predictions = model.predict(X_test)
25-
print('Classification accuracy (%s): %s' % (kernel, accuracy(y_test, predictions)))
29+
print('Classification accuracy (%s): %s'
30+
% (kernel, accuracy(y_test, predictions)))
2631

2732

2833
if __name__ == '__main__':

mla/neuralnet/layers/convnet.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ def forward_pass(self, X):
4242
n_images, n_channels, height, width = self.shape(X.shape)
4343
self.last_input = X
4444
self.col = image_to_column(X, self.filter_shape, self.stride, self.padding)
45-
self.col_W = self._params.W.reshape(self.n_filters, -1).T
45+
self.col_W = self._params['W'].reshape(self.n_filters, -1).T
4646

47-
out = np.dot(self.col, self.col_W) + self._params.b
47+
out = np.dot(self.col, self.col_W) + self._params['b']
4848
out = out.reshape(n_images, height, width, -1).transpose(0, 3, 1, 2)
4949
return out
5050

5151
def backward_pass(self, delta):
5252
delta = delta.transpose(0, 2, 3, 1).reshape(-1, self.n_filters)
5353

54-
self._params.d_b = np.sum(delta, axis=0)
55-
d_W = np.dot(self.col.T, delta)
56-
self._params.d_W = d_W.transpose(1, 0).reshape(self._params.W.shape)
54+
d_W = np.dot(self.col.T, delta).transpose(1, 0).reshape(self._params['W'].shape)
55+
d_b = np.sum(delta, axis=0)
56+
self._params.update_grad('b', d_b)
57+
self._params.update_grad('W', d_W)
5758

5859
d_c = np.dot(delta, self.col_W.T)
5960
return column_to_image(d_c, self.last_input.shape, self.filter_shape, self.stride, self.padding)

0 commit comments

Comments
 (0)