AttributeError: 'Series' object has no attribute 'as_matrix' in PyTorch 1.16

I am running a PyTorch code (2018 ish) with PyTorch 1.16 and I got the following error. Is there a quick fix for it?

landmarks_frame = pd.read_csv('faces/face_landmarks.csv')

n = 65 # Number of landmarks
img_name = landmarks_frame.iloc[n, 0]
landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
landmarks = landmarks.astype('float').reshape(-1, 2)

print('Image name: {}'.format(img_name))
print('Landmarks shape: {}'.format(landmarks.shape))
print('First 4 Landmarks: {}'.format(landmarks[:4]))

the error is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-8411fb211b60> in <module>
      3 n = 65 # Number of landmarks
      4 img_name = landmarks_frame.iloc[n, 0]
----> 5 landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
      6 landmarks = landmarks.astype('float').reshape(-1, 2)
      7 

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
   5275 
   5276     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'as_matrix'

The following works. I am not exactly sure why

landmarks_frame = pd.read_csv('faces/face_landmarks.csv')

##n = 65 # Number of landmarks
##img_name = landmarks_frame.iloc[n, 0]
##landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
##landmarks = landmarks.astype('float').reshape(-1, 2)

##print('Image name: {}'.format(img_name))
##print('Landmarks shape: {}'.format(landmarks.shape))
##print('First 4 Landmarks: {}'.format(landmarks[:4]))




n = 65
img_name = landmarks_frame.iloc[n, 0]
landmarks = landmarks_frame.iloc[n, 1:]
landmarks = np.asarray(landmarks)
landmarks = landmarks.astype('float').reshape(-1, 2)

print('Image name: {}'.format(img_name))
print('Landmarks shape: {}'.format(landmarks.shape))
print('First 4 Landmarks: {}'.format(landmarks[:4]))
print('image name:', img_name)

Hi,

This is not PyTorch issue, it is related to Pandas and the issue is that as_matrix has been deprecated in favor of to_numpy.

Bests

1 Like