Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 428
YC Sang Avatar asked Feb 11 '20 08:02

YC Sang


1 Answers

As stated in another answer, the as_matrix method is deprecated since 0.23.0, so you should use to_numpy instead. However, I want to highlight the fact that as_matrix and to_numpy have different signatures: as_matrix takes 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_numpy does 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.

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_matrix to convert, which looks more or less like:

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

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

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

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

df[['col1', 'col2', 'col4']].to_numpy()  # CORRECT 
like image 92
Sal Borrelli Avatar answered Sep 21 '22 03:09

Sal Borrelli