我正在尝试使用 knn 算法制作推荐系统。我导入了pandas(最新版本0.24)并创建了一个稀疏矩阵。现在我正在使用 reshape 功能,但它显示错误。
我正在使用 Jupyter Notebook。我已经导入了 3 个 CSV。
books = pd.read_csv('C:\\Users\\Shivam Upadhyay\\Desktop\\Project\\books\\BX-Books.csv', sep=';', error_bad_lines=False, encoding="latin-1")
books.columns = ['ISBN', 'bookTitle', 'bookAuthor', 'yearOfPublication', 'publisher', 'imageUrlS', 'imageUrlM', 'imageUrlL']
users = pd.read_csv('C:\\Users\\Shivam Upadhyay\\Desktop\\Project\\books\\BX-Users.csv', sep=';', error_bad_lines=False, encoding="latin-1")
users.columns = ['userID', 'Location', 'Age']
ratings = pd.read_csv('C:\\Users\\Shivam Upadhyay\\Desktop\\Project\\books\\BX-Book-Ratings.csv', sep=';', error_bad_lines=False, encoding="latin-1")
ratings.columns = ['userID', 'ISBN', 'bookRating']
query_index = np.random.choice(us_canada_user_rating_pivot.shape[0])
distances, indices = model_knn.kneighbors(us_canada_user_rating_pivot.iloc[query_index, :].reshape(1, -1), n_neighbors = 6)
for i in range(0, len(distances.flatten())):
if i == 0:
print('Recommendations for {0}:\n'.format(us_canada_user_rating_pivot.index[query_index]))
else:
print('{0}: {1}, with distance of {2}:'.format(i, us_canada_user_rating_pivot.index[indices.flatten()[i]], distances.flatten()[i]))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-d2c9c594ec59> in <module>
1 query_index = np.random.choice(us_canada_user_rating_pivot.shape[0])
----> 2 distances, indices = model_knn.kneighbors(us_canada_user_rating_pivot.iloc[query_index, :].reshape(1, -1), n_neighbors = 6)
3
4 for i in range(0, len(distances.flatten())):
5 if i == 0:
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\generic.py in __getattr__(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.__getattribute__(self, name)
5068
5069 def __setattr__(self, name, value):
AttributeError: 'Series' object has no attribute 'reshape'
最佳答案
来自 pandas
文档,
Deprecated since version 0.19.0: Calling this method will raise an error. Please call .values.reshape(...) instead.
参见pandas.Series.reshape了解更多。
关于python - 如何修复此Python代码中的属性错误:'Series'对象没有属性 'reshape'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607596/