In: Computer Science
Use the multi-layer perceptron algorithm to learn a model that classifies IRIS flower dataset.
Split the dataset into a train set to train the algorithm and test set to test the algorithm. Calculate the accuracy.
Use Scikit-Learn
#Python3 code
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.neural_network import MLPClassifier # neural network
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.datasets import load_iris
iris = load_iris() #load iris dataset
X=iris.data #input variable
y=iris.target #output variable
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.5) #spilting of the data into train and test
clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(3, 3), random_state=1)
clf.fit(X_train,y_train) #training the MLP classifier on the training data to create the model
prediction = clf.predict(X_test) #model validation on test data
print("Predicted Class Label:\n")
print(prediction)
print("Actual Class Label:\n")
print(y_test)
print('The accuracy of the Multi-layer Perceptron is:',metrics.accuracy_score(prediction,y_test)) #calculate the model accuracy.
#OUTPUT
Predicted Class Label:
[2 1 0 0 0 0 2 1 1 2 0 1 2 0 0 1 2 1 0 2 0 0 2 1 0 0 0 2 1 0 1 2
2 2 2 2 1
0 2 2 1 2 1 1 0 0 1 2 0 0 0 1 0 2 2 1 2 2 2 0 2 2 2 2 0 0 1 2 0 0 1
1 2 2
0]
Actual Class Label:
[2 1 0 0 0 0 2 1 1 2 0 1 2 0 0 1 2 1 0 1 0 0 2 1 0 0 0 2 1 0 1 2
2 2 2 2 1
0 2 2 1 2 1 1 0 0 1 2 0 0 0 1 0 2 2 1 1 2 2 0 2 2 1 2 0 0 1 2 0 0 1
1 2 2
0]
The accuracy of the Multi-layer Perceptron is: 0.96