X = np.array([ [0.0, 1.5], [1.0, 1.0], [2.0, 2.0], [2.0, 0.0], [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], ]) y = np.array([1,1,1,1,-1,-1,-1]).reshape(7,1) w_init = np.array([4, 1, -2]) perceptron = Perceptron(X, y, plot_errors = True, plot_data_lines = True) w_last = perceptron.train(w_init, epochs = 10) print('Ayak weights = ', w_last) y_pred = perceptron.predict(X) perceptron.error(y_pred, y) # Question 2 data = sio.loadmat('SampleCredit.mat') X_train = data['sample'][0:500,] X_test = data['sample'][500:,] y_train = data['label'][0:500,0].reshape(500,1) y_test = data['label'][500:,0].reshape(X_test.shape[0],1) # Correct Normalized for i in range(X_train.shape[1]):
# I am only going to use two of the features, # Sepal width and Petal width # for the Setos and Versicolor iris = sklearn.datasets.load_iris() # prepocessing data so be used with the PLA model iris_data = pd.DataFrame(iris.data, columns=iris.feature_names) iris_data['Class'] = iris.target # I will be using the first 100 data points from iris_data iris_test = shuffle_data(iris_data, 100) # I will input iris_test (shuffled data) and columns 0,2,4,5 x_train, y_train, x_test, y_test = train_test_data_split(iris_test, [0, 1, 3, 5]) iris_model = Perceptron() w, sse = iris_model.train(x_train, y_train) y_pred = iris_model.test(x_test, y_test) print("The accuracy of the model is: ", accuracy_score(y_test, y_pred)) print("The final weights are: ", w) print("SSE Cost") print(sse) # Simple scatter plot that shows the linearly seperable data. plt.scatter(x_train[:,1], x_train[:,2], c = y_train,alpha=0.8) plt.title("Perceptron") plot_decision_regions(x_train[:, 1:], y_train.astype(np.integer), clf=iris_model) plt.title('Perceptron Model') plt.xlabel('Sepal Width [cm]') plt.ylabel('Petal Width [cm]')