# %% Initialize and Train Adaline adaline = Adaline([0] * (adalineDimension), 0.1, lambda x: 1 if x >= 0 else -1) xTrain = inputData[trainIndexes] yTrain = outputData[trainIndexes] adaline.train(xTrain, yTrain, tol, maxIterations) # %% Test xTest = inputData[testIndexes] yTest = outputData[testIndexes] testResult = adaline.test(xTest, yTest) print(f"Mean Squared Error: {testResult}") # %% Plot adalineApproxYArr = adaline.evaluate(inputData) weights = adaline.getWeights() def hyperPlan(x): return -(weights[0] * x + weights[2]) / weights[1] xPlan = np.linspace(0.5, 5.5, 100) yPlan = np.vectorize(hyperPlan)(xPlan) fig = make_subplots(x_title="x", y_title="y") # Add traces fig.add_trace(
wi = [round(x, 4) for x in wi] wf = [round(x, 4) for x in wf] w = np.concatenate(([wi], [wf], [[ep]]), axis=1) Treino = pd.DataFrame(data=w, columns=[ 'Wi0', 'Wi1', 'Wi2', 'Wi3', 'Wi4', 'Wf0', 'Wf1', 'Wf2', 'Wf3', 'Wf4', 'N_Epocas' ], index=[f'T{i+1}']) Treinamentos = pd.concat([Treinamentos, Treino]) resultado_parcial = pd.DataFrame(columns=[f'T{i+1}']) respostas = [] for j in teste.values: respostas.append(adaline.evaluate(j)) resultado_parcial = pd.DataFrame(data=respostas, columns=[f'T{i+1}']) Resultado = pd.concat([Resultado, resultado_parcial], axis=1) print(Treinamentos) print(Resultado) import matplotlib.pyplot as plt plt.figure(figsize=(4, 8)) for i in range(5): plt.subplot(5, 1, i + 1) plt.plot(hist_EQM[i]) plt.title(f"T{i+1}") plt.subplots_adjust(hspace=0.7)
# %% Initialize and Train Adaline adaline = Adaline([0] * (xDimension + 1), 0.1) xTrain = adalineXSamples[trainIndexes] yTrain = ySamples[trainIndexes] adaline.train(xTrain, yTrain, tol, maxIterations) # %% Test xTest = adalineXSamples[testIndexes] yTest = ySamples[testIndexes] testResult = adaline.test(xTest, yTest) print(f"Mean Squared Error: {testResult}") # %% Plot adalineApproxYArr = adaline.evaluate(adalineXSamples) fig = make_subplots(x_title="t", y_title="y") # for xSample in xSamples.T: # fig.add_trace(go.Scatter(x=timeSamples, y=xSample, name="Entrada")) fig.add_trace( go.Scatter(x=timeSamples, y=ySamples, name="Saida", mode='lines+markers')) fig.add_trace( go.Scatter(x=timeSamples, y=adalineApproxYArr, name="Adaline", mode='lines+markers'))
import pandas as pd import matplotlib.pyplot as plt from activation_functions import SignFunction from adaline import Adaline # Database dataset-treinamento: dataset = pd.read_csv('database/dataset-treinamento_ADALINE.csv') X = dataset.iloc[:, 0:4].values d = dataset.iloc[:, 4:].values adaline = Adaline( X, d, 0.0025, 10**(-6), SignFunction ) # entrada, saída, taxa de ativação, precisão e função de ativação values_eqm = adaline.train() # Database dataset-teste: dataset = pd.read_csv('database/dataset-teste_ADALINE.csv') X_teste = dataset.iloc[:, 0:4].values for x in X_teste: y = adaline.evaluate(x) print(f'Input: {x},Output: {y}') # Plotando figura do problema plt.plot(values_eqm) plt.show()