Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numpy.void to numpy.ndarray

I have loaded a .csv file in python with numpy.genfromtxt. Now it returns a 1 dimensional numpy.ndarray with in that array, numpy.void objects which are actually just arrays of integers. However I would like to convert these from typenumpy.void to numpy.array. To clarify:

>>> print(train_data.shape)
(42000,)
>>> print(type(train_data[0]))
<class 'numpy.void'>
>>> print(train_data[0])
(9, 0, 0)

So here the array (9, 0, 0) which has type numpy.void should be a numpy.array.

How can I convert all values from train_data to be numpy arrays?

Efficiency is also somewhat important because I am working with a lot of data.

Some more code

>>> with open('filename.csv, 'rt') as raw_training_data:
>>>     train_data = numpy.genfromtxt(raw_training_data, delimiter=',', names=True, dtype=numpy.integer)
>>> print(train_data.dtype)
[('label', '<i4'), ('pixel0', '<i4'), ('pixel1', '<i4')]
>>> print(type(train_data))
<class 'numpy.ndarray'>
like image 292
Tristan Avatar asked Oct 30 '18 14:10

Tristan


People also ask

What is NumPy void?

numpy. void is a dtype that can be used to represent structures of arbitrary byte width.

How do I change the Dtype of a NumPy array?

In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.

How do I flatten an array in NumPy?

By using ndarray. flatten() function we can flatten a matrix to one dimension in python. order:'C' means to flatten in row-major. 'F' means to flatten in column-major.

How do you transpose a NumPy array in Python?

NumPy Matrix transpose() - Transpose of an Array in Python The transpose of a matrix is obtained by moving the rows data to the column and columns data to the rows. If we have an array of shape (X, Y) then the transpose of the array will have the shape (Y, X).


1 Answers

Use the numpy.asarray() method, which converts an input to an array

array=numpy.asarray(train_data[0])
like image 178
samlli Avatar answered Nov 11 '22 15:11

samlli