<jupyter_start><jupyter_text>2. Write a python program to build a K-Nearest Neighbour classifier using Scikit learn and predict the class of a unknown sample<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(x,y)
knn.predict([[5, 3, 1, 0],[6,3,5,2]])
knn5 = KNeighborsClassifier(n_neighbors=5)
x_new = [[5, 3, 1, 0],[6,3,5,2]]
knn5.fit(x,y)
knn5.predict(x_new)<jupyter_output><empty_output><jupyter_text>3. Write a Python program to create separate ndarrays for features and targets from data using Scikit learn library. Use train_test_split function to split the dataset into training and testing<jupyter_code>#from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
help(train_test_split)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.4,random_state = 4)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train,y_train)<jupyter_output><empty_output><jupyter_text>4. Write a python program to build a K-Nearest Neighbour classifier using Scikit learn and test it using the test dataset. Find the accuracy using accuracy_score() function<jupyter_code>from sklearn import metrics
type(metrics)
y_pred = knn.predict(x_test)
metrics.accuracy_score(y_test,y_pred)

metrics.confusion_matrix(y_test,y_pred)<jupyter_output><empty_output><jupyter_text>5. Write a python program to build a K-Nearest Neighbour classifier using Scikit learn and find the optimal value of k by plotting the accuracies for different values of k using matplotlib library<jupyter_code>k_range = range(1,30)
score = []
for k in k_range:
     knn = KNeighborsClassifier(n_neighbors=k)
     knn.fit(x_train,y_train)
     y_pred = knn.predict(x_test)
     score.append(metrics.accuracy_score(y_test,y_pred))
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(k_range,score)
plt.xlabel('Values of k')
plt.ylabel('Accuracy')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09021406727828746, 'fraction_numerical': 0.0163098878695209, 'mean_word_length': 7.353191489361702, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '511908', 'license': 'Apache-2.0', 'n_tokens_mistral': 735, 'n_tokens_neox': 704, 'n_words': 173, 'path': '1 Data-Sciecne-Basics/iris_dataset.ipynb', 'repo_name': 'soumyadip007/Data-Science-Using-Python-University-Course-Module', 'size': '1962'}