<jupyter_start><jupyter_text>K-Nearest Neighbors Algorithm  K-Nearest Neighbors (KNN) is  lazy learning algorithm and classification algorithms. In addition, KNN is the simplest methods in machine learning. KNN is used for to separated into classes to predict the classification for new point.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")

# fix_yahoo_finance is used to fetch data 
import fix_yahoo_finance as yf
yf.pdr_override()
# input
symbol = 'AMD'
start = '2014-01-01'
end = '2018-08-27'

# Read data 
dataset = yf.download(symbol,start,end)

# Only keep close columns 
dataset.head()
dataset['Up_Down'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'], 1, -1)
dataset.head()<jupyter_output><empty_output><jupyter_text>Step 1: Preprocessing<jupyter_code>X = dataset.iloc[ : , 1:4].values
y = dataset.iloc[ : , 6].values<jupyter_output><empty_output><jupyter_text>Step 2: Train Test Split<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.20)  
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)<jupyter_output>(937, 3)
(937,)
(235, 3)
(235,)<jupyter_text>Step 3: Feature Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler  
scaler = StandardScaler()  
scaler.fit(X_train)

X_train = scaler.transform(X_train)  
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Step 4: Training and Predictions<jupyter_code>from sklearn.neighbors import KNeighborsClassifier  
knn = KNeighborsClassifier(n_neighbors=5)  
knn.fit(X_train, y_train)  
y_pred = knn.predict(X_test)<jupyter_output><empty_output><jupyter_text>Step 5: Evaluating the Algorithm<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix  
print(confusion_matrix(y_test, y_pred))  
print(classification_report(y_test, y_pred))
error = []

# Calculating error for K values between 1 and 40
for i in range(1, 40):  
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train, y_train)
    pred_i = knn.predict(X_test)
    error.append(np.mean(pred_i != y_test))
plt.figure(figsize=(12, 6))  
plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o',  
         markerfacecolor='blue', markersize=10)
plt.title('Error Rate K Value')  
plt.xlabel('K Value')  
plt.ylabel('Mean Error')  
from sklearn.metrics import accuracy_score

for K in range(40):
    K_value = K+1
    knn = KNeighborsClassifier(n_neighbors = K_value, weights='uniform', algorithm='auto')
    knn.fit(X_train, y_train) 
    y_pred = knn.predict(X_test)
    print("Accuracy is ", accuracy_score(y_test,y_pred)*100,"% for K-Value:",K_value)<jupyter_output>Accuracy is  47.23404255319149 % for K-Value: 1
Accuracy is  48.51063829787234 % for K-Value: 2
Accuracy is  50.638297872340424 % for K-Value: 3
Accuracy is  49.361702127659576 % for K-Value: 4
Accuracy is  49.787234042553195 % for K-Value: 5
Accuracy is  52.34042553191489 % for K-Value: 6
Accuracy is  48.93617021276596 % for K-Value: 7
Accuracy is  49.787234042553195 % for K-Value: 8
Accuracy is  48.08510638297872 % for K-Value: 9
Accuracy is  48.93617021276596 % for K-Value: 10
Accuracy is  49.361702127659576 % for K-Value: 11
Accuracy is  51.48936170212765 % for K-Value: 12
Accuracy is  51.48936170212765 % for K-Value: 13
Accuracy is  47.23404255319149 % for K-Value: 14
Accuracy is  46.808510638297875 % for K-Value: 15
Accuracy is  48.08510638297872 % for K-Value: 16
Accuracy is  48.51063829787234 % for K-Value: 17
Accuracy is  47.65957446808511 % for K-Value: 18
Accuracy is  48.51063829787234 % for K-Value: 19
Accuracy is  45.95744680851064 % for K-Value: 20
Accuracy is  46.8085106[...]
{'fraction_non_alphanumeric': 0.10871848739495799, 'fraction_numerical': 0.1131827731092437, 'mean_word_length': 5.348333333333334, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '152355', 'license': 'MIT', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1436, 'n_words': 342, 'path': 'Stock_Algorithms/K_Nearest_Neighbors.ipynb', 'repo_name': 'sonukumarraj007/Deep-Learning-Machine-Learning-Stock', 'size': '3808'}