KNN基础使用

import numpy as np
import pandas as pd
from sklearn.neighbors import NearestNeighbors


samples = [[0, 0, 2], [1, 0, 0], [0, 0, 1], [1, 1, 0], [1, 1, 4], [4, 1, 0]]
samples2 = [[1, 1, 3], [2, 1, 0]]


neighbors_model = NearestNeighbors(n_neighbors=3, metric='cosine')
neighbors_model.fit(samples)
indices = neighbors_model.kneighbors(samples2, return_distance=False)
predictions = []
print(indices)

for k in range(len(indices)):
    pred = indices[k]
    print(pred)


indices shape: n_samples2 * n_neighbours
[[4 2 0]
 [5 3 1]]

distances shape: n_samples2 * n_neighnour
[[0.00506332 0.09546597 0.09546597]
 [0.02381294 0.0513167  0.10557281]]

优化

  • 距离定义
  • K
  • KNN with alpha-QE + DBA

Automatic Query Expansion

你可能感兴趣的:(机器学习,python,开发语言)