TypeError: float() argument must be a string or a number, not 'list' and ValueError: setting an array element with a sequence
TypeError: float() argument must be a string or a number, not 'list' and ValueError: setting an array element with a sequence
Sample = [{'id': 1, 'Jan': 150, 'Feb': 200, 'Mar': [[.332, .326], [.058, .138]]},
{'id': 2, 'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], [.234, .395]]},
{'id': 3, 'Jan': 50, 'Feb': 90, 'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)
df
df=df.assign(New=df[['Feb', 'Jan']].values.tolist())
这里我尝试从数据帧中找到 K 个最近的邻居
from sklearn.neighbors import NearestNeighbors
knn = NearestNeighbors(n_neighbors=10, algorithm='auto')
knn.fit(df['New'].to_numpy())
def get_neighbors(id):
vector = df.loc[id]
return knn.kneighbors([vector], 10, return_distance=False)
但是 TypeError: float() 参数必须是字符串或数字,而不是 'list' 出现在行 knn.fit(df['New'].to_numpy())
df=df.assign(New=df[['Feb', 'Jan']].values.tolist())
试试这条线
New = df[['Feb', 'Jan']].values.tolist()
Sample = [{'id': 1, 'Jan': 150, 'Feb': 200, 'Mar': [[.332, .326], [.058, .138]]},
{'id': 2, 'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], [.234, .395]]},
{'id': 3, 'Jan': 50, 'Feb': 90, 'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)
df
df=df.assign(New=df[['Feb', 'Jan']].values.tolist())
这里我尝试从数据帧中找到 K 个最近的邻居
from sklearn.neighbors import NearestNeighbors
knn = NearestNeighbors(n_neighbors=10, algorithm='auto')
knn.fit(df['New'].to_numpy())
def get_neighbors(id):
vector = df.loc[id]
return knn.kneighbors([vector], 10, return_distance=False)
但是 TypeError: float() 参数必须是字符串或数字,而不是 'list' 出现在行 knn.fit(df['New'].to_numpy())
df=df.assign(New=df[['Feb', 'Jan']].values.tolist())
试试这条线
New = df[['Feb', 'Jan']].values.tolist()