-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathiris_scikit_mlp.py
More file actions
60 lines (45 loc) · 1.58 KB
/
iris_scikit_mlp.py
File metadata and controls
60 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
SECTION 1 : Load and setup data for training
"""
import pandas as pd
from sklearn.model_selection import train_test_split
# Load dataset
datatrain = pd.read_csv('../Datasets/iris/iris.csv')
# Change string value to numeric
datatrain.loc[datatrain['species']=='Iris-setosa', 'species']=0
datatrain.loc[datatrain['species']=='Iris-versicolor', 'species']=1
datatrain.loc[datatrain['species']=='Iris-virginica', 'species']=2
datatrain = datatrain.apply(pd.to_numeric)
# Change dataframe to array
datatrain_array = datatrain.values
# Split x and y (feature and target)
X_train, X_test, y_train, y_test = train_test_split(datatrain_array[:,:4],
datatrain_array[:,4],
test_size=0.2)
"""
SECTION 2 : Build and Train Model
Multilayer perceptron model, with one hidden layer.
input layer : 4 neuron, represents the feature of Iris
hidden layer : 10 neuron, activation using ReLU
output layer : 3 neuron, represents the class of Iris, Softmax Layer
optimizer = stochastic gradient descent with no batch-size
loss function = categorical cross entropy
learning rate = 0.01
epoch = 500
"""
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(10),
solver='sgd',
learning_rate_init=0.01,
max_iter=500,
random_state=113)
# Train the model
mlp.fit(X_train, y_train)
# Test the model
print mlp.score(X_test,y_test)
sl = 5.8
sw = 4
pl = 1.2
pw = 0.2
data = [[sl,sw,pl,pw]]
print mlp.predict(data)