<jupyter_start><jupyter_text>Classification: K-Nearest Neighbour<jupyter_code>import pandas as pd
df = pd.read_csv("./data/iris-with-header.tsv", sep='\t')
df.head()<jupyter_output><empty_output><jupyter_text>Put feature matrix into X, label column into y<jupyter_code>X = df.iloc[:, 0:-1].values #X = features
y = df.iloc[:, -1].values #y = label<jupyter_output><empty_output><jupyter_text>Split data into train, test set<jupyter_code>from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=8)<jupyter_output><empty_output><jupyter_text>Data Scaling 1 of 2: Standardization<jupyter_code>from sklearn.preprocessing import StandardScaler
stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(X_train)
X_test_std = stdsc.transform(X_test)<jupyter_output><empty_output><jupyter_text>Data Scaling 2 of 2: Normalise to Range of Min-Mx<jupyter_code>from sklearn.preprocessing import MinMaxScaler
mmsc = MinMaxScaler()
X_train_mm = mmsc.fit_transform(X_train)
X_test_mm = mmsc.transform(X_test)<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbour 1 of 3 with as-is data<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5, algorithm='brute', metric='minkowski', p=2, weights='uniform')
iris_knn = knn.fit(X_train, y_train) # iris_knn is the model/classifier
iris_knn.score(X_test, y_test)
X1 = [[5.1,3.5,1.4,0.2]] # [] is list, [[]] is array
iris_knn.predict(X1)
X2 = [5.1,3.5,1.4,0.2] # [] is list, [[]] is array
iris_knn.predict(X2)
iris_knn.predict(X_test)<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbour 2 of 3 with standardised data<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5, algorithm='brute', metric='minkowski', p=2, weights='uniform')
iris_knn_std = knn.fit(X_train_std, y_train)
iris_knn_std.score(X_test_std, y_test)<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbour 3 of 3 with normalised data<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5, algorithm='brute', metric='minkowski', p=2, weights='uniform')
iris_knn_mm = knn.fit(X_train_mm, y_train)
iris_knn_mm.score(X_test_mm, y_test)<jupyter_output><empty_output><jupyter_text>Reference:<jupyter_code>help(KNeighborsClassifier)<jupyter_output>Help on class KNeighborsClassifier in module sklearn.neighbors.classification:

class KNeighborsClassifier(sklearn.neighbors.base.NeighborsBase, sklearn.neighbors.base.KNeighborsMixin, sklearn.neighbors.base.SupervisedIntegerMixin, sklearn.base.ClassifierMixin)
 |  Classifier implementing the k-nearest neighbors vote.
 |  
 |  Read more in the :ref:`User Guide <classification>`.
 |  
 |  Parameters
 |  ----------
 |  n_neighbors : int, optional (default = 5)
 |      Number of neighbors to use by default for :meth:`k_neighbors` queries.
 |  
 |  weights : str or callable
 |      weight function used in prediction.  Possible values:
 |  
 |      - 'uniform' : uniform weights.  All points in each neighborhood
 |        are weighted equally.
 |      - 'distance' : weight points by the inverse of their distance.
 |        in this case, closer neighbors of a query point will have a
 |        greater influence than neighbors which are further away.
 |      - [callable] : a user-defined functi[...]
{'fraction_non_alphanumeric': 0.11421021726365238, 'fraction_numerical': 0.01262477980035232, 'mean_word_length': 6.1425576519916145, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '236849', 'license': 'MIT', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1257, 'n_words': 252, 'path': 'classification-k-nn.ipynb', 'repo_name': 'ryubidragonfire/python-machine-learning-101', 'size': '3406'}