pandas AttributeError: 'Series' 对象没有属性 'as_matrix' 为什么会出错?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/60164560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:25:26  来源:igfitidea点击:

AttributeError: 'Series' object has no attribute 'as_matrix' Why is it error?

pythonpandasnumpy

提问by YC Sang

When I execute the code of the official website, I get such an error. Why? code show as follow:

当我执行官网的代码时,出现这样的错误。为什么?代码显示如下:

landmarks_frame = pd.read_csv(‘F:\OfficialData\faces\face_landmarks.csv')
n = 65
img_name = landmarks_frame.iloc[n, 0]
landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
landmarks = landmarks.astype(‘float').reshape(-1, 2)

List item

项目清单

回答by Sal Borrelli

As stated in another answer, the as_matrixmethod is deprecated since 0.23.0, so you should use to_numpyinstead. However, I want to highlight the fact that as_matrixand to_numpyhave different signatures: as_matrixtakes a list of column names as one of its parameter, in case you want to limit the conversion to a subset of the original DataFrame; to_numpydoes not accept such a parameter. As a consequence, the two methods are completely interchangeable only if you want to convert the DataFrame in full. If you (as in my case) need to convert a subset of the matrix, the usage would be quite different in the two use cases.

如另一个答案所述,该as_matrix方法自 0.23.0 起已弃用,因此您应该to_numpy改用。但是,我想强调一个事实,as_matrix并且to_numpy具有不同的签名:as_matrix将列名列表作为其参数之一,以防您想将转换限制为原始 DataFrame 的子集;to_numpy不接受这样的参数。因此,只有当您想完全转换 DataFrame 时,这两种方法才完全可以互换。如果您(如我的情况)需要转换矩阵的子集,则两种用例的用法将大不相同。

For example let's assume we only need to convert the subset ['col1', 'col2', 'col4'] of our original DataFrame to a Numpy array. In that case you might have some legacy code relying on as_matrixto convert, which looks more or less like:

例如,假设我们只需要将原始 DataFrame 的子集 ['col1', 'col2', 'col4'] 转换为 Numpy 数组。在这种情况下,您可能有一些依赖于as_matrix转换的遗留代码,它或多或少看起来像:

df.as_matrix(['col1', 'col2', 'col4'])

While converting the above code to to_numpyyou cannot simply replace the function name like in:

将上述代码转换为to_numpy您不能简单地替换函数名称,如:

df.to_numpy(['col1', 'col2', 'col4'])  # WRONG

because to_numpydoes not accept a subset of columns as parameter. The solution in that case would be to do the selection first, and apply to_numpyto the result, as in:

因为to_numpy不接受列的子集作为参数。在这种情况下,解决方案是先进行选择,然后应用于to_numpy结果,如下所示:

df[['col1', 'col2', 'col4']].to_numpy()  # CORRECT

回答by Remis Haroon

The purpose of as_matrixmethod is to

as_matrix方法的目的是

Convert the frame to its Numpy-array representation.

将帧转换为其 Numpy 数组表示。

as_matrixmethod is deprecated since 0.23.0
0.25.1 documentationsays : Deprecated since version 0.23.0: Use DataFrame.values() instead

as_matrix方法自 0.23.0
已弃用0.25.1 文档说:自 0.23.0 版起已弃用:改为使用 DataFrame.values()

The two alternatives are

两种选择是

  1. .values() : Returns numpy.ndarray
  2. .to_numpy() : Returns numpy.ndarray
  1. .values() :返回 numpy.ndarray
  2. .to_numpy() : 返回 numpy.ndarray

However, .values()documentationgives another warning :- Warning We recommend using DataFrame.to_numpy() instead.

但是,.values()文档给出了另一个警告:-Warning We recommend using DataFrame.to_numpy() instead.

I got the error in a slightly different way : AttributeError: 'DataFrame' object has no attribute 'as_matrix'

我以稍微不同的方式得到错误: AttributeError: 'DataFrame' object has no attribute 'as_matrix'