查看一个 list 中的元素是否全都一样
变成set之后看看元素个数
len(set(list_a.to_numpy()))
python 查看两个 list 是否有相同的元素
set_c = set(list_a) & set(list_b)
二维 array 查看最值,float 2 int 等操作
比如 arr.shape=(600, 30)
并不能直接 max(arr)
来看,会报经典的 The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
,可以 reshape 之后再来看:
max(arr.reshape(-1, 1))
相同的,也不能直接 int(arr)
把元素从 float 变成 int,应该:
arr.astype (int)
构建 pairwise distance 矩阵
抄自:www.shuzhiduo.com/A/obzbDAYV5…
from sklearn.metrics import pairwise_distances
from scipy.spatial import distance_matrix
from scipy.spatial.distance import cdist
三者都是计算 pairwise distance,发现 cdist
最快,占用 CPU 最少,选他:
import numpy as np
from scipy.spatial.distance import cdist
# 10-dimensional features
x = np.random.rand(400000).reshape((-1, 10))
y = np.random.rand(45000).reshape((-1, 10))
dists = cdist(x, y) # shape: (40000, 4500)