Monday, December 23, 2019

Machine Learning Day 5

In [2]:
import numpy as np
In [4]:
import pandas as pd
import seaborn as sns
In [6]:
from matplotlib import pyplot as plt
In [7]:
#today we will do support vector machine (svm)
In [8]:
df=pd.read_csv('Social_Network_Ads.csv')
df.head()
Out[8]:
User IDGenderAgeEstimatedSalaryPurchased
015624510Male19190000
115810944Male35200000
215668575Female26430000
315603246Female27570000
415804002Male19760000
In [9]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
User ID            400 non-null int64
Gender             400 non-null object
Age                400 non-null int64
EstimatedSalary    400 non-null int64
Purchased          400 non-null int64
dtypes: int64(4), object(1)
memory usage: 15.7+ KB
In [11]:
df.drop('User ID',axis=1,inplace=True)
In [12]:
df.head()
Out[12]:
GenderAgeEstimatedSalaryPurchased
0Male19190000
1Male35200000
2Female26430000
3Female27570000
4Male19760000
In [13]:
sns.heatmap(df.corr())
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x26d3fb334a8>
In [14]:
gend={"Male":0,"Female":1}
In [16]:
df['Gender']=df['Gender'].map(gend)
In [17]:
df.head()
Out[17]:
GenderAgeEstimatedSalaryPurchased
0019190000
1035200000
2126430000
3127570000
4019760000
In [18]:
sns.heatmap(df.corr())
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x26d40e732b0>
In [19]:
df.drop('Gender',axis=1,inplace=True)
In [20]:
df.head()
Out[20]:
AgeEstimatedSalaryPurchased
019190000
135200000
226430000
327570000
419760000
In [21]:
from sklearn.preprocessing import StandardScaler
In [22]:
scaler=StandardScaler()
In [24]:
scaler.fit(df.drop('Purchased',axis=1))
Out[24]:
StandardScaler(copy=True, with_mean=True, with_std=True)
In [25]:
scale_arr=scaler.transform(df.drop('Purchased',axis=1))
In [26]:
scale_arr
Out[26]:
array([[-1.78179743, -1.49004624],
       [-0.25358736, -1.46068138],
       [-1.11320552, -0.78528968],
       [-1.01769239, -0.37418169],
       [-1.78179743,  0.18375059],
       [-1.01769239, -0.34481683],
       [-1.01769239,  0.41866944],
       [-0.54012675,  2.35674998],
       [-1.20871865, -1.07893824],
       [-0.25358736, -0.13926283],
       [-1.11320552,  0.30121002],
       [-1.11320552, -0.52100597],
       [-1.6862843 ,  0.47739916],
       [-0.54012675, -1.51941109],
       [-1.87731056,  0.35993973],
       [-0.82666613,  0.30121002],
       [ 0.89257019, -1.3138571 ],
       [ 0.70154394, -1.28449224],
       [ 0.79705706, -1.22576253],
       [ 0.98808332, -1.19639767],
       [ 0.70154394, -1.40195167],
       [ 0.89257019, -0.60910054],
       [ 0.98808332, -0.84401939],
       [ 0.70154394, -1.40195167],
       [ 0.79705706, -1.37258681],
       [ 0.89257019, -1.46068138],
       [ 1.08359645, -1.22576253],
       [ 0.89257019, -1.16703281],
       [-0.82666613, -0.78528968],
       [-0.63563988, -1.51941109],
       [-0.63563988,  0.12502088],
       [-1.01769239,  1.97500684],
       [-1.59077117, -1.5781408 ],
       [-0.92217926, -0.75592482],
       [-1.01769239,  0.59485858],
       [-0.25358736, -1.25512738],
       [-0.44461362, -1.22576253],
       [-0.73115301, -0.60910054],
       [-1.11320552,  0.06629116],
       [-1.01769239, -1.13766796],
       [-1.01769239, -1.54877595],
       [-0.44461362, -0.55037082],
       [-0.25358736,  1.123426  ],
       [-0.73115301, -1.60750566],
       [-0.92217926,  0.41866944],
       [-1.39974491, -1.46068138],
       [-1.20871865,  0.27184516],
       [-1.01769239, -0.46227625],
       [-0.73115301,  1.91627713],
       [-0.63563988,  0.56549373],
       [-1.30423178, -1.1083031 ],
       [-1.87731056, -0.75592482],
       [-0.82666613,  0.38930459],
       [-0.25358736, -1.37258681],
       [-1.01769239, -0.34481683],
       [-1.30423178, -0.4329114 ],
       [-1.39974491, -0.63846539],
       [-0.92217926,  0.27184516],
       [-1.49525804, -1.51941109],
       [-0.54012675,  1.38770971],
       [-1.01769239, -1.46068138],
       [-1.20871865,  0.50676401],
       [-1.39974491, -0.10989798],
       [-0.54012675,  1.47580428],
       [ 2.03872775,  0.38930459],
       [-1.30423178, -0.34481683],
       [-1.30423178, -1.49004624],
       [-1.39974491,  0.35993973],
       [-1.49525804, -0.19799255],
       [-0.63563988, -0.05116826],
       [-1.20871865,  0.30121002],
       [-1.30423178, -1.25512738],
       [-1.6862843 , -1.37258681],
       [-0.44461362,  1.27025028],
       [-0.54012675, -1.51941109],
       [-0.34910049,  1.24088543],
       [-1.87731056, -0.52100597],
       [-1.49525804, -1.25512738],
       [-0.92217926,  0.50676401],
       [-1.11320552, -1.54877595],
       [-0.73115301,  0.30121002],
       [ 0.12846516, -0.81465453],
       [-1.6862843 , -0.60910054],
       [-0.25358736,  0.53612887],
       [-0.73115301, -0.2273574 ],
       [-0.63563988,  1.41707457],
       [-1.30423178, -0.4329114 ],
       [-0.92217926,  0.4480343 ],
       [-1.11320552,  0.33057487],
       [-0.25358736, -0.57973568],
       [-1.49525804,  0.33057487],
       [-0.73115301,  1.35834485],
       [-1.11320552, -1.60750566],
       [-0.82666613, -1.22576253],
       [-0.82666613,  0.38930459],
       [-0.25358736, -0.75592482],
       [-0.25358736, -1.3138571 ],
       [-0.92217926,  1.56389885],
       [-0.25358736,  0.09565602],
       [-0.92217926, -0.96147882],
       [-1.01769239,  0.53612887],
       [-0.92217926, -0.31545197],
       [-0.54012675,  0.47739916],
       [-0.44461362,  2.32738512],
       [-1.78179743, -1.43131652],
       [-1.59077117,  0.06629116],
       [-1.11320552, -1.02020853],
       [-1.01769239,  0.56549373],
       [-1.11320552,  0.47739916],
       [ 0.03295203,  0.30121002],
       [ 0.12846516,  0.03692631],
       [-0.0625611 ,  0.03692631],
       [ 0.03295203, -0.25672226],
       [-0.0625611 , -0.4329114 ],
       [ 0.41500455,  0.30121002],
       [ 0.22397829, -0.37418169],
       [-0.25358736,  0.15438573],
       [-0.15807423, -0.52100597],
       [ 0.22397829, -0.31545197],
       [ 0.31949142, -0.31545197],
       [-0.15807423,  0.15438573],
       [-0.0625611 ,  0.06629116],
       [ 0.22397829,  0.15438573],
       [-0.25358736, -0.49164111],
       [ 0.31949142, -0.55037082],
       [ 0.12846516, -0.25672226],
       [ 0.41500455, -0.13926283],
       [-1.11320552, -1.1083031 ],
       [-0.73115301, -1.54877595],
       [-1.11320552,  0.41866944],
       [-0.63563988, -0.34481683],
       [-0.44461362, -1.13766796],
       [-0.73115301,  0.50676401],
       [-1.59077117, -0.05116826],
       [-0.92217926, -0.4329114 ],
       [-1.39974491, -0.19799255],
       [-1.6862843 ,  0.35993973],
       [-0.73115301,  1.09406114],
       [-0.92217926, -0.31545197],
       [-1.78179743, -1.3138571 ],
       [-1.78179743,  0.4480343 ],
       [-1.87731056, -0.05116826],
       [-0.25358736, -0.31545197],
       [-0.73115301,  0.56549373],
       [-0.34910049, -1.3138571 ],
       [-1.30423178,  0.56549373],
       [-1.01769239,  0.77104772],
       [ 0.31949142, -1.16703281],
       [-0.82666613, -0.25672226],
       [-1.6862843 ,  0.12502088],
       [-1.11320552, -1.60750566],
       [ 0.31949142, -0.72655996],
       [-0.63563988,  0.18375059],
       [-0.15807423, -0.57973568],
       [ 0.22397829, -0.66783025],
       [-0.63563988, -1.60750566],
       [ 0.79705706, -0.31545197],
       [-0.82666613,  0.15438573],
       [-1.11320552, -1.16703281],
       [-0.54012675,  1.91627713],
       [-0.54012675,  0.88850715],
       [-1.20871865,  0.59485858],
       [-0.0625611 , -1.07893824],
       [-0.25358736, -0.93211396],
       [-0.44461362, -0.02180341],
       [-1.87731056,  0.47739916],
       [-1.49525804, -0.4329114 ],
       [-0.25358736,  0.03692631],
       [-0.82666613,  2.29802026],
       [-0.82666613, -0.66783025],
       [-1.59077117,  0.53612887],
       [-0.34910049,  1.32898   ],
       [-1.11320552,  1.41707457],
       [-0.34910049, -0.78528968],
       [-0.34910049,  0.06629116],
       [-1.39974491, -1.22576253],
       [-0.25358736, -0.66783025],
       [-1.20871865, -1.40195167],
       [-1.30423178, -1.37258681],
       [-0.63563988, -1.04957339],
       [-1.11320552, -1.5781408 ],
       [-0.63563988,  0.03692631],
       [-0.54012675,  1.38770971],
       [-0.44461362, -0.78528968],
       [-0.44461362, -0.28608712],
       [-0.63563988, -0.10989798],
       [-1.6862843 ,  0.35993973],
       [-0.44461362, -0.84401939],
       [-0.25358736,  0.06629116],
       [-0.92217926, -1.1083031 ],
       [-1.30423178,  0.41866944],
       [-1.78179743, -1.28449224],
       [-0.82666613, -0.78528968],
       [-1.78179743,  0.00756145],
       [-0.92217926,  0.56549373],
       [-0.34910049, -0.78528968],
       [-0.73115301,  0.27184516],
       [-1.6862843 , -0.99084367],
       [-1.11320552,  0.30121002],
       [-0.25358736, -1.40195167],
       [-0.25358736, -0.9027491 ],
       [ 1.08359645,  0.12502088],
       [ 0.12846516,  1.88691227],
       [ 0.31949142,  0.03692631],
       [ 1.94321462,  0.917872  ],
       [ 0.89257019, -0.66783025],
       [ 1.65667523,  1.76945285],
       [ 1.37013584,  1.29961514],
       [ 0.22397829,  2.12183112],
       [ 0.79705706, -1.40195167],
       [ 0.98808332,  0.77104772],
       [ 1.37013584,  2.35674998],
       [ 2.03872775, -0.81465453],
       [-0.25358736, -0.34481683],
       [ 0.89257019, -0.78528968],
       [ 2.13424088,  1.123426  ],
       [ 1.08359645, -0.13926283],
       [ 0.22397829,  0.2424803 ],
       [ 0.79705706,  0.77104772],
       [ 2.03872775,  2.15119598],
       [ 0.31949142,  0.30121002],
       [-0.25358736,  0.62422344],
       [-0.0625611 ,  2.18056084],
       [ 2.13424088,  0.94723686],
       [-0.25358736, -0.28608712],
       [-0.0625611 , -0.49164111],
       [-0.15807423,  1.65199342],
       [ 1.75218836,  1.85754742],
       [ 0.22397829,  0.06629116],
       [ 0.41500455,  0.30121002],
       [-0.25358736,  2.26865541],
       [ 0.12846516, -0.81465453],
       [ 0.22397829,  1.09406114],
       [ 1.08359645,  0.47739916],
       [ 0.03295203,  1.24088543],
       [ 0.79705706,  0.27184516],
       [ 0.22397829, -0.37418169],
       [-0.0625611 ,  0.30121002],
       [ 0.79705706,  0.35993973],
       [ 1.46564897,  2.15119598],
       [ 0.41500455,  2.32738512],
       [ 0.03295203, -0.31545197],
       [ 1.17910958,  0.53612887],
       [ 1.75218836,  1.00596657],
       [ 0.31949142,  0.06629116],
       [ 1.27462271,  2.23929055],
       [-0.25358736, -0.57973568],
       [ 1.84770149,  1.53453399],
       [ 0.31949142, -0.52100597],
       [-0.25358736,  0.80041258],
       [ 0.60603081, -0.9027491 ],
       [-0.0625611 , -0.52100597],
       [ 0.98808332,  1.88691227],
       [-0.0625611 ,  2.23929055],
       [ 1.17910958, -0.75592482],
       [ 1.37013584,  0.59485858],
       [ 0.31949142,  0.06629116],
       [ 0.22397829, -0.37418169],
       [ 1.94321462,  0.74168287],
       [ 0.70154394,  1.7988177 ],
       [-0.25358736,  0.21311545],
       [-0.15807423,  2.18056084],
       [ 1.65667523,  1.62262856],
       [-0.25358736,  0.06629116],
       [ 0.98808332,  0.59485858],
       [ 0.41500455,  1.123426  ],
       [ 0.22397829,  0.15438573],
       [-0.0625611 ,  0.12502088],
       [ 0.89257019,  2.18056084],
       [ 0.22397829, -0.25672226],
       [ 0.51051768,  1.85754742],
       [ 2.03872775,  0.18375059],
       [ 2.13424088, -0.81465453],
       [ 0.12846516,  1.06469629],
       [ 1.84770149, -1.28449224],
       [ 1.84770149,  0.12502088],
       [ 0.03295203,  0.03692631],
       [ 1.08359645,  0.53612887],
       [ 1.37013584, -0.93211396],
       [ 1.17910958, -0.99084367],
       [ 2.03872775,  0.53612887],
       [-0.25358736, -0.25672226],
       [-0.0625611 ,  0.00756145],
       [ 1.37013584, -1.43131652],
       [ 0.98808332,  2.09246627],
       [-0.0625611 ,  0.68295315],
       [-0.0625611 , -0.2273574 ],
       [ 0.98808332,  2.0043717 ],
       [ 0.31949142,  0.27184516],
       [-0.0625611 ,  0.2424803 ],
       [ 0.12846516,  1.88691227],
       [ 1.08359645,  0.56549373],
       [ 1.65667523, -0.9027491 ],
       [-0.0625611 ,  0.21311545],
       [-0.25358736, -0.37418169],
       [-0.15807423, -0.19799255],
       [ 0.41500455,  0.09565602],
       [ 0.51051768,  1.24088543],
       [ 0.70154394,  0.27184516],
       [ 0.79705706,  1.38770971],
       [ 1.94321462, -0.93211396],
       [ 0.98808332,  0.12502088],
       [-0.0625611 ,  1.97500684],
       [-0.0625611 ,  0.27184516],
       [ 0.22397829, -0.28608712],
       [ 0.41500455, -0.46227625],
       [ 1.27462271,  1.88691227],
       [ 0.89257019,  1.27025028],
       [-0.15807423,  1.62262856],
       [ 0.03295203, -0.57973568],
       [ 0.41500455,  0.00756145],
       [ 0.12846516,  0.77104772],
       [ 0.03295203, -0.57973568],
       [ 1.08359645,  2.09246627],
       [ 0.12846516,  0.27184516],
       [ 0.12846516,  0.15438573],
       [ 1.5611621 ,  1.00596657],
       [-0.25358736, -0.4329114 ],
       [ 0.70154394, -1.1083031 ],
       [-0.15807423, -0.28608712],
       [ 1.37013584,  2.0043717 ],
       [ 1.46564897,  0.35993973],
       [ 0.31949142, -0.52100597],
       [ 0.98808332, -1.16703281],
       [ 0.98808332,  1.7988177 ],
       [ 0.31949142, -0.28608712],
       [ 0.31949142,  0.06629116],
       [ 0.41500455,  0.15438573],
       [-0.15807423,  1.41707457],
       [ 0.89257019,  1.09406114],
       [ 0.03295203, -0.55037082],
       [ 0.98808332,  1.44643942],
       [ 0.41500455, -0.13926283],
       [ 0.22397829, -0.13926283],
       [ 1.84770149, -0.28608712],
       [-0.15807423, -0.46227625],
       [ 1.94321462,  2.18056084],
       [-0.25358736,  0.27184516],
       [ 0.03295203, -0.4329114 ],
       [ 0.12846516,  1.53453399],
       [ 1.46564897,  1.00596657],
       [-0.25358736,  0.15438573],
       [ 0.03295203, -0.13926283],
       [ 0.89257019, -0.55037082],
       [ 0.89257019,  1.03533143],
       [ 0.31949142, -0.19799255],
       [ 1.46564897,  0.06629116],
       [ 1.5611621 ,  1.123426  ],
       [ 0.12846516,  0.21311545],
       [ 0.03295203, -0.25672226],
       [ 0.03295203,  1.27025028],
       [-0.0625611 ,  0.15438573],
       [ 0.41500455,  0.59485858],
       [-0.0625611 , -0.37418169],
       [-0.15807423,  0.85914229],
       [ 2.13424088, -1.04957339],
       [ 1.5611621 ,  0.00756145],
       [ 0.31949142,  0.06629116],
       [ 0.22397829,  0.03692631],
       [ 0.41500455, -0.46227625],
       [ 0.51051768,  1.74008799],
       [ 1.46564897, -1.04957339],
       [ 0.89257019, -0.57973568],
       [ 0.41500455,  0.27184516],
       [ 0.41500455,  1.00596657],
       [ 2.03872775, -1.19639767],
       [ 1.94321462, -0.66783025],
       [ 0.79705706,  0.53612887],
       [ 0.03295203,  0.03692631],
       [ 1.5611621 , -1.28449224],
       [ 2.13424088, -0.69719511],
       [ 2.13424088,  0.38930459],
       [ 0.12846516,  0.09565602],
       [ 2.03872775,  1.76945285],
       [-0.0625611 ,  0.30121002],
       [ 0.79705706, -1.1083031 ],
       [ 0.79705706,  0.12502088],
       [ 0.41500455, -0.49164111],
       [ 0.31949142,  0.50676401],
       [ 1.94321462, -1.37258681],
       [ 0.41500455, -0.16862769],
       [ 0.98808332, -1.07893824],
       [ 0.60603081,  2.03373655],
       [ 1.08359645, -1.22576253],
       [ 1.84770149, -1.07893824],
       [ 1.75218836, -0.28608712],
       [ 1.08359645, -0.9027491 ],
       [ 0.12846516,  0.03692631],
       [ 0.89257019, -1.04957339],
       [ 0.98808332, -1.02020853],
       [ 0.98808332, -1.07893824],
       [ 0.89257019, -1.37258681],
       [ 0.70154394, -0.72655996],
       [ 2.13424088, -0.81465453],
       [ 0.12846516, -0.31545197],
       [ 0.79705706, -0.84401939],
       [ 1.27462271, -1.37258681],
       [ 1.17910958, -1.46068138],
       [-0.15807423, -1.07893824],
       [ 1.08359645, -0.99084367]])
