<jupyter_start><jupyter_text>K - Nearest Neighbors <NAME>  Importing required libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>About dataset Imagine a telecommunications provider has segmented its customer base by service usage patterns, categorizing the customers into four groups. If demographic data can be used to predict group membership, the company can customize offers for individual prospective customers. It is a classification problem. That is, given the dataset,  with predefined labels, we need to build a model to be used to predict class of a new or unknown case. The example focuses on using demographic data, such as region, age, and marital, to predict usage patterns. The target field, called __custcat__, has four possible values that correspond to the four customer groups, as follows:  1- Basic Service  2- E-Service  3- Plus Service  4- Total ServiceOur objective is to build a classifier, to predict the class of unknown cases. We will use a specific type of classification called K nearest neighbour.  Reading the dataset<jupyter_code>dataFrame = pd.read_csv('teleCust.csv')
dataFrame.head()
dataFrame.shape
dataFrame.describe()
dataFrame['custcat'].value_counts()
dataFrame.hist(column='income', bins=50)
dataFrame.columns<jupyter_output><empty_output><jupyter_text>Splitting into features and lables<jupyter_code># Features
X = dataFrame.iloc[:, 0:-1]
X.head()
# Labels
y = dataFrame.iloc[:, -1]
y.head()<jupyter_output><empty_output><jupyter_text>Normalize the Data<jupyter_code>from sklearn import preprocessing

X = preprocessing.StandardScaler().fit_transform(X.astype(float))
print(X)<jupyter_output>[[-0.02696767 -1.055125    0.18450456 ... -0.22207644 -1.03459817
  -0.23065004]
 [ 1.19883553 -1.14880563 -0.69181243 ... -0.22207644 -1.03459817
   2.55666158]
 [ 1.19883553  1.52109247  0.82182601 ... -0.22207644  0.96655883
  -0.23065004]
 ...
 [ 1.19883553  1.47425216  1.37948227 ... -0.22207644  0.96655883
  -0.92747794]
 [ 1.19883553  1.61477311  0.58283046 ... -0.22207644  0.96655883
  -0.92747794]
 [ 1.19883553  0.67796676 -0.45281689 ... -0.22207644  0.96655883
   0.46617787]]<jupyter_text>Split the data into Training and Testing<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
print ('Train set:', X_train.shape,  y_train.shape)
print ('Test set:', X_test.shape,  y_test.shape)<jupyter_output>Test set: (200, 11) (200,)<jupyter_text>Modeling<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors = 4)
knn.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>y_pred = knn.predict(X_test)
y_pred[0:5]<jupyter_output><empty_output><jupyter_text>Accuracy Score<jupyter_code>from sklearn import metrics

print("Test set Accuracy: ", metrics.accuracy_score(y_test, y_pred))<jupyter_output>Test set Accuracy:  0.32<jupyter_text>Modeling with differnet parameters ( Neighbors = 6 )<jupyter_code>knn6 = KNeighborsClassifier(n_neighbors = 6).fit(X_train, y_train)
y_pred_6 = knn6.predict(X_test)
print("Test set Accuracy: ", metrics.accuracy_score(y_test, y_pred_6))<jupyter_output>Test set Accuracy:  0.31<jupyter_text>Calucalting the accuracy of KNN for different K's.<jupyter_code>no_of_neighbors = 10
accuracy = []

for n in range(1, no_of_neighbors):    
    #Train Model and Predict  
    knn = KNeighborsClassifier(n_neighbors = n).fit(X_train,y_train)
    y_pred = knn.predict(X_test)
    accuracy.append(metrics.accuracy_score(y_test, y_pred))
accuracy<jupyter_output><empty_output><jupyter_text>Accuracy Graph<jupyter_code>plt.plot(range(1, no_of_neighbors), accuracy)
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup