forked from Ishaan28malik/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iris - SVM.py
129 lines (63 loc) · 1.96 KB
/
Iris - SVM.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# coding: utf-8
# # Iris Dataset - SVM
# In[2]:
# Importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# In[13]:
#Importing dataset
data = pd.read_csv(r'C:\Users\Dell\Desktop\Machine Learning\Sample Dataset\IRIS.csv')
data.shape
# In[14]:
data.head(5)
# In[15]:
#Check if any null value is present
data.isnull().values.any()
# In[16]:
#Correlation
import seaborn as sns
import matplotlib.pyplot as plt
cormat = data.corr()
top_corr_features = cormat.index
plt.figure(figsize=(20,20))
#Plotting heat map
g = sns.heatmap(data[top_corr_features].corr(), annot = True)
# In[17]:
data.corr()
# In[18]:
#Splitting Training and Testing datset
from sklearn.model_selection import train_test_split
features_column = [ 'sepal_length', 'sepal_width', 'petal_length', 'petal_width' ]
prediction_class = ['species']
X = data[features_column].values
Y = data[prediction_class].values
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.30, random_state = 10)
(X, Y)
# In[19]:
X_train.shape
# In[20]:
X_test.shape
# In[21]:
Y_train.shape
# In[22]:
Y_test.shape
# In[23]:
#Checking null values
print("Total no of rows : {0}".format(len(data)))
print("Number of missing rows in sepal_length : {0}" .format(len(data.loc[data['sepal_length'] ==0 ])))
print("Number of missing rows in sepal_width : {0}" .format(len(data.loc[data['sepal_width'] ==0 ])))
print("Number of missing rows in petal_length : {0}" .format(len(data.loc[data['petal_length'] ==0 ])))
print("Number of missing rows in petal_width : {0}" .format(len(data.loc[data['petal_width'] ==0 ])))
# In[24]:
#Applying SVM to model - Most Accurate
from sklearn import svm
model = svm.SVC()
model.fit(X_train, Y_train.ravel())
#Predicting Accuracy of model
predict = model.predict(X_test)
from sklearn import metrics
print("Accuracy of SVM = {0:.3f}".format(metrics.accuracy_score(Y_test, predict)))
# In[ ]:
# In[ ]: