AttributeError:“ numpy.ndarray”对象没有属性“ as_matrix”

时间:2018-11-14 11:01:11

标签: python-3.x

我遇到了一个代码https://github.com/scorpionhiccup/StockPricePrediction,看起来很有趣。但是,当我尝试执行程序时(regression_models.py即https://github.com/scorpionhiccup/StockPricePrediction/blob/master/scripts/Algorithms/regression_models.py),我收到了错误

AttributeError: 'numpy.ndarray' object has no attribute 'as_matrix'

产生此错误的代码部分是

for pred in predicted_values:
        mean_squared_errors.append(mean_squared_error(test[output].as_matrix(), \
         pred.as_matrix()))
        r2_scores.append(r2_score(test[output].as_matrix(), pred.as_matrix()))

我是python的新手,我尝试了很多,但没有找到解决方案。任何帮助表示赞赏。 我正在使用基于以下内容的ActivePython 3.6.0.3600(ActiveState Software Inc.) 在Win32上使用Python 3.6.0(默认值,2017年1月23日,20:01:14)[MSC v.1900 64位(AMD64)]

1 个答案:

答案 0 :(得分:0)

as_matrix()pandas.DataFramehttps://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.as_matrix.html

的旧方法

您显然正在传递一个NumPy数组,其中代码期望使用DataFrame。但这很好,因为所有as_matrix()所做的就是将DataFrame转换为数组。因此,请尝试仅删除一个或多个.as_matrix()调用(我不知道是哪个问题,或者可能是所有问题)。