代码之家  ›  专栏  ›  技术社区  ›  user136819

如何使用最近邻算法在Python中将数组输出[0]转换为[0,0,0]?

  •  -2
  • user136819  · 技术社区  · 8 年前

    import numpy as np
    from sklearn.neighbors import NearestNeighbors
    
    X = np.array([[28273, 20866, 29961, 27190, 31790, 19714, 8643, 14482, 5384],
    [12343, 45634, 29961, 27130, 33790, 14714, 7633, 15483, 4484]])
    
    knn = NearestNeighbors(algorithm='auto', leaf_size=30, n_neighbors=5, p=2, 
    radius=1.0, warn_on_equidistant=True).fit(X)
    
    distance, indices = knn.kneighbors(X[0])
    print(indices)
    

    当脚本成功运行时,我也得到了一个输出- [[0 1]] (或类似的东西)
    array([[0, 1]]) [[0, 1]]
    我已经试过了 print(', '.join(indices))
    print(', '.join(indices)) TypeError: sequence item 0: expected string, numpy.ndarray found

    提前感谢您的帮助:)

    2 回复  |  直到 8 年前
        1
  •  0
  •   G.Bruce    8 年前

    打印(索引)

    (打印[0 1])

    打印b

        2
  •  0
  •   Mohamed Thasin ah    8 年前

    list(indices)
    

    尝试

    print indices
    >>>[[0 1]]
    print list(indices)
    >>>[array([0, 1])]
    print indices.tolist()
    >>>[[0, 1]]