def test_svm_classification(): y_signed_train = (y_train * 2) - 1 y_signed_test = (y_test * 2) - 1 for kernel in [RBF(gamma=0.1), Linear()]: model = SVM(max_iter=250, kernel=kernel) model.fit(X_train, y_signed_train) predictions = model.predict(X_test) assert accuracy(y_signed_test, predictions) >= 0.8
def test_svm_classification(): y_signed_train = (y_train * 2) - 1 y_signed_test = (y_test * 2) - 1 for kernel in [RBF(gamma=0.1), Linear()]: model = SVM(max_iter=250, kernel=kernel) model.fit(X_train, y_signed_train) predictions = model.predict(X_test) assert accuracy(y_signed_test, predictions) >= 0.8
def addition_nlp(ReccurentLayer): X_train, X_test, y_train, y_test = addition_dataset(8, 5000) print(X_train.shape, X_test.shape) model = NeuralNet( layers=[ ReccurentLayer, TimeDistributedDense(1), Activation('sigmoid'), ], loss='mse', optimizer=Adam(), metric='mse', batch_size=64, max_epochs=15, ) model.fit(X_train, y_train) predictions = np.round(model.predict(X_test)) predictions = np.packbits(predictions.astype(np.uint8)) y_test = np.packbits(y_test.astype(np.int)) print(accuracy(y_test, predictions))
except ImportError: from sklearn.cross_validation import train_test_split from sklearn.datasets import make_classification from mla.linear_models import LogisticRegression from mla.metrics import accuracy from mla.pca import PCA # logging.basicConfig(level=logging.DEBUG) # Generate a random binary classification problem. X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2, class_sep=2.5, ) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111) for s in ['svd', 'eigen']: p = PCA(15, solver=s) # fit PCA with training data, not entire dataset p.fit(X_train) X_train_reduced = p.transform(X_train) X_test_reduced = p.transform(X_test) model = LogisticRegression(lr=0.001, max_iters=2500) model.fit(X_train_reduced, y_train) predictions = model.predict(X_test_reduced) print('Classification accuracy for %s PCA: %s' % (s, accuracy(y_test, predictions)))
def test_knn(): clf = KNN(k=5) clf.fit(X_train, y_train) predictions = clf.predict(X_test) assert accuracy(y_test, predictions) >= 0.95
def test_knn(): clf = KNNClassifier(k=5) clf.fit(X_train, y_train) predictions = clf.predict(X_test) assert accuracy(y_test, predictions) >= 0.95
from sklearn.cross_validation import train_test_split from sklearn.datasets import make_classification from mla.linear_models import LogisticRegression from mla.metrics import accuracy from mla.pca import PCA # logging.basicConfig(level=logging.DEBUG) # Generate a random binary classification problem. X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2, class_sep=2.5, ) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111) for s in ['svd', 'eigen']: p = PCA(15, solver=s) # fit PCA with training data, not entire dataset p.fit(X_train) X_train_reduced = p.transform(X_train) X_test_reduced = p.transform(X_test) model = LogisticRegression(lr=0.001, max_iters=2500) model.fit(X_train_reduced, y_train) predictions = model.predict(X_test_reduced) print('Classification accuracy for %s PCA: %s' % (s, accuracy(y_test, predictions)))
layers=[ Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)), Activation('relu'), Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)), Activation('relu'), MaxPooling(pool_shape=(2, 2), stride=(2, 2)), Dropout(0.5), Flatten(), Dense(128), Activation('relu'), Dropout(0.5), Dense(10), Activation('softmax'), ], loss='categorical_crossentropy', optimizer=Adadelta(), metric='accuracy', batch_size=128, max_epochs=3, ) model.fit(X_train, y_train) predictions = model.predict(X_test) print(accuracy(y_test, predictions))
except ImportError: from sklearn.cross_validation import train_test_split from sklearn.datasets import make_classification from mla.linear_models import LogisticRegression from mla.metrics import accuracy from mla.pca import PCA # logging.basicConfig(level=logging.DEBUG) # Generate a random binary classification problem. X, y = make_classification( n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2, class_sep=2.5 ) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111) for s in ["svd", "eigen"]: p = PCA(15, solver=s) # fit PCA with training data, not entire dataset p.fit(X_train) X_train_reduced = p.transform(X_train) X_test_reduced = p.transform(X_test) model = LogisticRegression(lr=0.001, max_iters=2500) model.fit(X_train_reduced, y_train) predictions = model.predict(X_test_reduced) print("Classification accuracy for %s PCA: %s" % (s, accuracy(y_test, predictions)))
y_test = one_hot(y_test.flatten()) print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) # Approx. 15-20 min. per epoch model = NeuralNet( layers=[ Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)), Activation('relu'), Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)), Activation('relu'), MaxPooling(pool_shape=(2, 2), stride=(2, 2)), Dropout(0.5), Flatten(), Dense(128), Activation('relu'), Dropout(0.5), Dense(10), Activation('softmax'), ], loss='categorical_crossentropy', optimizer=Adadelta(), metric='accuracy', batch_size=128, max_epochs=3, ) model.fit(X_train, y_train) predictions = model.predict(X_test) print(accuracy(y_test, predictions))