Saturday, December 28, 2019

Machine Learning day 7

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
#regressor
In [3]:
from sklearn.ensemble import RandomForestRegressor
from sklearn import datasets,metrics
from sklearn.model_selection import train_test_split
In [4]:
diabetes=datasets.load_diabetes()
In [5]:
print(diabetes.DESCR)
.. _diabetes_dataset:

Diabetes dataset
----------------

Ten baseline variables, age, sex, body mass index, average blood
pressure, and six blood serum measurements were obtained for each of n =
442 diabetes patients, as well as the response of interest, a
quantitative measure of disease progression one year after baseline.

**Data Set Characteristics:**

  :Number of Instances: 442

  :Number of Attributes: First 10 columns are numeric predictive values

  :Target: Column 11 is a quantitative measure of disease progression one year after baseline

  :Attribute Information:
      - Age
      - Sex
      - Body mass index
      - Average blood pressure
      - S1
      - S2
      - S3
      - S4
      - S5
      - S6

Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).

Source URL:
https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html

For more information see:
Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) "Least Angle Regression," Annals of Statistics (with discussion), 407-499.
(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)
In [6]:
x=diabetes.data
y=diabetes.target
In [7]:
df=pd.DataFrame(x,columns=diabetes.feature_names)
df['target']=y
df.head()
Out[7]:
agesexbmibps1s2s3s4s5s6target
00.0380760.0506800.0616960.021872-0.044223-0.034821-0.043401-0.0025920.019908-0.017646151.0
1-0.001882-0.044642-0.051474-0.026328-0.008449-0.0191630.074412-0.039493-0.068330-0.09220475.0
20.0852990.0506800.044451-0.005671-0.045599-0.034194-0.032356-0.0025920.002864-0.025930141.0
3-0.089063-0.044642-0.011595-0.0366560.0121910.024991-0.0360380.0343090.022692-0.009362206.0
40.005383-0.044642-0.0363850.0218720.0039350.0155960.008142-0.002592-0.031991-0.046641135.0
In [8]:
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=101)
In [9]:
regressor=RandomForestRegressor(random_state=101)
In [10]:
regressor.fit(x_train,y_train)
C:\Users\AbhishekSingh\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
Out[10]:
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
                      max_features='auto', max_leaf_nodes=None,
                      min_impurity_decrease=0.0, min_impurity_split=None,
                      min_samples_leaf=1, min_samples_split=2,
                      min_weight_fraction_leaf=0.0, n_estimators=10,
                      n_jobs=None, oob_score=False, random_state=101, verbose=0,
                      warm_start=False)
In [11]:
y_prediction=regressor.predict(x_test)
In [12]:
y_prediction
Out[12]:
array([ 74.8,  86.9, 224.5, 117.4, 201.2, 172.7, 245.7, 160.6, 174.6,
       123.6,  73.9, 138.8,  71.7, 222.9, 122.8, 244.7,  98.8, 164.6,
       290.3,  85.1, 182.7, 102.1, 248.1,  82.4, 119.1, 142.3, 180.7,
       150.9, 178.9, 117. , 131.1,  70.8, 164.7, 129. ,  88. , 251.5,
        84.7, 184.8,  74.4, 161.7, 104.5,  88.1, 119.6, 290.6, 267.4,
        75.7, 103.1, 176.6, 150.5, 196.9, 117.9, 139.4, 127.3, 122.9,
        95.9, 169.1,  89.7, 179.3, 126.1, 159.6,  94.6,  87.6,  70.6,
       293.2, 150.3, 115.7, 105.8, 118.2,  69.3, 220.7, 169.3, 224.5,
       300.8, 207.3, 182.6, 251.5, 204.9, 204.3, 264.4,  94.7, 102.5,
       132.2, 127.4,  79.3,  94.2,  89.5, 190.6, 109.2, 163.3])