In [27]:
new_df=pd.DataFrame(scale_arr,columns=['Age','EstimatedSalary'])
In [28]:
new_df.head()
Out[28]:
AgeEstimatedSalary
0-1.781797-1.490046
1-0.253587-1.460681
2-1.113206-0.785290
3-1.017692-0.374182
4-1.7817970.183751
In [29]:
from sklearn.model_selection import train_test_split
In [31]:
x_train,x_test,y_train,y_test=train_test_split(new_df,df['Purchased'],test_size=0.30,random_state=101)
In [32]:
from sklearn.svm import SVC
In [33]:
model=SVC()
In [34]:
model.fit(x_train,y_train)
C:\Users\AbhishekSingh\Anaconda3\lib\site-packages\sklearn\svm\base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)
Out[34]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
    kernel='rbf', max_iter=-1, probability=False, random_state=None,
    shrinking=True, tol=0.001, verbose=False)
In [35]:
y_pred=model.predict(x_test)
In [36]:
y_pred
Out[36]:
array([0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
       1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
       0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1,
       1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
       1, 1, 0, 0, 1, 0, 0, 0, 0, 1], dtype=int64)
In [37]:
from sklearn.metrics import confusion_matrix
In [38]:
confusion_matrix(y_pred,y_test)
Out[38]:
array([[73,  1],
       [ 7, 39]], dtype=int64)
In [39]:
from sklearn import metrics
In [40]:
metrics.accuracy_score(y_test,y_pred)
Out[40]:
0.9333333333333333
In [ ]:
 

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...