Can't create tensors as shown in first Captum Titanic tutorial?!

username: rbelew
cooked: <p>I’m stumped by the simplest part of the most basic “Titanic Basic” <a href="https://captum.ai/tutorials/Titanic_Basic_Interpret" rel="noopener nofollow ugc">captum tutorial</a>:  converting the data into tensors?!</p>
<p>After getting the data and performing the first preprocessing steps,<br>
converting to numpy arrays and separating out train and test sets works fine:</p>
<pre><code>	data = titanic_data.to_numpy()

	train_indices = np.random.choice(len(labels), int(0.7*len(labels)), replace=False)
	test_indices = list(set(range(len(labels))) - set(train_indices))
	train_features = data[train_indices]
	train_labels = labels[train_indices]
	test_features = data[test_indices]
	test_labels = labels[test_indices]
</code></pre>
<p>but converting to tensors doesn’t work:</p>
<pre><code>	File ".../Titanic_Basic_Interpret.py", line 139, in &lt;module&gt;
	input_tensor = torch.as_tensor(train_features,dtype=torch.float32)
	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	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>Maybe the problem is <a href="https://stackoverflow.com/a/21197863" rel="noopener nofollow ugc">“no more magic, convert_objects has been<br>
deprecated in pandas 0.17” </a>?  It seems the tutorial was added back in 2019, but other issues seem to have used it more recently?</p>
<p>I’ve tried some of the suggestions there (building a separate dictionary of data types and then <code>data = data.astype(dtype=dtypeDict)</code>, converting each column separately:</p>
<pre><code>	for c in titanic_data.columns:
	    titanic_data[c] = pd.to_numeric(titanic_data[c])
</code></pre>
<p>but these don’t go thru either. What could the issue be?!</p>

username: ptrblck
cooked: <p>Your <code>np.array</code> is an <code>object</code>, which cannot be transformed to a tensor directly.<br>
This can be the case if your array contains different incompatible types:</p>
<pre data-code-wrap="python"><code class="lang-python">a = np.array([1.0, tuple((1, 1))])
print(a.dtype)
# object
</code></pre>
<p>You could try to transform the array to a numerical dtype first via <code>.astype()</code> before transforming it to a tensor.</p>

username: rbelew
cooked: <p>hi <a class="mention" href="/u/ptrblck">@ptrblck</a> thanks so much.  that is indeed the problem.</p>
<p>I’ve tried two of the suggestions mentioned in that SO post: (<a href="https://stackoverflow.com/a/21197863:" rel="noopener nofollow ugc">https://stackoverflow.com/a/21197863:</a></p>
<ol>
<li>
<p>building a separate dictionary of data types and then <code>data = data.astype(dtype=dtypeDict)</code>, as suggested here:<br>
<a href="https://stackoverflow.com/a/43289220" class="inline-onebox" rel="noopener nofollow ugc">python - Assign pandas dataframe column dtypes - Stack Overflow</a></p>
<pre><code> dtypeDict = {}
 # numpy _internal _useffields() requires "names" and "formats" ?
 # dtypeDict = {"names": [], "formats":[]}

 for k,v in titanic_data.dtypes.items(): 
 	dtypeDict[k] = v # or v.name?
 	# or ala _useffields
 	# dtypeDict['names'].append(k)
 	# dtypeDict['formats'].append(v)
 data = data.astype(dtype=dtypeDict)
</code></pre>
</li>
<li>
<p>converting each column separately, as the first answer here:<br>
<a href="https://stackoverflow.com/a/21197863" class="inline-onebox" rel="noopener nofollow ugc">python - Assign pandas dataframe column dtypes - Stack Overflow</a></p>
<pre><code> for c in titanic_data.columns:
     titanic_data[c] = pd.to_numeric(titanic_data[c])
</code></pre>
</li>
</ol>
<p>but these don’t go thru either. How am I supposed to do this?  (And<br>
why am I the one bumping into this issue on a captum tutorial from<br>
2019?!)</p>

username: rbelew
cooked: <p>way simpler than I’d imagined:  <code>data = data.astype(np.float32)</code> is all that was required!</p>