4-1 k近邻算法基础

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28306361/article/details/87888348
import numpy as np
import sklearn
import matplotlib.pyplot as plt
raw_data_x = np.random.rand(20)
raw_data_x
array([0.73926217, 0.85530604, 0.57330886, 0.10024075, 0.86413266,
       0.75800531, 0.90690065, 0.41902893, 0.2067514 , 0.31596071,
       0.79943476, 0.31684349, 0.08522602, 0.51840576, 0.83998825,
       0.28529047, 0.56895044, 0.65566675, 0.02615133, 0.92569249])
raw_data_x = raw_data_x.reshape(10,2)
raw_data_x
array([[0.73926217, 0.85530604],
       [0.57330886, 0.10024075],
       [0.86413266, 0.75800531],
       [0.90690065, 0.41902893],
       [0.2067514 , 0.31596071],
       [0.79943476, 0.31684349],
       [0.08522602, 0.51840576],
       [0.83998825, 0.28529047],
       [0.56895044, 0.65566675],
       [0.02615133, 0.92569249]])
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
x_train = np.array(raw_data_y)
x_train = raw_data_x
y_train = np.array(raw_data_y)
x_train
array([[0.73926217, 0.85530604],
       [0.57330886, 0.10024075],
       [0.86413266, 0.75800531],
       [0.90690065, 0.41902893],
       [0.2067514 , 0.31596071],
       [0.79943476, 0.31684349],
       [0.08522602, 0.51840576],
       [0.83998825, 0.28529047],
       [0.56895044, 0.65566675],
       [0.02615133, 0.92569249]])
y_train
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
plt.scatter( x_train[y_train == 0, 0], x_train[y_train == 0,  1], color = 'g' )
plt.scatter( x_train[y_train == 1, 0], x_train[y_train == 1,  1], color = 'r' )
<matplotlib.collections.PathCollection at 0x1c31cf4a9e8>

在这里插入图片描述

x = np.array([0.8, 0.5])
plt.scatter( x_train[y_train == 0, 0], x_train[y_train == 0,  1], color = 'g' )
plt.scatter( x_train[y_train == 1, 0], x_train[y_train == 1,  1], color = 'r' )
plt.scatter(x[0], x[1], color = 'b')
plt.show()

在这里插入图片描述

KNN

from math import sqrt
distance = []
for xtrain in x_train:
    d =sqrt(np.sum((xtrain - x) ** 2))
    distance.append(d)
    
distance
[0.3604600793002089,
 0.45956101424338214,
 0.265856610529157,
 0.1341046688862789,
 0.6211395713879941,
 0.18315737983164557,
 0.7150109186989645,
 0.21840155714132786,
 0.27859655394067245,
 0.8832077063446624]
distance = [ sqrt(np.sum((xtrain - x)**2)) for xtrain in x_train]
distance
[0.3604600793002089,
 0.45956101424338214,
 0.265856610529157,
 0.1341046688862789,
 0.6211395713879941,
 0.18315737983164557,
 0.7150109186989645,
 0.21840155714132786,
 0.27859655394067245,
 0.8832077063446624]
np.argsort(distance)
array([3, 5, 7, 2, 8, 0, 1, 4, 6, 9], dtype=int64)
nearest = np.argsort(distance)
k = 6
topK_y = [y_train[i] for i in nearest[:k]]
topK_y
[0, 1, 1, 0, 1, 0]
from collections import Counter
Counter(topK_y)
Counter({0: 3, 1: 3})
votes = Counter(topK_y)
votes.most_common(2)
[(0, 3), (1, 3)]
votes.most_common(1)
[(0, 3)]
votes.most_common(1)[0][0]
0
predict_y = votes.most_common(1)[0][0]
predict_y
0

猜你喜欢

转载自blog.csdn.net/qq_28306361/article/details/87888348