【发布时间】:2019-12-08 10:41:09
【问题描述】:
我只能使用 numpy 导入。 我需要计算最近的距离是测试集到训练集。即在测试中找到最近的距离(找到训练数组中所有列表之间的距离)并返回测试名称和训练名称。使用以下公式:
dist(x,y)=√((a-a2 )^2+(b-b2 )^2+(c-c2 )^2+(d-d2)^2 )
link 到使用的数据并期望第一行。
这是我为训练测试集中的第一行正确运行的代码。我需要训练数组的每一行在变量 q 中进行相同的操作。 以下是我的意见
Training
a b c d name training
5 3 1.6 0.2 G
5 3.4 1.6 0.4 G
5.5 2.4 3.7 1 R
5.8 2.7 3.9 1.2 R
7.2 3.2 6 1.8 Y
6.2 2.8 4.8 1.8 Y
testing
a2 b2 c2 d2 name true
5 3.6 1.4 0.2 E
5.4 3.9 1.7 0.4 G
6.9 3.1 4.9 1.5 R
5.5 2.3 4 1.3 R
6.4 2.7 5.3 1.9 Y
6.8 3 5.5 2.1 Y
train = np.asarray(train)
test = np.asarray(test)
print('Train shape',train.shape)
print('test shape',test.shape)
train_1 = train[:,0:(train.shape[1])-1].astype(float)
test_1 = test[:,0:(test.shape[1])-1].astype(float)
print('Train '+'\n',train_1)
print('test '+'\`enter code here`n',test_1)
q=min((np.sqrt(np.sum((train_1[0,:]-test_1)**2,axis=1,keepdims=True))))
与整个测试数组相比,我希望从训练行获得最近的距离。使用该公式的第一行火车将产生以下结果。然后我会返回 G,E,因为它们是最接近的 2 行。
【问题讨论】: