在运行此代码时,
def recommend(features, feature_list):
neighbors = NearestNeighbors(n_neighbors=6, algorithm="brute", metric="euclidean")
neighbors.fit(feature_list)
indices = neighbors.kneighbors([features]) # <=
return indices
if uploaded_file is not None:
if save_uploaded_file(uploaded_file):
# display the file
display_image = Image.open(uploaded_file)
st.image(display_image)
# feature extract
features = feature_extraction(
os.path.join("uploads", uploaded_file.name), model
)
# st.text(features)
# recommendention
indices = recommend(features, feature_list) # <=
字符串
我得到这个错误
ValueError: Expected 2D array, got 1D array instead: array= ['images\\10000.jpg' 'images\\10001.jpg' 'images\\10002.jpg' ... 'images\\9997.jpg' 'images\\9998.jpg' 'images\\9999.jpg']. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.`
型
1条答案
按热度按时间ctehm74n1#
代码中的第4行,
indices = neighbors.kneighbors([features])
应该有2个参数(2D数组)。
但是,你已经给出了一个1D数组。
根据KNN的文档,
kneighbours()
函数至少接受2个参数:为了找到一个点的K-邻居,正确的语法是:
kneighbors([X,n_neighbors,return_distance])
return_distance默认为False。
参考编号:
x1c 0d1x的数据
因此,您需要给予n_neighbors沿着features数组。
因此,您的代码应更改为:
字符串