Skip to content

Commit b602c19

Browse files
committed
Added two neural networks classifying iris flowers into 3 subspecies
1 parent d5551f6 commit b602c19

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

Neural_Network/iris.csv

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
"sepal.length","sepal.width","petal.length","petal.width","variety"
2+
5.1,3.5,1.4,.2,"Setosa"
3+
4.9,3,1.4,.2,"Setosa"
4+
4.7,3.2,1.3,.2,"Setosa"
5+
4.6,3.1,1.5,.2,"Setosa"
6+
5,3.6,1.4,.2,"Setosa"
7+
5.4,3.9,1.7,.4,"Setosa"
8+
4.6,3.4,1.4,.3,"Setosa"
9+
5,3.4,1.5,.2,"Setosa"
10+
4.4,2.9,1.4,.2,"Setosa"
11+
4.9,3.1,1.5,.1,"Setosa"
12+
5.4,3.7,1.5,.2,"Setosa"
13+
4.8,3.4,1.6,.2,"Setosa"
14+
4.8,3,1.4,.1,"Setosa"
15+
4.3,3,1.1,.1,"Setosa"
16+
5.8,4,1.2,.2,"Setosa"
17+
5.7,4.4,1.5,.4,"Setosa"
18+
5.4,3.9,1.3,.4,"Setosa"
19+
5.1,3.5,1.4,.3,"Setosa"
20+
5.7,3.8,1.7,.3,"Setosa"
21+
5.1,3.8,1.5,.3,"Setosa"
22+
5.4,3.4,1.7,.2,"Setosa"
23+
5.1,3.7,1.5,.4,"Setosa"
24+
4.6,3.6,1,.2,"Setosa"
25+
5.1,3.3,1.7,.5,"Setosa"
26+
4.8,3.4,1.9,.2,"Setosa"
27+
5,3,1.6,.2,"Setosa"
28+
5,3.4,1.6,.4,"Setosa"
29+
5.2,3.5,1.5,.2,"Setosa"
30+
5.2,3.4,1.4,.2,"Setosa"
31+
4.7,3.2,1.6,.2,"Setosa"
32+
4.8,3.1,1.6,.2,"Setosa"
33+
5.4,3.4,1.5,.4,"Setosa"
34+
5.2,4.1,1.5,.1,"Setosa"
35+
5.5,4.2,1.4,.2,"Setosa"
36+
4.9,3.1,1.5,.2,"Setosa"
37+
5,3.2,1.2,.2,"Setosa"
38+
5.5,3.5,1.3,.2,"Setosa"
39+
4.9,3.6,1.4,.1,"Setosa"
40+
4.4,3,1.3,.2,"Setosa"
41+
5.1,3.4,1.5,.2,"Setosa"
42+
5,3.5,1.3,.3,"Setosa"
43+
4.5,2.3,1.3,.3,"Setosa"
44+
4.4,3.2,1.3,.2,"Setosa"
45+
5,3.5,1.6,.6,"Setosa"
46+
5.1,3.8,1.9,.4,"Setosa"
47+
4.8,3,1.4,.3,"Setosa"
48+
5.1,3.8,1.6,.2,"Setosa"
49+
4.6,3.2,1.4,.2,"Setosa"
50+
5.3,3.7,1.5,.2,"Setosa"
51+
5,3.3,1.4,.2,"Setosa"
52+
7,3.2,4.7,1.4,"Versicolor"
53+
6.4,3.2,4.5,1.5,"Versicolor"
54+
6.9,3.1,4.9,1.5,"Versicolor"
55+
5.5,2.3,4,1.3,"Versicolor"
56+
6.5,2.8,4.6,1.5,"Versicolor"
57+
5.7,2.8,4.5,1.3,"Versicolor"
58+
6.3,3.3,4.7,1.6,"Versicolor"
59+
4.9,2.4,3.3,1,"Versicolor"
60+
6.6,2.9,4.6,1.3,"Versicolor"
61+
5.2,2.7,3.9,1.4,"Versicolor"
62+
5,2,3.5,1,"Versicolor"
63+
5.9,3,4.2,1.5,"Versicolor"
64+
6,2.2,4,1,"Versicolor"
65+
6.1,2.9,4.7,1.4,"Versicolor"
66+
5.6,2.9,3.6,1.3,"Versicolor"
67+
6.7,3.1,4.4,1.4,"Versicolor"
68+
5.6,3,4.5,1.5,"Versicolor"
69+
5.8,2.7,4.1,1,"Versicolor"
70+
6.2,2.2,4.5,1.5,"Versicolor"
71+
5.6,2.5,3.9,1.1,"Versicolor"
72+
5.9,3.2,4.8,1.8,"Versicolor"
73+
6.1,2.8,4,1.3,"Versicolor"
74+
6.3,2.5,4.9,1.5,"Versicolor"
75+
6.1,2.8,4.7,1.2,"Versicolor"
76+
6.4,2.9,4.3,1.3,"Versicolor"
77+
6.6,3,4.4,1.4,"Versicolor"
78+
6.8,2.8,4.8,1.4,"Versicolor"
79+
6.7,3,5,1.7,"Versicolor"
80+
6,2.9,4.5,1.5,"Versicolor"
81+
5.7,2.6,3.5,1,"Versicolor"
82+
5.5,2.4,3.8,1.1,"Versicolor"
83+
5.5,2.4,3.7,1,"Versicolor"
84+
5.8,2.7,3.9,1.2,"Versicolor"
85+
6,2.7,5.1,1.6,"Versicolor"
86+
5.4,3,4.5,1.5,"Versicolor"
87+
6,3.4,4.5,1.6,"Versicolor"
88+
6.7,3.1,4.7,1.5,"Versicolor"
89+
6.3,2.3,4.4,1.3,"Versicolor"
90+
5.6,3,4.1,1.3,"Versicolor"
91+
5.5,2.5,4,1.3,"Versicolor"
92+
5.5,2.6,4.4,1.2,"Versicolor"
93+
6.1,3,4.6,1.4,"Versicolor"
94+
5.8,2.6,4,1.2,"Versicolor"
95+
5,2.3,3.3,1,"Versicolor"
96+
5.6,2.7,4.2,1.3,"Versicolor"
97+
5.7,3,4.2,1.2,"Versicolor"
98+
5.7,2.9,4.2,1.3,"Versicolor"
99+
6.2,2.9,4.3,1.3,"Versicolor"
100+
5.1,2.5,3,1.1,"Versicolor"
101+
5.7,2.8,4.1,1.3,"Versicolor"
102+
6.3,3.3,6,2.5,"Virginica"
103+
5.8,2.7,5.1,1.9,"Virginica"
104+
7.1,3,5.9,2.1,"Virginica"
105+
6.3,2.9,5.6,1.8,"Virginica"
106+
6.5,3,5.8,2.2,"Virginica"
107+
7.6,3,6.6,2.1,"Virginica"
108+
4.9,2.5,4.5,1.7,"Virginica"
109+
7.3,2.9,6.3,1.8,"Virginica"
110+
6.7,2.5,5.8,1.8,"Virginica"
111+
7.2,3.6,6.1,2.5,"Virginica"
112+
6.5,3.2,5.1,2,"Virginica"
113+
6.4,2.7,5.3,1.9,"Virginica"
114+
6.8,3,5.5,2.1,"Virginica"
115+
5.7,2.5,5,2,"Virginica"
116+
5.8,2.8,5.1,2.4,"Virginica"
117+
6.4,3.2,5.3,2.3,"Virginica"
118+
6.5,3,5.5,1.8,"Virginica"
119+
7.7,3.8,6.7,2.2,"Virginica"
120+
7.7,2.6,6.9,2.3,"Virginica"
121+
6,2.2,5,1.5,"Virginica"
122+
6.9,3.2,5.7,2.3,"Virginica"
123+
5.6,2.8,4.9,2,"Virginica"
124+
7.7,2.8,6.7,2,"Virginica"
125+
6.3,2.7,4.9,1.8,"Virginica"
126+
6.7,3.3,5.7,2.1,"Virginica"
127+
7.2,3.2,6,1.8,"Virginica"
128+
6.2,2.8,4.8,1.8,"Virginica"
129+
6.1,3,4.9,1.8,"Virginica"
130+
6.4,2.8,5.6,2.1,"Virginica"
131+
7.2,3,5.8,1.6,"Virginica"
132+
7.4,2.8,6.1,1.9,"Virginica"
133+
7.9,3.8,6.4,2,"Virginica"
134+
6.4,2.8,5.6,2.2,"Virginica"
135+
6.3,2.8,5.1,1.5,"Virginica"
136+
6.1,2.6,5.6,1.4,"Virginica"
137+
7.7,3,6.1,2.3,"Virginica"
138+
6.3,3.4,5.6,2.4,"Virginica"
139+
6.4,3.1,5.5,1.8,"Virginica"
140+
6,3,4.8,1.8,"Virginica"
141+
6.9,3.1,5.4,2.1,"Virginica"
142+
6.7,3.1,5.6,2.4,"Virginica"
143+
6.9,3.1,5.1,2.3,"Virginica"
144+
5.8,2.7,5.1,1.9,"Virginica"
145+
6.8,3.2,5.9,2.3,"Virginica"
146+
6.7,3.3,5.7,2.5,"Virginica"
147+
6.7,3,5.2,2.3,"Virginica"
148+
6.3,2.5,5,1.9,"Virginica"
149+
6.5,3,5.2,2,"Virginica"
150+
6.2,3.4,5.4,2.3,"Virginica"
151+
5.9,3,5.1,1.8,"Virginica"

