如何计算 R^2 和 RMSE 考虑多点对均值 python

问题描述 投票:0回答:1

我想计算 R^2 和 RMSE,考虑多个点对均值 python

df = {'Key': ['1', '2','3', '4','1', '2','3', '4','1', '2','3', '4','1', '2','3', '4'],
      'value': ['21', '24','35', '30','31', '34','25', '19','25', '14','29', '23','31', '34','15', '25'],
        'category': ['M', 'M','M', 'M','K', 'K','K', 'K','T', 'T','T', 'T','Q', 'Q','Q', 'Q']}
  
# Create DataFrame
df = pd.DataFrame(df)

df["Key"] = pd.to_numeric(df["Key"])
df["value"] = pd.to_numeric(df["value"])
print(df)


s = df.pivot('Key', 'category', 'value')


ax = s.plot()
s.mean(1).plot(ax=ax, color='b', linestyle='--', label='Mean')
ax.legend()
#plt.annotate("r-squared = {:.3f}".format(r2_score(y_test, s.mean(1))), (0, 1))

python pandas numpy mean
1个回答
0
投票

你可以用这个:

s = df.pivot('Key', 'category', 'value')

mean = s.mean(1)
rmse = np.sqrt(mean_squared_error(s.mean(axis=1), mean))

r_squared = r2_score(s, np.tile(mean, (s.shape[1], 1)).T)
© www.soinside.com 2019 - 2024. All rights reserved.