In [ ]:
#next class we'll talk about neural network, then after thanksgiving we'll talk about deeplearning, and final exam review
In [ ]:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
In [ ]:
df = pd.read_csv('iris_new.csv')
df.head()
Out[ ]:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 5.0 3.2 1.2 0.2 Iris-setosa
1 6.3 2.5 4.9 1.5 Iris-versicolor
2 5.5 2.6 4.4 1.2 Iris-versicolor
3 6.0 2.7 5.1 1.6 Iris-versicolor
4 4.9 2.5 4.5 1.7 Iris-virginica
In [ ]:
df.isnull().sum() #use to find missing values
Out[ ]:
0
SepalLengthCm 0
SepalWidthCm 0
PetalLengthCm 0
PetalWidthCm 0
Species 0

In [ ]:
df['Species'].value_counts() #use to find if the data is evenly distributed
Out[ ]:
count
Species
Iris-setosa 50
Iris-versicolor 50
Iris-virginica 50

In [ ]:
#explore the stats of each numerical column
df.describe()
Out[ ]:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
In [ ]:
#do some data exploration by using visualizations
df.hist(figsize=(5,5), color='blueviolet')
plt.show()
No description has been provided for this image
In [ ]:
# split data and construct model
feature_set = df.iloc[:, 0:4]
feature_set.head()
Out[ ]:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm
0 5.0 3.2 1.2 0.2
1 6.3 2.5 4.9 1.5
2 5.5 2.6 4.4 1.2
3 6.0 2.7 5.1 1.6
4 4.9 2.5 4.5 1.7
In [ ]:
target = df.iloc[:, -1]
target.head()
Out[ ]:
Species
0 Iris-setosa
1 Iris-versicolor
2 Iris-versicolor
3 Iris-versicolor
4 Iris-virginica

In [ ]:
# split data into training and testing
train_x, test_x, train_y, test_y = train_test_split(feature_set, target, test_size=0.3,
                                                    random_state=42) #train data size is .7
In [ ]:
#model construction
classifier = SVC() #support vector machine
classifier.fit(train_x, train_y)
Out[ ]:
SVC()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVC()
In [ ]:
#use model to make preditions
predicted_y = classifier.predict(test_x)
predicted_y[0:5] #predicting first 5 rows of our data
Out[ ]:
array(['Iris-virginica', 'Iris-setosa', 'Iris-setosa', 'Iris-virginica',
       'Iris-versicolor'], dtype=object)
In [ ]:
#compare it to our real data
test_y[0:5]
Out[ ]:
Species
73 Iris-virginica
18 Iris-setosa
118 Iris-setosa
78 Iris-virginica
76 Iris-virginica

In [ ]:
#accuracy
acy = accuracy_score(test_y, predicted_y)
print(acy)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/tmp/ipython-input-4041155894.py in <cell line: 0>()
      1 #accuracy
----> 2 acy = accuracy_score(test_y, predicted_y)
      3 print(acy)

NameError: name 'accuracy_score' is not defined
In [ ]:
rp = classification_report(test_y, predicted_y)
print(rp)
                 precision    recall  f1-score   support

    Iris-setosa       1.00      1.00      1.00        15
Iris-versicolor       0.89      1.00      0.94        16
 Iris-virginica       1.00      0.86      0.92        14

       accuracy                           0.96        45
      macro avg       0.96      0.95      0.95        45
   weighted avg       0.96      0.96      0.96        45

In [ ]:
#create confusion matrix for vizualization
cm = confusion_matrix(test_y, predicted_y, labels=classifier.classes_)
cmd = ConfusionMatrixDisplay(cm, display_labels=classifier.classes_)
cmd.plot()
plt.show()
No description has been provided for this image
In [ ]: