def main(): data = datasets.load_digits() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) # Perceptron clf = Perceptron(n_iterations=4000, learning_rate=0.01, plot_errors=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Perceptron", accuracy=accuracy, legend=True)
def main(): # Load dataset data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] y[y == 1] = 0 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) clf = LogisticRegression(gradient_descent=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Logistic Regression", accuracy=accuracy)
def main(): data = datasets.load_digits() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) # Optimization method for finding weights that minimizes loss optimizer = RMSprop(learning_rate=0.01) # Perceptron clf = Perceptron(n_iterations=5000, activation_function=ExpLU, optimizer=optimizer, early_stopping=True, plot_errors=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Perceptron", accuracy=accuracy, legend_labels=np.unique(y))
def main(): data = datasets.load_digits() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) # MLP clf = MultilayerPerceptron(n_hidden=12, n_iterations=5000, learning_rate=0.01, early_stopping=True, plot_errors=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Multilayer Perceptron", accuracy=accuracy, legend_labels=np.unique(y))
def simplify(self, param, algo='zigzag'): output = [] temp = data_manipulation.normalize(self.data) if algo == 'zigzag': output = self.zigzag(temp, param) if algo == 'fractal': output = self.fractal(temp, param) return np.asarray(output)
def main(): # Load the dataset data = datasets.load_iris() X = normalize(data.data) y = data.target # Project the data onto the 2 primary components multi_class_lda = MultiClassLDA() multi_class_lda.plot_in_2d(X, y)
def main(): # Load the dataset data = datasets.load_iris() X = normalize(data.data) y = data.target # Project the data onto the 2 primary components multi_class_lda = MultiClassLDA() multi_class_lda.plot_in_2d(X, y)
def main(): # Load the dataset data=load_iris_dataset(dir_path + r"/../data/iris.csv") X=data['X'] y=data['target'] X = normalize(X) # Project the data onto the 2 primary components multi_class_lda = MultiClassLDA() multi_class_lda.plot_in_2d(X, y)
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, 0.3) clf = NaiveBayes() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print(accuracy_score(y_pred, y_test))
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, 0.3) knn = KNN(3) y_pred = knn.predict(X_test, X_train, y_train) accuracy = accuracy_score(y_pred, y_test) print("accuracy is ", accuracy) pca = PCA() pca.plot_in_2d(X_test, y_pred, title="knn", accuracy=accuracy)
def main(): iris = load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = KNN(k=3) y_pred = clf.predict(X_test, X_train, y_train) print "Accuracy score:", accuracy_score(y_test, y_pred) # Reduce dimensions to 2d using pca and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): iris = load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = KNN(k=3) y_pred = clf.predict(X_test, X_train, y_train) print "Accuracy score:", accuracy_score(y_test, y_pred) # Reduce dimensions to 2d using pca and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): iris = datasets.load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) clf = NaiveBayes() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print "Accuracy score:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): iris = datasets.load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) clf = NaiveBayes() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print "Accuracy score:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data=load_iris_dataset(dir_path + r"/../data/iris.csv") X=data['X'] y=data['target'] X = normalize(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = KNN(k=3) y_pred = clf.predict(X_test, X_train, y_train) print "Accuracy score:", accuracy_score(y_test, y_pred) # Reduce dimensions to 2d using pca and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data = datasets.load_digits() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) # MLP clf = MultilayerPerceptron(n_hidden=10) clf.fit(X_train, y_train, n_iterations=4000, learning_rate=0.01) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = KNN(k=3) y_pred = clf.predict(X_test, X_train, y_train) accuracy = accuracy_score(y_test, y_pred) print ("Accuracy:", accuracy) # Reduce dimensions to 2d using pca and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="K Nearest Neighbors", accuracy=accuracy, legend_labels=data.target_names)
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) # Perceptron clf = Perceptron() clf.fit(X_train, y_train, plot_errors=True) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] y[y == 1] = -1 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = SupportVectorMachine(kernel=polynomial_kernel, power=4, coef=1) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data = load_iris_dataset(dir_path + r"/../data/iris.csv") X = data['X'] y = data['target'] X = normalize(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) # Perceptron clf = Perceptron() clf.fit(X_train, y_train, plot_errors=True) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) clf = NaiveBayes() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print ("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Naive Bayes", accuracy=accuracy, legend_labels=data.target_names)
def main(): # Testing functionality with the iris data for now. data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] # Make the targets in range -1 and 1 y[y == 1] = -1 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) svm = SupportVectorMachine() svm.fit(X_train, y_train) yPred = svm.predict(X_test) print("Accuracy:", accuracy_score(y_test, yPred)) svm.plotIn2D(X_test, yPred, ['', '', ''])
def graph_data_sources(self, sources): r"""Plots the data sources from sources and prints a legend Parameters ---------- sources : list subset of data dictionaries from data """ sources = dm.listify(sources) for source in sources: source = dm.normalize(source) time = source['data']['times'] values = source['data']['values'] name = source['metadata']['name'] subname = source['metadata']['subname'] plt.plot(time, values, label = name + " | " + subname) plt.legend(loc=2, prop={'size': 10}) plt.show()
def main(): data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] y[y == 1] = -1 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = SupportVectorMachine(kernel=polynomial_kernel, power=4, coef=1) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print ("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Support Vector Machine", accuracy=accuracy)
def main(): data = datasets.load_iris() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = KNN(k=3) y_pred = clf.predict(X_test, X_train, y_train) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimensions to 2d using pca and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="K Nearest Neighbors", accuracy=accuracy, legend_labels=data.target_names)
def main(): data = load_iris_dataset(dir_path + r"/../data/iris.csv") X = data['X'] y = data['target'] X = normalize(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) # MLP clf = MultilayerPerceptron(n_hidden=10) clf.fit(X_train, y_train, n_iterations=4000, learning_rate=0.01, plot_errors=True) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def main(): # Load dataset data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] y[y == 1] = 0 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) clf = LogisticRegression(gradient_descent=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print ("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Logistic Regression", accuracy=accuracy)
def main(): data = datasets.load_digits() X = normalize(data.data) y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) # Perceptron clf = Perceptron(n_iterations=5000, learning_rate=0.01, early_stopping=True, plot_errors=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print ("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Perceptron", accuracy=accuracy, legend_labels=np.unique(y))
def main(): iris = datasets.load_iris() X = normalize(iris.data) y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) clf = NaiveBayes() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred, title="Naive Bayes", accuracy=accuracy, legend=True, labels=iris.target_names)
def main(): # Load dataset data = load_iris_dataset(dir_path + r"/../data/iris.csv") X = data['X'] y = data['target'] X = normalize(X[y != 0]) y = y[y != 0] y[y == 1] = 0 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) clf = LogisticRegression() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print "Accuracy:", accuracy_score(y_test, y_pred) # Reduce dimension to two using PCA and plot the results pca = PCA() pca.plot_in_2d(X_test, y_pred)
def fit(self, X, y): X = normalize(polynomial_features(X, degree=self.degree)) super(ElasticNet, self).fit(X, y)
def predict(self, X): X = normalize(polynomial_features(X, degree=self.degree)) return super(ElasticNet, self).predict(X)
sys.path.insert(0, dir_path + "/unsupervised_learning") from principal_component_analysis import PCA # ........... # LOAD DATA # ........... data = datasets.load_digits() digit1 = 1 digit2 = 8 idx = np.append(np.where(data.target == digit1)[0], np.where(data.target == digit2)[0]) y = data.target[idx] # Change labels to {0, 1} y[y == digit1] = 0 y[y == digit2] = 1 X = data.data[idx] X = normalize(X) # .......................... # DIMENSIONALITY REDUCTION # .......................... pca = PCA() X = pca.transform(X, n_components=5) # Reduce to 5 dimensions # .......................... # TRAIN / TEST SPLIT # .......................... X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) # Rescale label for Adaboost to {-1, 1} rescaled_y_train = 2*y_train - np.ones(np.shape(y_train)) rescaled_y_test = 2*y_test - np.ones(np.shape(y_test))
# ........... # LOAD DATA # ........... data = datasets.load_digits() digit1 = 1 digit2 = 8 idx = np.append( np.where(data.target == digit1)[0], np.where(data.target == digit2)[0]) y = data.target[idx] # Change labels to {0, 1} y[y == digit1] = 0 y[y == digit2] = 1 X = data.data[idx] X = normalize(X) # .......................... # DIMENSIONALITY REDUCTION # .......................... pca = PCA() X = pca.transform(X, n_components=5) # Reduce to 5 dimensions # .......................... # TRAIN / TEST SPLIT # .......................... X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) # Rescale label for Adaboost to {-1, 1} rescaled_y_train = 2 * y_train - np.ones(np.shape(y_train)) rescaled_y_test = 2 * y_test - np.ones(np.shape(y_test))
import sys, os from sklearn import datasets import matplotlib.pyplot as plt import numpy as np import pandas as pd # Import helper functions dir_path = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, dir_path + "/../utils") from data_operation import calculate_covariance_matrix, calculate_correlation_matrix from data_manipulation import normalize # Load dataset and only use the two first classes data = datasets.load_iris() X = normalize(data.data[data.target < 2]) y = data.target[data.target < 2] X1 = X[y == 0] X2 = X[y == 1] # Calculate the covariances of the two class distributions cov1 = calculate_covariance_matrix(X1) cov2 = calculate_covariance_matrix(X2) cov_tot = cov1 + cov2 # Get the means of the two class distributions mean1 = X1.mean(0) mean2 = X2.mean(0) mean_diff = np.atleast_1d(mean1 - mean2) # Calculate w as (x1_mean - x2_mean) / (cov1 + cov2) w = np.linalg.pinv(cov_tot).dot(mean_diff)
import sys, os from sklearn import datasets import matplotlib.pyplot as plt import numpy as np import pandas as pd # Import helper functions dir_path = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, dir_path + "/../utils") from data_operation import calculate_covariance_matrix, calculate_correlation_matrix from data_manipulation import normalize # Load dataset and only use the two first classes data = datasets.load_iris() X = normalize(data.data[data.target < 2]) y = data.target[data.target < 2] X1 = X[y == 0] X2 = X[y == 1] # Calculate the covariances of the two class distributions cov1 = calculate_covariance_matrix(X1) cov2 = calculate_covariance_matrix(X2) cov_tot = cov1 + cov2 # Get the means of the two class distributions mean1 = X1.mean(0) mean2 = X2.mean(0) mean_diff = np.atleast_1d(mean1 - mean2) # Calculate w as (x1_mean - x2_mean) / (cov1 + cov2) w = np.linalg.pinv(cov_tot).dot(mean_diff)
def predict(self, X): X = normalize(polynomial_features(X, degree=self.degree)) return super(LassoRegression, self).predict(X)
def fit(self, X, y): X = normalize(polynomial_features(X, degree=self.degree)) super(LassoRegression, self).fit(X, y)
def predict(self, X): X = normalize(polynomial_features(X, degree=self.degree)) super(PolynomialRidgeRegression, self).predict(X)