In [13]:
new_prediction=regressor.predict([[-0.001882,-0.044642,-0.051474,-0.026328,-0.008449,-0.019163,0.074412,-0.039493,-0.068330,-0.092204]])
In [14]:
new_prediction
Out[14]:
array([63.4])
In [15]:
metrics.mean_squared_error(y_test,y_prediction)
Out[15]:
3523.512359550562
In [16]:
np.sqrt(metrics.mean_squared_error(y_test,y_prediction))
Out[16]:
59.3591809204824
In [17]:
#now we will use decision tree as classifier
In [18]:
from sklearn.ensemble import RandomForestClassifier
In [19]:
iris=datasets.load_iris()
In [20]:
x1=iris.data
y1=iris.target
In [23]:
df2=pd.DataFrame(x1,columns=iris.feature_names)
df2['target']=y1
df2.head()
Out[23]:
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20
In [24]:
x1_train,x1_test,y1_train,y1_test=train_test_split(x1,y1,test_size=0.20,random_state=101)
In [25]:
clf=RandomForestClassifier()
In [26]:
clf.fit(x1_train,y1_train)
C:\Users\AbhishekSingh\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
Out[26]:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
                       max_depth=None, max_features='auto', max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=10,
                       n_jobs=None, oob_score=False, random_state=None,
                       verbose=0, warm_start=False)
In [27]:
y_pred=clf.predict(x1_test)
In [28]:
metrics.accuracy_score(y1_test,y_pred)
Out[28]:
0.9333333333333333
In [29]:
metrics.confusion_matrix(y1_test,y_pred)
Out[29]:
array([[10,  0,  0],
       [ 0, 12,  0],
       [ 0,  2,  6]], dtype=int64)
In [30]:
y_pred
Out[30]:
array([0, 0, 0, 1, 1, 2, 1, 1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 1, 0, 1, 1, 0,
       1, 1, 1, 1, 1, 2, 0, 0])
In [ ]:
 

Machine Learning Day 6

In [1]:
#decision tree can be used as regressor as well as classifier
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
from sklearn import datasets,metrics
from sklearn.model_selection import train_test_split 
from sklearn.tree import DecisionTreeRegressor
In [4]:
diabetes=datasets.load_diabetes()
In [5]:
print(diabetes.DESCR)
.. _diabetes_dataset:

Diabetes dataset
----------------

Ten baseline variables, age, sex, body mass index, average blood
pressure, and six blood serum measurements were obtained for each of n =
442 diabetes patients, as well as the response of interest, a
quantitative measure of disease progression one year after baseline.

**Data Set Characteristics:**

  :Number of Instances: 442

  :Number of Attributes: First 10 columns are numeric predictive values

  :Target: Column 11 is a quantitative measure of disease progression one year after baseline

  :Attribute Information:
      - Age
      - Sex
      - Body mass index
      - Average blood pressure
      - S1
      - S2
      - S3
      - S4
      - S5
      - S6

Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).

Source URL:
https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html

For more information see:
Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) "Least Angle Regression," Annals of Statistics (with discussion), 407-499.
(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)
In [6]:
x=diabetes.data
y=diabetes.target
In [7]:
df=pd.DataFrame(x,columns=diabetes.feature_names)
df['target']=y
df.head()
Out[7]:
agesexbmibps1s2s3s4s5s6target
00.0380760.0506800.0616960.021872-0.044223-0.034821-0.043401-0.0025920.019908-0.017646151.0
1-0.001882-0.044642-0.051474-0.026328-0.008449-0.0191630.074412-0.039493-0.068330-0.09220475.0
20.0852990.0506800.044451-0.005671-0.045599-0.034194-0.032356-0.0025920.002864-0.025930141.0
3-0.089063-0.044642-0.011595-0.0366560.0121910.024991-0.0360380.0343090.022692-0.009362206.0
40.005383-0.044642-0.0363850.0218720.0039350.0155960.008142-0.002592-0.031991-0.046641135.0
In [8]:
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=101)
In [9]:
regressor=DecisionTreeRegressor(random_state=101)
In [10]:
regressor.fit(x_train,y_train)
Out[10]:
DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
                      max_leaf_nodes=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      presort=False, random_state=101, splitter='best')
In [11]:
y_prediction=regressor.predict(x_test)
In [12]:
y_prediction
Out[12]:
array([ 87.,  53., 163., 187., 258., 206., 220.,  88., 109., 158.,  53.,
       198.,  87., 242.,  71., 283., 168., 196., 275.,  53., 265., 141.,
       248., 116.,  68.,  69., 107.,  49., 229., 142.,  83., 181., 142.,
       102.,  93., 128.,  84., 196.,  63., 321.,  66.,  97.,  81., 230.,
       236.,  64., 132., 242.,  91., 233., 146., 190.,  88., 111., 116.,
       151., 200., 198., 147., 172.,  71.,  52.,  49., 297., 198., 135.,
       103.,  90.,  39.,  99., 190., 268., 261., 163., 222., 341., 268.,
       142., 273., 153.,  87.,  71., 150.,  57.,  99.,  53., 221., 135.,
       170.])
In [13]:
new_prediction=regressor.predict([[0.038076,0.050680,0.061696,0.021872,-.044223,-0.034821,-0.043401,-0.002592,0.019908,-0.017646]])
In [14]:
new_prediction
Out[14]:
array([151.])
In [15]:
metrics.mean_squared_error(y_test,y_prediction)
Out[15]:
5124.752808988764
In [17]:
np.sqrt(metrics.mean_squared_error(y_test,y_prediction))
Out[17]:
71.58737883865258
In [18]:
y_test.std()
Out[18]:
79.93591202683353
In [20]:
#now we will use decision tree as classifier
In [21]:
from sklearn.tree import DecisionTreeClassifier
In [22]:
iris=datasets.load_iris()
In [23]:
print(iris.DESCR)
.. _iris_dataset:

Iris plants dataset
--------------------

**Data Set Characteristics:**

    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
                
    :Summary Statistics:

    ============== ==== ==== ======= ===== ====================
                    Min  Max   Mean    SD   Class Correlation
    ============== ==== ==== ======= ===== ====================
    sepal length:   4.3  7.9   5.84   0.83    0.7826
    sepal width:    2.0  4.4   3.05   0.43   -0.4194
    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)
    ============== ==== ==== ======= ===== ====================

    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

.. topic:: References

   - Fisher, R.A. "The use of multiple measurements in taxonomic problems"
     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
     Mathematical Statistics" (John Wiley, NY, 1950).
   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
     Structure and Classification Rule for Recognition in Partially Exposed
     Environments".  IEEE Transactions on Pattern Analysis and Machine
     Intelligence, Vol. PAMI-2, No. 1, 67-71.
   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
     on Information Theory, May 1972, 431-433.
   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
     conceptual clustering system finds 3 classes in the data.
   - Many, many more ...
In [24]:
x=iris.data
y=iris.target
In [25]:
df1=pd.DataFrame(x,columns=iris.feature_names)
df1['target']=y
df1.head()
Out[25]:
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20
In [26]:
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=101)
In [27]:
clf=DecisionTreeClassifier(random_state=101)
In [28]:
clf.fit(x_train,y_train)
Out[28]:
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
                       max_features=None, max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, presort=False,
                       random_state=101, splitter='best')
In [29]:
y_pred=clf.predict(x_test)
In [30]:
metrics.accuracy_score(y_test,y_pred)
Out[30]:
0.9666666666666667
In [31]:
metrics.confusion_matrix(y_test,y_pred)
Out[31]:
array([[10,  0,  0],
       [ 0, 12,  0],
       [ 0,  1,  7]], dtype=int64)
In [32]:
y_pred
Out[32]:
array([0, 0, 0, 1, 1, 2, 1, 1, 2, 0, 2, 0, 0, 2, 2, 1, 1, 1, 0, 2, 1, 0,
       1, 1, 1, 1, 1, 2, 0, 0])
In [ ]:
 

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 [ ]:
 

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...