#Day093 - k-fold cross validation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('Social_Network_Ads_2.csv')

df.shape
df.columns.to_list()

features = df.iloc[:, [2, 3]].values
labels = df.iloc[:, 4].values

#Train Testv Split
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = \
train_test_split(features, labels, test_size = 0.3, random_state = 0)

#Feature Scaling
from sklearn.preprocessing import StandardScaler

sc = StandardScaler() #feature scaling - age and salary are very low and high

features_train = sc.fit_transform(features_train)
features_test = sc.transform(features_test)

#KNN
from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors = 5, p = 2) #Euclidean distance
knn.fit(features_train, labels_train)

#Prediction
prediction = knn.predict(features_test)
len(prediction)
print(list(zip(prediction, labels_test)))

#Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(labels_test, prediction)
print(cm)

score = (cm[0][0]+cm[1][1])/ cm.sum()
print(score)

#k-fold Cross Validation
from sklearn.model_selection import cross_val_score

k_fold_scores = cross_val_score(estimator = knn, X = features_train, y = labels_train, cv = 10)

print ("Accuracies are ", k_fold_scores)
print ("Mean accuracy is",k_fold_scores.mean())
print ("standard deviation for the accuracies is",k_fold_scores.std())

features_train.shape
features_test.shape

"""
Our test or training data is randomly spread:
traininig data = 280   (features_train)
testing data   = 120   (features_test)
If we scatter these points on 2D, they would be far from each other.

Its difficult to draw the decission boundary which states whether it falls
in state 1 or state 2.

Had these points scattered as a dense network, then it would become easy to 
draw the decision boundary.

How to generate the dense data set, since we dont have that much amount of data.

To do that we first need to have the minimum and maximum values in x and y axis.
"""

#Visualising the Training set results
#Plot the decision boundary. For that, we will assign a color to each.


#Step 1 is to find the minimum and maximum of x and y 
#0th column is the x values
x_min = features_train[:, 0].min() - 1
x_max = features_train[:, 0].max() + 1
print(x_min)
print(x_max)

#1st column is the y values
y_min = features_train[:, 1].min() - 1
y_max = features_train[:, 1].max() + 1
print(y_min)
print(y_max)

#Minimum values are in negative and maximum values are positive

#Step 2
#We want to generate more points between these range only
#We need to give minimum and maximum and the difference 
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

print(xx.shape)
print(yy.shape)

#x set of values are in xx
#y set of values are in yy

#If v open and check, there is a pattern, x axis values are changing since y = 0
print(xx)

#If v open and check, there is a pattern, y axis values are changing since x = 0
print(yy)

#We need to take x values from first row of xx and y values from the first row of yy 
#These two will make the points.
#We want to predict for each points generated for drawing the decission boundary.
#For prediction method we need to pass them as one set
#We need to flatten all the data of xx, that will come in one column
#2D to 1D
xt = xx.ravel()
print(xt.shape)   #60x62 = 3720
print(xt)

#Then we need to flatten all the data of yy, that will come in one column
#2D to 1D
yt = yy.ravel()
print(yt.shape)   #60x62 = 3720
print(yt)


#Need to make them as points now, similar to zip
pt = np.c_[xt,yt]   #similar to pt = zip(xt,yt) or np.concatenate
print(pt.shape)
print(pt)

#To draw the decission boundary need to get the prediction whether it belongs to which category
#xx, yy and Z should have the same shape, so we have to reshape it
Z = knn.predict(pt)
print(Z.shape)

#We have to reshape Z same as xx and yy 
Z = Z.reshape(xx.shape)
print(Z.shape)
#Z stores the prediction wiht only 2 class 


#Now we need to plot xx , yy and Z, all 3 combination 
#Its a 3D data, but we plot only 2D data in matplotlib
#How to show 3D data into 2D ?
#We can use 2 colors and show one set of xx and yy in GREEN which has Z = 0 and in RED which has Z = 1
#Such things are known as contours
#We are trying to plot a 3D data now, since we have xx, yy and Z also.
#How to show 3D data into 2D form
#So we can use the color to represent the Z value

#Open contour_plot.png

"""
A contour plot is a graph that you can use to explore the potential relationship 
between three variables. Contour plots display the 3-dimensional relationship in 
two dimensions, with x-factors and y-factors (predictors) plotted on the x-scales 
and y-scales and response values represented by contours.
""" 
"""
#plot the decission boundary
plt.contourf(xx, yy, Z, alpha=1.0)

#We have to overlays the points
#Plot the points

#class = 0 and x coordinate so 0 and y coordinate so 1
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0, 1], 'ro', label='Class 1')

#class = 1 and x coordinate so 0 and y coordinate so 1
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1, 1], 'bo', label='Class 2')

#We can see from the visual and compare it with cm, that 4 points were different and similarly for 3 points 
plt.show()
"""

cs = plt.contourf(xx, yy, Z, alpha=1.0, extend='both')
cs.cmap.set_over('yellow')
cs.cmap.set_under('cyan')
cs.changed()
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0, 1], 'ro', label='Class 1')
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1, 1], 'bo', label='Class 2')
plt.show()

print(cm)
f323ede71356e5e18c2854b803208db400a48339
<mask token>

<mask token>
df.shape
df.columns.to_list()
<mask token>
knn.fit(features_train, labels_train)
<mask token>
len(prediction)
print(list(zip(prediction, labels_test)))
<mask token>
print(cm)
<mask token>
print(score)
<mask token>
print('Accuracies are ', k_fold_scores)
print('Mean accuracy is', k_fold_scores.mean())
print('standard deviation for the accuracies is', k_fold_scores.std())
features_train.shape
features_test.shape
<mask token>
print(x_min)
print(x_max)
<mask token>
print(y_min)
print(y_max)
<mask token>
print(xx.shape)
print(yy.shape)
print(xx)
print(yy)
<mask token>
print(xt.shape)
print(xt)
<mask token>
print(yt.shape)
print(yt)
<mask token>
print(pt.shape)
print(pt)
<mask token>
print(Z.shape)
<mask token>
print(Z.shape)
<mask token>
cs.cmap.set_over('yellow')
cs.cmap.set_under('cyan')
cs.changed()
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0,
    1], 'ro', label='Class 1')
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1,
    1], 'bo', label='Class 2')
plt.show()
print(cm)

<mask token>
df = pd.read_csv('Social_Network_Ads_2.csv')
df.shape
df.columns.to_list()
features = df.iloc[:, [2, 3]].values
labels = df.iloc[:, 4].values
<mask token>
features_train, features_test, labels_train, labels_test = train_test_split(
    features, labels, test_size=0.3, random_state=0)
<mask token>
sc = StandardScaler()
features_train = sc.fit_transform(features_train)
features_test = sc.transform(features_test)
<mask token>
knn = KNeighborsClassifier(n_neighbors=5, p=2)
knn.fit(features_train, labels_train)
prediction = knn.predict(features_test)
len(prediction)
print(list(zip(prediction, labels_test)))
<mask token>
cm = confusion_matrix(labels_test, prediction)
print(cm)
score = (cm[0][0] + cm[1][1]) / cm.sum()
print(score)
<mask token>
k_fold_scores = cross_val_score(estimator=knn, X=features_train, y=
    labels_train, cv=10)
print('Accuracies are ', k_fold_scores)
print('Mean accuracy is', k_fold_scores.mean())
print('standard deviation for the accuracies is', k_fold_scores.std())
features_train.shape
features_test.shape
<mask token>
x_min = features_train[:, 0].min() - 1
x_max = features_train[:, 0].max() + 1
print(x_min)
print(x_max)
y_min = features_train[:, 1].min() - 1
y_max = features_train[:, 1].max() + 1
print(y_min)
print(y_max)
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)
    )
print(xx.shape)
print(yy.shape)
print(xx)
print(yy)
xt = xx.ravel()
print(xt.shape)
print(xt)
yt = yy.ravel()
print(yt.shape)
print(yt)
pt = np.c_[xt, yt]
print(pt.shape)
print(pt)
Z = knn.predict(pt)
print(Z.shape)
Z = Z.reshape(xx.shape)
print(Z.shape)
<mask token>
cs = plt.contourf(xx, yy, Z, alpha=1.0, extend='both')
cs.cmap.set_over('yellow')
cs.cmap.set_under('cyan')
cs.changed()
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0,
    1], 'ro', label='Class 1')
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1,
    1], 'bo', label='Class 2')
plt.show()
print(cm)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('Social_Network_Ads_2.csv')
df.shape
df.columns.to_list()
features = df.iloc[:, [2, 3]].values
labels = df.iloc[:, 4].values
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = train_test_split(
    features, labels, test_size=0.3, random_state=0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
features_train = sc.fit_transform(features_train)
features_test = sc.transform(features_test)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5, p=2)
knn.fit(features_train, labels_train)
prediction = knn.predict(features_test)
len(prediction)
print(list(zip(prediction, labels_test)))
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(labels_test, prediction)
print(cm)
score = (cm[0][0] + cm[1][1]) / cm.sum()
print(score)
from sklearn.model_selection import cross_val_score
k_fold_scores = cross_val_score(estimator=knn, X=features_train, y=
    labels_train, cv=10)
print('Accuracies are ', k_fold_scores)
print('Mean accuracy is', k_fold_scores.mean())
print('standard deviation for the accuracies is', k_fold_scores.std())
features_train.shape
features_test.shape
<mask token>
x_min = features_train[:, 0].min() - 1
x_max = features_train[:, 0].max() + 1
print(x_min)
print(x_max)
y_min = features_train[:, 1].min() - 1
y_max = features_train[:, 1].max() + 1
print(y_min)
print(y_max)
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)
    )
print(xx.shape)
print(yy.shape)
print(xx)
print(yy)
xt = xx.ravel()
print(xt.shape)
print(xt)
yt = yy.ravel()
print(yt.shape)
print(yt)
pt = np.c_[xt, yt]
print(pt.shape)
print(pt)
Z = knn.predict(pt)
print(Z.shape)
Z = Z.reshape(xx.shape)
print(Z.shape)
<mask token>
cs = plt.contourf(xx, yy, Z, alpha=1.0, extend='both')
cs.cmap.set_over('yellow')
cs.cmap.set_under('cyan')
cs.changed()
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0,
    1], 'ro', label='Class 1')
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1,
    1], 'bo', label='Class 2')
plt.show()
print(cm)

#Day093 - k-fold cross validation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('Social_Network_Ads_2.csv')

df.shape
df.columns.to_list()

features = df.iloc[:, [2, 3]].values
labels = df.iloc[:, 4].values

#Train Testv Split
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = \
train_test_split(features, labels, test_size = 0.3, random_state = 0)

#Feature Scaling
from sklearn.preprocessing import StandardScaler

sc = StandardScaler() #feature scaling - age and salary are very low and high

features_train = sc.fit_transform(features_train)
features_test = sc.transform(features_test)

#KNN
from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors = 5, p = 2) #Euclidean distance
knn.fit(features_train, labels_train)

#Prediction
prediction = knn.predict(features_test)
len(prediction)
print(list(zip(prediction, labels_test)))

#Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(labels_test, prediction)
print(cm)

score = (cm[0][0]+cm[1][1])/ cm.sum()
print(score)

#k-fold Cross Validation
from sklearn.model_selection import cross_val_score

k_fold_scores = cross_val_score(estimator = knn, X = features_train, y = labels_train, cv = 10)

print ("Accuracies are ", k_fold_scores)
print ("Mean accuracy is",k_fold_scores.mean())
print ("standard deviation for the accuracies is",k_fold_scores.std())

features_train.shape
features_test.shape

