s20021024 2022-11-09 10:19 采纳率: 0%

# coding:UTF-8

import matplotlib.pyplot as plt
from collections import Counter
from math import sqrt
import numpy as np
raw_data_X=[[3.85, 3.05],
[1.57, 2.63],
[4.28, 4.42],
[5.68,3.64],
[1.94,3.68],
[2.49,2.66],
[0.49,3.66],
[0.12,1.12],
[2.25,2.04],
[0.23,0.54],
[1.33,1.49],
[2.35,0.09],
[1.46,1.63],
[3.66,3.93],
[5.11,0.39],
[5.69,1.14],
[4.03,2.49],
[4.92,1.62],
[5.26,2.05],
[4.26,2.05],
[5.84,1.31],
]
raw_data_Y=[0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2]
x_train= np.array(raw_data_X)
y_train= np.array(raw_data_Y)
x_test=np.array([3.35,2.46])
def train(X,y):
Xtr=X
Ytr=y
return Xtr,Ytr
def predict_math(X,xtrain,ytrain):
distances=[sqrt(np.sum((X_train-X)*2))for X_train in xtrain]
nearest=np.argsort(distances)
k=3
topK_y=[ytrain[i]for i in nearest[:k]]
k=3
topK_X=nearest[:k]
for i in range(3):
plt.plot([X[0],xtrain[topK_X[i]][0]],[X[1],xtrain[topK_X[i]][1]])
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.scatter(x_train[y_train==0,0],x_train[y_train==0,1],marker="
")
plt.scatter(x_train[y_train==1,0],x_train[y_train==1,1], marker="^")
plt.scatter(x_train[y_train==2,0],x_train[y_train==2,1], marker="s")
plt.scatter(x_test[0],x_test[1],marker="o")
plt.show()
xtrain,ytrain=train(x_train,y_train)
predict_math(x_test,xtrain,ytrain)

• 写回答

• 创建了问题 11月9日

#### 悬赏问题

• ¥15 粘贴替换字符串的时候，右边引号会自动换行导致报错
• ¥15 用verilog HDL语法仿真
• ¥15 用超表面产生涡旋光束，怎么用matlab代码算得到的涡旋光束的模式纯度
• ¥40 返乡没拿电脑航班取消被困在机场了，C语言实验ddl要到了
• ¥15 find 命令优化语句问题
• ¥15 js 使用contenteditable属性模拟富文本框 实现具体关键字高亮
• ¥15 QT QList<QLIst<int>> 遍历问题
• ¥15 关于#C++#2048游戏问题
• ¥15 这种html怎么制作
• ¥60 高仿微信的即时通讯app