Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- import sklearn
- from math import floor
- # Load the csv file
- alldata = pd.read_csv("./alldata.txt")
- N = len(alldata)
- stop = floor(0.75*N)
- xtrain = alldata.loc[0:stop-1, ["X1", "X2"]]
- ytrain = alldata.loc[0:stop-1, "y"]
- xtest = alldata.loc[stop:N, ["X1", "X2"]]
- ytest = alldata.loc[stop:N, "y"]
- # Scatter the training data
- plt.scatter(xtrain[ytrain == 1].X1, xtrain[ytrain == 1].X2, c="blue", marker="o", label="1")
- plt.scatter(xtrain[ytrain == 2].X1, xtrain[ytrain == 2].X2, c="red", marker="+", label="2")
- plt.title("Initial points and their class")
- plt.legend()
- plt.show()
- # ANN Creation
- # 2 hidden layers, 20 hidden nodes/layer, max_iter = 10000
- from sklearn.neural_network import MLPRegressor
- clf = MLPRegressor(hidden_layer_sizes=(2, 2), tol=0.01)
- clf = clf.fit(xtrain, ytrain)
- # Training Error - Compare 'pred' with 'ytrain'
- pred = clf.predict(xtrain)
- trainingError = [(t - p) for (t, p) in zip(ytrain, pred)]
- MA_TrE = np.mean(np.abs(trainingError))
- print("Training Error = MA_TrE = " + str(MA_TrE))
- plt.hist(trainingError, range=(-1, 1), rwidth=0.5)
- plt.title("Training Error")
- plt.show()
- # Testing Error - Compare 'pred' with 'ytest'
- pred = clf.predict(xtest)
- testingError = [(t - p) for (t, p) in zip(ytest, pred)]
- MA_TeE = np.mean(np.abs(testingError))
- print("Testing Error = MA_TeE = " + str(MA_TeE))
- plt.hist(testingError, range=(-1, 1), rwidth=0.5)
- plt.title("Testing Error")
- plt.show()
- print(trainingError)
- print(testingError)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement