K-NeighbourClassifier
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# In[2]:
#mydatafile="Desktop\Machine_Learning\week3_Social_Network_Ads.csv"
mydatafile = r"C:\Users\91887\OneDrive\Desktop\Machine_Learning\week3_Social_Network_Ads.csv"
dataset = pd.read_csv(mydatafile)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
# In[3]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)
# In[4]:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# In[6]:
print(X_test)
# In[7]:
print(X_train)
# In[8]:
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
classifier.fit(X_train, y_train)
# In[13]:
#print(classifier.predict(sc.transform([[30,87000]])))
y_pred = classifier.predict(X_test)
print(y_pred)
# In[14]:
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
# In[ ]:
Comments
Post a Comment