Simple question about numpy:
I load 100 values to a vector a. From this vector, I want to create an array A with 2 columns, where one column has name “C1” and second one “C2”, one has type int32 and another int64. An example:
a = range(100)
A = array(a).reshape( len(a)/2, 2)
# A.dtype = ...?
How to define the columns’ types and names, when I create array from a?
NumPy structured arrays have named columns:
You can access the columns by name like this:
Note that using
np.arraywithzipcauses NumPy to build an array from a temporary list of tuples. Python lists of tuples use a lot more memory than equivalent NumPy arrays. So if your array is very large you may not want to usezip.Instead, given a NumPy array
A, you could useravel()to makeAa 1Darray, and then use
viewto turn it into a structured array, and then useastypeto convert the columns to the desired type: