# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
"""
    KNN from scratch
    
"""

from sklearn import datasets
from sklearn.cross_validation import train_test_split
# from sklearn.model_selection import train_test_split
# -

data = datasets.load_iris()

X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)


# Input : list
def euclideanDistance(l1, l2):
    if (len(l1) == len(l2)):
        dist = 0
        for i in range(len(l1)):
            dist += (l1[i] - l2[i])**2
        return dist**0.5
    else:
        raise ValueError('Hehe, panjangnya beda gan')


# +
# Calculate Neighbors Distance
k = 5               # number ok k
y_test_predict = [] # target yang diprediksi

for x_unknown in X_test:
    neighbors_distance = []
    
    for x_known in X_train:
        neighbors_distance.append(euclideanDistance(x_unknown, x_known))
        
    neighbors_distance_sorted_with_target = sorted(zip(neighbors_distance, y_train))
    
    k_nearest_neighbors_target = [items[1] for items in neighbors_distance_sorted_with_target[:k]]

    # mode of knn : classification
    y_test_predict.append(max(set(k_nearest_neighbors_target), key=k_nearest_neighbors_target.count))

print (y_test_predict)
# -

n_benar = 0
for i, prediksi in enumerate(y_test_predict):
    if (prediksi == y_test[i]):
        n_benar += 1
print(n_benar/len(y_test))

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
knn.score(X_test,y_test)

# +
# REGRESSION
# -

data = datasets.load_boston()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)

# +
from sklearn.metrics import mean_squared_error

# Calculate Neighbors Distance
k = 5               # number ok k
y_test_predict = [] # target yang diprediksi

for x_unknown in X_test:
    neighbors_distance = []
    
    for x_known in X_train:
        neighbors_distance.append(euclideanDistance(x_unknown, x_known))
        
    neighbors_distance_sorted_with_target = sorted(zip(neighbors_distance, y_train))
    
    k_nearest_neighbors_target = [items[1] for items in neighbors_distance_sorted_with_target[:k]]

    # bedanyan hanya di sini
    # mean of knn : regression
    y_test_predict.append(round(sum(k_nearest_neighbors_target)/k, 2))

# print (y_test_predict)
print (mean_squared_error(y_test_predict, y_test))
# -

from sklearn.neighbors import KNeighborsRegressor

knn_reg = KNeighborsRegressor()
knn_reg.fit(X_train, y_train)
y_predict = knn_reg.predict(X_test)
mean_squared_error(y_predict, y_test)



StarcoderdataJupyterScriptsDedupFiltered