import numpy as np
import pandas as pd
import seaborn as sns
import re
import requests
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib import rcParams
import matplotlib.dates as mdates
from datetime import datetime
from IPython.display import display, Math
from functions import *
plt.style.use('seaborn')
plt.rcParams.update({'axes.titlepad': 20, 'font.size': 12, 'axes.titlesize':20})
colors = [(0/255,107/255,164/255), (255/255, 128/255, 14/255), 'red', 'green', '#9E80BA', '#8EDB8E', '#58517A']
Ncolors = 10
color_map = plt.cm.Blues_r(np.linspace(0.2, 0.5, Ncolors))
#color_map = plt.cm.tab20c_r(np.linspace(0.2, 0.5, Ncolors))
#specific to this project
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
#to normalize data
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report, confusion_matrix
print("Defaults Loaded")
# Read in dataset
iris = pd.read_csv("./data/iris.csv")
display(iris[:3])
# shuffle rows
shuffled_rows = np.random.permutation(iris.index)
iris = iris.loc[shuffled_rows,:]
my_dict = {'Iris-versicolor': 1, 'Iris-virginica': 2}
iris['species'] = iris['species'].map(my_dict)
display(iris.describe().transpose())
X = iris.drop('species',axis=1)
y = iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y)
#normalize data using StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
StandardScaler(copy=True, with_mean=True, with_std=True)
# Now apply the transformations to the data:
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
print("data normalization data")
mlp = MLPClassifier(hidden_layer_sizes=(13,13,13),max_iter=500)
mlp.fit(X_train,y_train)
predictions = mlp.predict(X_test)
#classification report
print(classification_report(y_test,predictions))
#confusion matrix
True_positives = confusion_matrix(y_test,predictions)[0][0]
False_Positives = confusion_matrix(y_test,predictions)[0][1]
False_Negatives = confusion_matrix(y_test,predictions)[1][0]
True_Negatives = confusion_matrix(y_test,predictions)[1][1]
print("True_positives: {:d}".format(True_positives))
print("False_Positives: {:d}".format(False_Positives))
print("False_Negatives: {:d}".format(False_Negatives))
print("True_Negatives: {:d}".format(True_Negatives))
#AUC
auc = roc_auc_score(y_test, predictions)
print("\nAUC: {:0.3f}".format(auc))
columns = ["Cultivator", "Alchol", "Malic_Acid", "Ash", "Alcalinity_of_Ash",
"Magnesium", "Total_phenols", "Falvanoids", "Nonflavanoid_phenols",
"Proanthocyanins", "Color_intensity", "Hue", "OD280", "Proline"]
wine = pd.read_csv('./data/wine_data.csv', names = columns)
display(wine.iloc[:3,:12])
wine.describe().transpose()
print(wine.shape)
178 data points with 13 features and 1 label column
X = wine.drop('Cultivator',axis=1)
y = wine['Cultivator']
X_train, X_test, y_train, y_test = train_test_split(X, y)
#normalize data using StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
StandardScaler(copy=True, with_mean=True, with_std=True)
# Now apply the transformations to the data:
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
print("data normalization data")
mlp = MLPClassifier(hidden_layer_sizes=(13,13,13),max_iter=500)
mlp.fit(X_train,y_train)
parameters can be adjusted
predictions = mlp.predict(X_test)
print(confusion_matrix(y_test,predictions))
print(classification_report(y_test,predictions))
print(len(mlp.coefs_))
print(len(mlp.coefs_[0]))
print(len(mlp.intercepts_[0]))