Neural_Network/neural_network.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from sklearn.metrics import classification_report, confusion_matrix
2+
from sklearn.model_selection import train_test_split
3+
from sklearn.neural_network import MLPClassifier
4+
import pandas as pd
5+
6+
# ----------------------------------------------------------------- #
7+
# An implementation of two neural networks with different topology #
8+
# classifying iris flowers into three species based on sepal length #
9+
# and width as well as petal length and width. #
10+
# #
11+
# Dataset used: #
12+
# https://gist.github.com/netj/8836201 #
13+
# ----------------------------------------------------------------- #
14+
15+
16+
def read_data(file):
17+
"""
18+
Reading the data and transforming the species from text to integers.
19+
:param file: String
20+
:return: numpy.ndarray
21+
"""
22+
data = pd.read_csv(file)
23+
data.loc[data["variety"] == 'Setosa', 'variety'] = 0
24+
data.loc[data["variety"] == 'Versicolor', 'variety'] = 1
25+
data.loc[data["variety"] == 'Virginica', 'variety'] = 2
26+
27+
return data.values
28+
29+
30+
data = read_data("iris.csv")
31+
X_train, X_test, y_train, y_test = train_test_split(data[:, :4], data[:, -1:], test_size=0.2)
32+
33+
# A neural network which consists of
34+
# an input layer (R^4),
35+
# one hidden layer (R^10)
36+
# and an output layer (R^3).
37+
# Visualization can also be found in the README
38+
mlp_single_hidden_layer = MLPClassifier(hidden_layer_sizes=(10,), activation="relu", solver="adam", max_iter=350,
39+
batch_size=10, verbose=True)
40+
41+
# A neural network which consists of
42+
# an input layer (R^4),
43+
# two hidden layers (R^5 and R^3)
44+
# and an output layer (R^3).
45+
# Visualization can be found in the README as well
46+
mlp_multi_hidden_layer = MLPClassifier(hidden_layer_sizes=(5,3), activation="tanh", solver="adam", max_iter=500,
47+
batch_size=10, verbose=True)
48+
49+
# Training both networks
50+
mlp_single_hidden_layer.fit(X_train, y_train.ravel())
51+
mlp_multi_hidden_layer.fit(X_train, y_train.ravel())
52+
print("--------------------------------\n")
53+
print("Result of training (Single hidden layer): %5.3f" % mlp_single_hidden_layer.score(X_train, y_train))
54+
print("Result of training (Multiple hidden layers): %5.3f" % mlp_multi_hidden_layer.score(X_train, y_train))
55+
print("--------------------------------\n")
56+
57+
# Evaluating the model using the test data
58+
pred_single = mlp_single_hidden_layer.predict(X_test)
59+
pred_multi = mlp_multi_hidden_layer.predict(X_test)
60+
61+
print("Confusion Matrix (Single hidden layer):")
62+
print(confusion_matrix(y_test, pred_single))
63+
print("\nClassification Report (Single hidden layer):")
64+
print(classification_report(y_test, pred_single))
65+
print("\nConfusion Matrix (Multiple hidden layers):")
66+
print(confusion_matrix(y_test, pred_multi))
67+
print("\nClassification Report (Multiple hidden layers):")
68+
print(classification_report(y_test, pred_multi))
69+
print("--------------------------------\n")
70+
71+
print("Accuracy while testing: (Single hidden layer): %5.3f" % mlp_single_hidden_layer.score(X_test, y_test))
72+
print("Accuracy while testing: (Multiple hidden layers): %5.3f" % mlp_multi_hidden_layer.score(X_test, y_test))

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Additionally, if you want a graphical reprentation of the decision tree, you'll
2020
3. [Support Vector Machine](#svm)
2121
4. [K-Means Clustering](#k-means)
2222
5. [Decision Trees](#dec-trees)
23+
6. [Neural Networks](#nn)
2324

2425
<a id="lin-reg"></a>
2526
## Linear Regression
@@ -114,3 +115,20 @@ Image generated using decision_tree.py.
114115
- **~78%** with a depth of *4* and *information gain* as attribute selection measure
115116
- **~76%** with a depth of *3* and *information gain* as attribute selection measure
116117
- **~76%** with a depth of *4* and *gini impurity* as attribute selection measure
118+
119+
<a id="nn"></a>
120+
## Neural Networks
121+
![A network with the topology 4-10-3](https://i.gyazo.com/c9e40613d43415769cb5f5544d1b580f.png)
122+
The first neural network declared in neural_network.py
123+
124+
![A neural network with the topology 4-5-3-3](https://i.gyazo.com/239189a52dc30c5521015c525ccf6fd3.png)
125+
The second network declared in neural_network.py
126+
127+
**Definition:**
128+
> Artificial neural networks (ANN) or connectionist systems are computing systems that are inspired by, but not identical to, biological neural networks that constitute animal brains. Such systems "learn" to perform tasks by considering examples, generally without being programmed with task-specific rules.
129+
> -- [Wikipedia](https://en.wikipedia.org/wiki/Artificial_neural_network)
130+
131+
**Problem solved using neural networks:** Classifcation of iris flowers based on petal length and width as well as sepal length and width.
132+
133+
134+
**Accuracy:** Both networks get a mean accuracy of around 0.9 to 1.0.

0 commit comments

Comments
 (0)