"""
Our test or training data is randomly spread:
traininig data = 280   (features_train)
testing data   = 120   (features_test)
If we scatter these points on 2D, they would be far from each other.

Its difficult to draw the decission boundary which states whether it falls
in state 1 or state 2.

Had these points scattered as a dense network, then it would become easy to 
draw the decision boundary.

How to generate the dense data set, since we dont have that much amount of data.

To do that we first need to have the minimum and maximum values in x and y axis.
"""

#Visualising the Training set results
#Plot the decision boundary. For that, we will assign a color to each.


#Step 1 is to find the minimum and maximum of x and y 
#0th column is the x values
x_min = features_train[:, 0].min() - 1
x_max = features_train[:, 0].max() + 1
print(x_min)
print(x_max)

#1st column is the y values
y_min = features_train[:, 1].min() - 1
y_max = features_train[:, 1].max() + 1
print(y_min)
print(y_max)

#Minimum values are in negative and maximum values are positive

#Step 2
#We want to generate more points between these range only
#We need to give minimum and maximum and the difference 
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

print(xx.shape)
print(yy.shape)

#x set of values are in xx
#y set of values are in yy

#If v open and check, there is a pattern, x axis values are changing since y = 0
print(xx)

#If v open and check, there is a pattern, y axis values are changing since x = 0
print(yy)

#We need to take x values from first row of xx and y values from the first row of yy 
#These two will make the points.
#We want to predict for each points generated for drawing the decission boundary.
#For prediction method we need to pass them as one set
#We need to flatten all the data of xx, that will come in one column
#2D to 1D
xt = xx.ravel()
print(xt.shape)   #60x62 = 3720
print(xt)

#Then we need to flatten all the data of yy, that will come in one column
#2D to 1D
yt = yy.ravel()
print(yt.shape)   #60x62 = 3720
print(yt)


#Need to make them as points now, similar to zip
pt = np.c_[xt,yt]   #similar to pt = zip(xt,yt) or np.concatenate
print(pt.shape)
print(pt)

#To draw the decission boundary need to get the prediction whether it belongs to which category
#xx, yy and Z should have the same shape, so we have to reshape it
Z = knn.predict(pt)
print(Z.shape)

#We have to reshape Z same as xx and yy 
Z = Z.reshape(xx.shape)
print(Z.shape)
#Z stores the prediction wiht only 2 class 


#Now we need to plot xx , yy and Z, all 3 combination 
#Its a 3D data, but we plot only 2D data in matplotlib
#How to show 3D data into 2D ?
#We can use 2 colors and show one set of xx and yy in GREEN which has Z = 0 and in RED which has Z = 1
#Such things are known as contours
#We are trying to plot a 3D data now, since we have xx, yy and Z also.
#How to show 3D data into 2D form
#So we can use the color to represent the Z value

#Open contour_plot.png

"""
A contour plot is a graph that you can use to explore the potential relationship 
between three variables. Contour plots display the 3-dimensional relationship in 
two dimensions, with x-factors and y-factors (predictors) plotted on the x-scales 
and y-scales and response values represented by contours.
""" 
"""
#plot the decission boundary
plt.contourf(xx, yy, Z, alpha=1.0)

#We have to overlays the points
#Plot the points

#class = 0 and x coordinate so 0 and y coordinate so 1
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0, 1], 'ro', label='Class 1')

#class = 1 and x coordinate so 0 and y coordinate so 1
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1, 1], 'bo', label='Class 2')

#We can see from the visual and compare it with cm, that 4 points were different and similarly for 3 points 
plt.show()
"""

cs = plt.contourf(xx, yy, Z, alpha=1.0, extend='both')
cs.cmap.set_over('yellow')
cs.cmap.set_under('cyan')
cs.changed()
plt.plot(features_test[labels_test == 0, 0], features_test[labels_test == 0, 1], 'ro', label='Class 1')
plt.plot(features_test[labels_test == 1, 0], features_test[labels_test == 1, 1], 'bo', label='Class 2')
plt.show()

print(cm)