from mystuff import perceptron
from mystuff import plotting_stuff

plt.close('all')

df = pd.read_csv('./iris.data', header=None)
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0, 2]].values
fig1 = plt.figure()
initial_data_plot = fig1.add_subplot(111)
initial_data_plot.set_title('initial data')
initial_data_plot.set_xlabel('petal length')
initial_data_plot.set_ylabel('sepal length')
initial_data_plot.legend(loc='upper left')
initial_data_plot.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
initial_data_plot.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')

ppn = perceptron.Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)
fig2 = plt.figure()
errors_plot = fig2.add_subplot(111)
errors_plot.set_title('errors during fitting')
errors_plot.set_xlabel('Epochs')
errors_plot.set_ylabel('Number of misclassifications')
errors_plot.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o')

plotting_stuff.plot_decision_regions(X, y, classifier=ppn)

plt.show()
ax[0].set_xlabel("Epochs")
ax[0].set_ylabel("log(Sum-Squared-Error)")

ada2 = adaline.AdalineGD(n_iter=10, eta=0.0001).fit(X, y)
ax[1].plot(range(1, len(ada2.cost_) + 1), np.log10(ada2.cost_), marker="o")
ax[1].set_title("Adaline -- learning rate 0.0001")
ax[1].set_xlabel("Epochs")
ax[1].set_ylabel("log(Sum-Squared-Error)")

X_std = np.copy(X)
X_std[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X_std[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

ada = adaline.AdalineGD(n_iter=15, eta=0.01)
ada.fit(X_std, y)
plotting_stuff.plot_decision_regions(X_std, y, classifier=ada)

fig3 = plt.figure()
cost_plot = fig3.add_subplot(111)
cost_plot.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker="o")
cost_plot.set_xlabel("Epochs")
cost_plot.set_ylabel("Sum-Squared-Error")

adaSGD = adaline.AdalineSGD(n_iter=15, eta=0.01, random_state=1)
adaSGD.fit(X_std, y)
plotting_stuff.plot_decision_regions(X_std, y, classifier=adaSGD)

fig4 = plt.figure()
cost_SGD_plot = fig4.add_subplot(111)
cost_SGD_plot.plot(range(1, len(adaSGD.cost_) + 1), adaSGD.cost_, marker="o")
cost_SGD_plot.set_xlabel("Epochs")
import numpy as np
from mystuff.plotting_stuff import plot_decision_regions
import matplotlib.pyplot as plt

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

lr = LogisticRegression(C=1000.0, random_state=0)
lr.fit(X_train_std, y_train)
y_pred = lr.predict(X_test_std)
print('Misclassified samples {0:d}'.format((y_test != y_pred).sum()))
print('Accuracy score {:.2%}'.format(accuracy_score(y_test, y_pred)))

X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
fig = plot_decision_regions(X=X_combined_std, y=y_combined, classifier=lr, test_idx=range(105, 150))
subplot = fig.add_subplot(111)
subplot.set_xlabel('petal length [standardized]')
subplot.set_ylabel('petal width [standardized]')
subplot.set_title('decision regions')
subplot.legend(loc='upper left')
plt.show()