Case Study (T-Shirt Size) :

SKLearrn (K-Nearest Neighbors (K-NN))

You own an online clothing business and you would like to develop a new app (or in-store) feature in which customers would enter their own height and weight and the system would predict what T-shirt size should they wear. Features are height and weight and output is either L (Large) or S (Small). 



Download DataSet

Dr. Ryan @STEMplicity

Importing the Relevant Libraries

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

Importing the Dataset

In [2]:
url = "http://datascienceschools.github.io/Machine_Learning/Classification_Models_CaseStudies/Tshirt_Sizing_Dataset.csv"

df = pd.read_csv(url)

df.head()
Out[2]:
Height (in cms) Weight (in kgs) T Shirt Size
0 158 58 S
1 158 59 S
2 158 63 S
3 160 59 S
4 160 60 S

Declaring the Dependent & the Independent Variables

In [3]:
X = df.iloc[:, :-1].values

y = df.iloc[:, -1].values

Label Encoding the Dependent Variable

In [4]:
from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()

y = le.fit_transform(y)

Splitting the Dataset into the Training Set and Test Set

In [5]:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

Feature Scaling

In [6]:
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

X_train = sc.fit_transform(X_train)

X_test = sc.transform(X_test)

Training the K-Nearest Neighbors Model

In [7]:
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)

model.fit(X_train, y_train)
Out[7]:
KNeighborsClassifier()

Predicting the Test Set Results

In [8]:
y_pred = model.predict(X_test)

Confusion Matrix

In [9]:
from sklearn.metrics import confusion_matrix, accuracy_score

cm = confusion_matrix(y_test, y_pred)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy is: {:.2f} %".format(accuracy*100))

sns.heatmap(cm, annot=True, fmt="d")

plt.show()
Accuracy is: 100.00 %

Classification Report

In [10]:
from sklearn.metrics import classification_report

print(classification_report(y_test,y_pred))
              precision    recall  f1-score   support

           0       1.00      1.00      1.00         3
           1       1.00      1.00      1.00         2

    accuracy                           1.00         5
   macro avg       1.00      1.00      1.00         5
weighted avg       1.00      1.00      1.00         5

k-Fold Cross Validation

In [11]:
from sklearn.model_selection import cross_val_score

accuracies = cross_val_score(estimator = model, X = X_train, y = y_train, cv = 5)

print("Accuracy: {:.2f} %".format(accuracies.mean()*100))

print("Standard Deviation: {:.2f} %".format(accuracies.std()*100))
Accuracy: 86.67 %
Standard Deviation: 26.67 %

Visualising the Training Set Results

In [12]:
from matplotlib.colors import ListedColormap

X_grid, y_grid = X_train, y_train

X1, X2 = np.meshgrid(np.arange(start = X_grid[:, 0].min() - 1, stop = X_grid[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_grid[:, 1].min() - 1, stop = X_grid[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('magenta', 'blue')))

plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_grid)):
    plt.scatter(X_grid[y_grid == j, 0], X_grid[y_grid == j, 1],
                color = ListedColormap(('magenta', 'blue'))(i), label = j)
    
plt.title('Training Dataset')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.legend()
plt.show()

Visualising the Test Set Results

In [13]:
from matplotlib.colors import ListedColormap

X_grid, y_grid = X_test, y_test

X1, X2 = np.meshgrid(np.arange(start = X_grid[:, 0].min() - 1, stop = X_grid[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_grid[:, 1].min() - 1, stop = X_grid[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('magenta', 'blue')))

plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(y_grid)):
    plt.scatter(X_grid[y_grid == j, 0], X_grid[y_grid == j, 1], color = ListedColormap(('magenta', 'blue'))(i), label = j)
    
plt.title('Testing Dataset')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.legend()
plt.show()