python - TypeError unhashable type: 'numpy.ndarray' 当我尝试绘制线性回归的结果时
问题描述
我正在尝试做一些简单的线性回归。我有一个包含书名、出版日期、作者姓名等的书籍数据集。我试图根据出版年份预测一本书的平均好读率。我使用了以下代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as seabornInstance
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
df = pd.read_csv('books.csv', error_bad_lines= False, warn_bad_lines = False, encoding= 'utf-8')
df.columns = ['BookID', 'Title','Authors', 'Average_Rating', 'ISBN', 'ISBN13', 'Language', 'Pages', 'Ratings_Count', 'Text_Reviews_Count', 'Publication_Date', 'Publisher']
df.drop(columns = ["ISBN", "ISBN13"], inplace = True)
df.loc[df.Language == "en-US", "Language"] = "eng"
df.loc[df.Language == "en-GB", "Language"] = "eng"
df.loc[df.Language == "en-CA", "Language"] = "eng"
df.loc[df.Language != "eng", "Language"] = "other"
temp = df["Publication_Date"].str.split("/", n = 2, expand = True)
df["Publication_Month"] = temp[1]
df["Publication_Year"] = temp[2]
X = df["Publication_Year"].values.reshape(-1,1)
y = df["Average_Rating"].values.reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
regressor = LinearRegression()
regressor.fit(X_train, y_train)
print(regressor.intercept_)
print(regressor.coef_)
y_pred = regressor.predict(X_test)
在我尝试将我的预测与训练集上的实际值进行对比之前,我没有任何问题。我不断收到 TypeError 不可散列的类型:'numpy.ndarray'。
plt.plot(X_test, y_pred, label = "Predictions", color='red', linewidth=2)
plt.plot(X_test, y_test, label = "Actual Values", color='blue', linewidth=2)
plt.xlabel('Year of publication')
plt.ylabel('Avg. Rating')
plt.show()
我该如何解决这个问题?
解决方案
推荐阅读
- r - 如何获取R中重复值的映射?
- java - 是否可以将 Kotlin 的代理委托与现有的类/对象(即箭头的 Either)一起使用?
- facebook - Facebook Oauth 提示并不要求提供所有范围
- lua - 为什么当键是表时lua kv对是序列
- unity3d - Unity - 启动构建的游戏.exe时,窗口每次都缩小?
- html - 如何根据父组件动态调整子组件的高度?
- ios - 在结构类上查看为 var
- html - 具有不透明度的子级不能出现在具有 z-index 的父级兄弟姐妹的下方
- ios - 从 Firebase 实时数据库中检索数据现在间歇性工作
- html - chrome 84 版中“a”标签的“href”行为是否发生了变化?