Can't convert a given np.ndarray to a tensor

492e2b01855c8315b26d: <pre><code class="lang-auto">import torch
from torch import nn
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
xy = pd.read_csv("all.csv",names=['pm2.5','pm10,s02','no2','co','o3','tem','hig_tem','low_tem','rain','qiya','shidu','zuixiaoshidu','fengsu_10min','fengsu','rizuidafengsu','rizhao'])
x_data = xy.iloc[6:-1,:-1].values
y_data = xy.iloc[:6,:-1].values


x_data = Variable(torch.from_numpy(x_data))
y_data = Variable(torch.from_numpy(y_data))
.....
</code></pre>
<p>I want to run an RNN but  i can’t solve the data problem . Then i got an error:</p>
<pre><code class="lang-auto">RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8.
</code></pre>
<p>how can i make it available?</p>

Tank: <p>Can you print the type of your data? you might have to cast it as a one of the accepted types before you convert?</p>

492e2b01855c8315b26d: <pre><code class="lang-auto">print(x_data[:5,:5])
</code></pre>
<p>result:<br>
[[-164.2 -211.7 -87.2 -25.8 -1272.1]<br>
[-27.8 4.1 34.0 6.6 -381.1]<br>
[64.5 111.9 129.7 30.8 788.7]<br>
[170.9 241.6 44.3 36.4 2754.0]<br>
[-259.6 -403.2 -207.5 -65.5 -3982.6]]</p>

Tank: <p>Seeing floats doesn’t necessarily mean it is a float. What does x_data.dtype return? Try and explicitly cast x_data as a type. x_data.astype(dtype = 'float32)</p>

492e2b01855c8315b26d: <p>the  x_data.dtype is object and i try</p>
<pre><code class="lang-auto">x_data.astype(np.float32)
x_data = Variable(torch.from_numpy(x_data))
</code></pre>
<p>but i failed<br>
get this result:</p>
<pre><code class="lang-auto">ValueError: could not convert string to float: '#VALUE!'
</code></pre>

Tank: <aside class="quote no-group quote-modified" data-username="492e2b01855c8315b26d" data-post="5" data-topic="21321">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/4/ad7895/48.png" class="avatar"> 492e2b01855c8315b26d:</div>
<blockquote>
<p><span class="hashtag-raw">#VALUE</span>!’</p>
</blockquote>
</aside>
<p>basically you need to remove all the rows that contain <span class="hashtag-raw">#value</span>!. the data set you are using doesn’t contain only numbers as I expected. Once you have removed these values it should work. Also try and make sense of the error messages… sometimes they are not very helpful but in this case it is.</p>

492e2b01855c8315b26d: <p>thanks very much<br>
i will try again</p>

492e2b01855c8315b26d: <p>ooooops<br>
i try to make data small and accurate</p>
<pre><code class="lang-auto">xy = pd.read_csv("all.csv",names=['pm2.5','pm10,s02','no2','co','o3','tem','hig_tem','low_tem','rain','qiya','shidu','zuixiaoshidu','fengsu_10min','fengsu','rizuidafengsu','rizhao'])
x_data = xy.iloc[7:16,:2192].values
y_data = xy.iloc[:6,:2192].values

x_data.astype(np.float32)
x_data = Variable(torch.from_numpy(x_data))
</code></pre>
<p>result:</p>
<pre><code class="lang-auto">RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8.

</code></pre>
<p>and i have checked the data …there is no <span class="hashtag">#VALUE</span></p>

ptrblck: <p>Could you please print <code>np.unique(x_data)</code> before passing it to the tensor constructor?<br>
As <a class="mention" href="/u/tank">@Tank</a> explained there seem to be some invalid values in your array.</p>

492e2b01855c8315b26d: <p>the result of np.unique(x_data)</p>
<pre><code class="lang-auto">TypeError: '&gt;' not supported between instances of 'float' and 'str'
</code></pre>
<p>but i don’t know  where the str?</p>

Tank: <p>you probably have some sort of nan value or empty string e.g. ’ ’ somewhere. Trying np.unique() on each column or row of the dataset might help you narrow down the problem.</p>

Mostafa_Elhoushi: <p>I had a similar problem when converting data read from pandas to PyTorch.<br>
I was able to solve it using <code>pd.to_numeric(...)</code></p>
<p>Hence, for the code at the top, the fix could be:</p>
<pre><code class="lang-auto">x_data = pd.to_numeric(xy.iloc[6:-1,:-1].values)
y_data = pd.to_numeric(xy.iloc[:6,:-1].values)
</code></pre>