import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from pandas import DataFrame
import pandas as pd
import seaborn as sns
# Input data into dataframe
iris=load_iris()
X=iris.data
# make data frame
df=DataFrame(X)
# add column with targets
df.columns=iris['feature_names']
df['target']=iris['target']
#add species name for plotting
df['species']=df['target'].map({0:iris.target_names[0],1:iris.target_names[1],2:iris.target_names[2]})
df.head()
# Create NumPy target array
y=df.target.values
# Split data in test and train sets using 70-30 split
#
from sklearn.model_selection import train_test_split
y=df.target.values
(X_train,X_test,y_train,y_test)=train_test_split(X,y,test_size=.3)
n_train=len(X_train)
n_test=len(X_test)
print(n_train,n_test)
# ANN sensitive to scaling so scale
#
# Import StandardScaler and scale
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
# Create classifier and train
mlp=MLPClassifier(max_iter=1000,solver='adam',random_state=1)
mlp.fit(X_train,y_train)
# Print out precent accuracy of training set
print("Percent accuracy for training data is",100*mlp.score(X_train,y_train))
# Print out percent accuracy of test set
predictions=mlp.predict(X_test)
print("Percent accuracy for test data is",100*mlp.score(X_test,y_test))
# Scale the entire data X
scaler.fit(X)
X = scaler.transform(X)
# Predict the entire dataset and add column to dataframe for Prediction and predicted species
pred=mlp.predict(X)
df['Prediction']=pred
# Plot petal length vs sepal length for actual data with hue = species and Prediction; compare
sns.relplot(x='petal length (cm)',y='sepal length (cm)',data=df,hue='species')
# Plot petal width vs sepal width for actual data with hue = species and Prediction; compare
sns.relplot(x='petal width (cm)',y='sepal width (cm)',data=df,hue='species')
#Both of the classifications are very similar with Setosa being the smallest of the flowers, then Versicolor, and the largest on average was Virginica.