AttributeError: 'Series' object has no attribute 'as_matrix' (From kaggle)

username: DaveRH
cooked: <p>I am new to using Torch but I am trying to use a pretrained model to build my model. As a first step to understanding the process I am trying to duplicate the results from the kaggle site: <a href="https://www.kaggle.com/code/pvlima/use-pretrained-pytorch-models/notebook" class="inline-onebox" rel="noopener nofollow ugc">Use pretrained PyTorch models | Kaggle</a></p>
<p>All is well until I reach the first line of cell 13: img, label = next(iter(train_dl))</p>
<h2>
<a name="i-then-get-an-attributeerror-1" class="anchor" href="#i-then-get-an-attributeerror-1"></a>I then get an AttributeError:</h2>
<p>AttributeError                            Traceback (most recent call last)<br>
/tmp/ipykernel_30361/1705970776.py in <br>
----&gt; 1 img, label = next(iter(train_dl))<br>
2 print(img.size(), label.size())<br>
3 fig = plt.figure(1, figsize=(16, 4))<br>
4 grid = ImageGrid(fig, 111, nrows_ncols=(1, 4), axes_pad=0.05)<br>
5 for i in range(img.size()[0]):</p>
<p>~/anaconda3/lib/python3.9/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
528             if self._sampler_iter is None:<br>
529                 self._reset()<br>
 → 530             data = self._next_data()<br>
531             self._num_yielded += 1<br>
532             if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>~/anaconda3/lib/python3.9/site-packages/torch/utils/data/dataloader.py in _next_data(self)<br>
568     def _next_data(self):<br>
569         index = self._next_index()  # may raise StopIteration<br>
 → 570         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
571         if self._pin_memory:<br>
572             data = _utils.pin_memory.pin_memory(data)</p>
<p>~/anaconda3/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)<br>
48         if self.auto_collation:<br>
49<br>
—&gt; 50             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
51         else:<br>
52             data = self.dataset[possibly_batched_index]</p>
<p>~/anaconda3/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py in (.0)<br>
48         if self.auto_collation:<br>
49<br>
—&gt; 50             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
51         else:<br>
52             data = self.dataset[possibly_batched_index]</p>
<p>/tmp/ipykernel_30361/2070777975.py in <strong>getitem</strong>(self, idx)<br>
12         fullname = join(self.root_dir, img_name)<br>
13         image = Image.open(fullname)<br>
—&gt; 14         labels = self.labels.iloc[idx, 1:].as_matrix().astype(‘float’)<br>
15         labels = np.argmax(labels)<br>
16         if self.transform:</p>
<p>~/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py in <strong>getattr</strong>(self, name)<br>
5485         ):<br>
5486             return self[name]<br>
 → 5487         return object.<strong>getattribute</strong>(self, name)<br>
5488<br>
5489     def <strong>setattr</strong>(self, name: str, value) → None:</p>
<p>AttributeError: ‘Series’ object has no attribute ‘as_matrix’</p>
<p>I suspect that I am missing something obvious. Any help is greatly appreciated.</p>

username: ptrblck
cooked: <p>You seem to be running into a <code>pandas</code> error:<br>
<a href="https://pandas.pydata.org/pandas-docs/version/0.25.1/reference/api/pandas.DataFrame.as_matrix.html"><code>.as_matrix</code></a> is deprecated since <code>0.23.0</code> in favor of <a href="https://pandas.pydata.org/pandas-docs/version/0.25.1/reference/api/pandas.DataFrame.values.html#pandas.DataFrame.values"><code>.values</code></a>, which is apparently also not the recommended way anymore and <a href="https://pandas.pydata.org/pandas-docs/version/0.25.1/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy"><code>.to_numpy</code></a> should be used.</p>

username: DaveRH
cooked: <p>Thank you very much! Both .values and .to_numpy() work for me.</p>