Creating tensor TypeError: can't convert np.ndarray of type numpy.object_

Xara: <p>I am trying to impement this code <a href="https://github.com/tuhinsharma121/federated-ml/blob/master/notebooks/network-threat-detection-using-federated-learning.ipynb" rel="noopener nofollow ugc">https://github.com/tuhinsharma121/federated-ml/blob/master/notebooks/network-threat-detection-using-federated-learning.ipynb</a></p>
<p>while creating these pytorch sensor</p>
<pre><code class="lang-auto">X = final_df.values
y = df['Target'].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.4, random_state=42)
train_inputs = torch.tensor(X_train,dtype=torch.float).tag("#iot", "#network","#data","#train")
train_labels = torch.tensor(y_train).tag("#iot", "#network","#target","#train")
test_inputs = torch.tensor(X_test,dtype=torch.float).tag("#iot", "#network","#data","#test")
test_labels = torch.tensor(y_test).tag("#iot", "#network","#target","#test")
</code></pre>
<p>i got these errors current_tensor = hook_self.torch.native_tensor(*args, **kwargs) TypeError: can’t convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool. how to resolve this</p>
<p>I tried this code by reading other stack questions</p>
<blockquote>
<p>train_inputs = torch.tensor(X_train.astype(np.float32)).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<a class="hashtag-cooked" href="/c/data/37" data-type="category" data-slug="data" data-id="37"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>data</span></a>”,“<span class="hashtag-raw">#train</span>”)<br>
train_labels = torch.tensor(y_train).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<span class="hashtag-raw">#target</span>”,“<span class="hashtag-raw">#train</span>”)<br>
test_inputs = torch.tensor(X_test.astype(np.float32)).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<a class="hashtag-cooked" href="/c/data/37" data-type="category" data-slug="data" data-id="37"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>data</span></a>”,“<span class="hashtag-raw">#test</span>”)<br>
test_labels = torch.tensor(y_test).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<span class="hashtag-raw">#target</span>”,“<span class="hashtag-raw">#test</span>”<br>
but then it gives error ValueError: could not convert string to float: ‘Total Fwd Packets’ Total Fwd Packets is my first feature name. Any recommendation</p>
</blockquote>
<p>I also followed other threads</p>
<blockquote>
<p>train_inputs = torch.tensor(X_train.to_numpy()).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<a class="hashtag-cooked" href="/c/data/37" data-type="category" data-slug="data" data-id="37"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>data</span></a>”,“<span class="hashtag-raw">#train</span>”)<br>
but got this error</p>
<p>train_inputs = torch.tensor(X_train.to_numpy()).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<a class="hashtag-cooked" href="/c/data/37" data-type="category" data-slug="data" data-id="37"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>data</span></a>”,“<span class="hashtag-raw">#train</span>”)</p>
</blockquote>
<p>but got this error</p>
<p>train_inputs = torch.tensor(X_train.to_numpy()).tag(“<span class="hashtag-raw">#iot</span>”, “<span class="hashtag-raw">#network</span>”,“<a class="hashtag-cooked" href="/c/data/37" data-type="category" data-slug="data" data-id="37"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>data</span></a>”,“<span class="hashtag-raw">#train</span>”) AttributeError: ‘numpy.ndarray’ object has no attribute ‘to_numpy’</p>

ptrblck: <p><code>numpy.object_</code> is often referring to a mixed data type used in numpy or a collection of arrays having a different shape, which is not supported in PyTorch.<br>
Here is a small example:</p>
<pre><code class="lang-python">a = np.array([np.random.randn(1), np.random.randn(1, 1)])
a
&gt; array([[-0.8689455135513591],
         [0.6826695103629262]], dtype=object)

x = torch.from_numpy(a)
&gt; TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
</code></pre>
<p>Make sure the numpy array contains values in the same <code>dtype</code> in the same shape.</p>

Sara_Pr: <p>How can we convert this array of type objects to something that can be used in from_numpy() method</p>

ptrblck: <p>You won’t be able to directly convert it as the <code>object</code> type contains arbitrary or mixed data.<br>
Transform your numpy object to an array first and call <code>torch.from_numpy</code> afterwards.</p>