<jupyter_start><jupyter_text>Applied Machine Learning, Module 1: A simple Classification Task  Import required modules and data file<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

fruits = pd.read_table('fruit_data_with_colors.txt')
fruits.head() #.head() displays first 5 entries<jupyter_output><empty_output><jupyter_text>color_score is a single number which roughly captures the color of the fruit![image.png](attachment:image.png)<jupyter_code>fruits.shape
# Create mapping from fruit label value to fruit name to make results easier to interpret

lookup_fruit_name = dict(zip(fruits.fruit_label.unique(),fruits.fruit_name.unique()))
lookup_fruit_name<jupyter_output><empty_output><jupyter_text>Examining the Data Creating training and test data sets from original data set by partitioning original data set (75% - 25%)<jupyter_code>X = fruits[['height','width','mass','color_score']]
y = fruits['fruit_label']
X
y<jupyter_output><empty_output><jupyter_text>Creating Train and test data sets<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0) #does 75-25 partision (75% to train and 25% to test)
X_train
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>It is always good to analyze and visualize raw data before proceeding onto training it. Sometimes data may be inconsistent and may have missing data. What type of cleaning and pre-processing needs to be done, can be identified<jupyter_code># Plotting a scatter matrix
from matplotlib import cm
from pandas.plotting import scatter_matrix

fig = plt.figure()
cmap = cm.get_cmap('gnuplot')
scatter = scatter_matrix(X_train, c=y_train, marker='o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)
plt.show()
# Plotting a 3D scatter plot
from mpl_toolkits.mplot3d import Axes3D

fig  = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(X_train['width'], X_train['height'], X_train['color_score'], c = y_train, marker = 'o', s=100)
ax.set_xlabel('width')
ax.set_ylabel('height')
ax.set_zlabel('color_score')
plt.show()<jupyter_output><empty_output><jupyter_text>Create a Classifier Object - k-NN (k-Nearest-Neighbour)KNN can be used for both classification and regression; k =  of nearest neighbours; good to use odd number for k; majority vote of class labels is used for calssification<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors = 5)<jupyter_output><empty_output><jupyter_text>Train the Classifier using Training Data<jupyter_code>knn.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Estimate the accuracy of the classfier on future data using training data<jupyter_code>knn.score(X_test,y_test)<jupyter_output><empty_output><jupyter_text>Use the trained k-NN classifier model to classify new data<jupyter_code># Lets create a hypothetical fruit with mass, width and height attributes. 
#Feed it to k-NN classifier and see what fruit it turns out to be

fruit_prediction = knn.predict([[5.5,4.3,20,0.5]])  #[height,width,mass,color_score]
lookup_fruit_name[fruit_prediction[0]]<jupyter_output><empty_output><jupyter_text>Plot decision boundaries of the k-NN classifier<jupyter_code>from adspy_shared_utilities import plot_fruit_knn

plot_fruit_knn(X_train, y_train, 5, 'uniform')  #k=5 i.e. 5 nearest neighbors<jupyter_output><empty_output><jupyter_text>How sensitive is k-NN classification accuracy to choice of 'k' parameter<jupyter_code>k_range = range(1,20)
scores = []

for k in k_range:
    knn = KNeighborsClassifier(n_neighbors = k)
    knn.fit(X_train, y_train)
    scores.append(knn.score(X_test, y_test))
    
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20]);
plt.show()<jupyter_output><empty_output><jupyter_text>How sensitive is k-NN classification accuracy to train/test split proportion<jupyter_code>t = [0.8,0.7,0.6,0.5,0.4,0.3,0.3,0.2]

knn = KNeighborsClassifier(n_neighbors = 5)
plt.figure()

for s in t:
    scores=[]
    for i in range(1,1000):
        X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 1-s) #test_size: proportion of data to be included in TEST set
        knn.fit(X_train,y_train)
        scores.append(knn.score(X_test,y_test))
    
    plt.plot(s,np.mean(scores), 'bo')
    
plt.xlabel('Training set proportion (%)')
plt.ylabel('accuracy')
plt.show()
    
help(train_test_split)
scores<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10836459942617524, 'fraction_numerical': 0.017214742882365925, 'mean_word_length': 6.7206132879046, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '196539', 'license': 'MIT', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1593, 'n_words': 392, 'path': 'Module_1_Simple_Classification.ipynb', 'repo_name': 'rakshithb174/Applied_ML_using_Python', 'size': '4531'}