【问题标题】:performing mathematical operations for each row of a 2d array against another 2d array对二维数组的每一行对另一个二维数组执行数学运算
【发布时间】: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 行。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    您可以使用numpy.linalg.norm。这是一个例子:

    >>> import numpy as np
    >>> arr = np.array([1, 2, 3, 4])
    >>> np.linalg.norm(arr)
    5.477225575051661
    

    5.477225575051661sqrt(1^2 + 2^2 + 3^2 + 4^2) 的结果

    import numpy as np
    
    train = np.array([[5, 3, 1.6, 0.2],
                      [5, 3.4, 1.6, 0.4],
                      [5.5, 2.4, 3.7, 1],
                      [5.8, 2.7, 3.9, 1.2],
                      [7.2, 3.2, 6, 1.8],
                      [6.2, 2.8, 4.8, 1.8]])
    
    test = np.array([[5, 3.6, 1.4, 0.2],
                     [5.4, 3.9, 1.7, 0.4],
                     [6.9, 3.1, 4.9, 1.5],
                     [5.5, 2.3, 4, 1.3],
                     [6.4, 2.7, 5.3, 1.9],
                     [6.8, 3, 5.5, 2.1]])
    
    # first get subtraction of each row of train to test
    subtraction = train[:, None, :] - test[None, :, :]
    # get distance from each train_row to test
    s = np.linalg.norm(subtraction, axis=2, keepdims=True)
    print(np.min(s, axis=1))
    # get minimum
    q = np.argmin(s, axis=1)
    print("minimum indices:")
    print(q)
    

    输出:

    [[0.63245553]
     [0.34641016]
     [0.43588989]
     [0.51961524]
     [0.73484692]
     [0.55677644]]
    minimum indices:
    [[0]
     [0]
     [3]
     [3]
     [5]
     [4]]
    

    【讨论】:

    • 这其实只是求对应行之间的距离。测试和训练的第 1 行,测试和训练的第 2 行等。我希望每行训练从测试的每一行中减去,并且这些子阵列的距离将最小。因此,将从所有 6 行测试中减去 train 的第 1 行,然后将公式应用于那些以找到最小距离。然后,我将针对所有 6 行测试对 train 的第 2 行执行此操作,依此类推。
    • @user11861166 我已经编辑了我的答案,检查是否可以
    • 是的,这已经解决了我的问题的第一部分,第二部分是打印用于获取最小距离的行索引。 0.63245553 使用训练和测试的第 1 行,而 0.34641016 使用训练的第 2 行和测试的第 1 行。
    • @user11861166 抱歉没仔细看,我又更新了。
    • 非常感谢您对此提供的帮助。对于索引,我希望使用最接近的原始数组的索引。 IE。 0.63245553(Train-0,Test-0)、0.34641016(Train-1,Test-0)等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多