Trying to create a tagged numpy array

I want to have a numpy array with values ​​and corresponding labels for each value. I use this array for linear regression, and it will be my data vector Xin the equation y = Xb + error.

My Xvector consists of about 20 variables, each of which I would like to be able to refer by name like that X['variable1']. At first I used a dictionary for this, but realized that the scikit library for linear regression requires a numpy matrix, so I'm trying to create a numpy array that is marked as.

I get an error message:

TypeError: a bytes-like object is required, not 'int'.

This is what I do:

X = np.array([3],dtype=[('label1','int')])

In the end, I want to have 20 marked values, something like this:

X = np.array([3,40,7,2,...],
             dtype=[('label1',int'),('label2','int'),('label3','int')...])

Truly appreciate any syntax help here. Thanks!

+4
2

:

In [55]: X
Out[55]: 
array([(3,)], 
      dtype=[('label1', '<i4')])

In [56]: X=np.array([(3,4)],dtype=[('label1',int),('label2',int)])

In [57]: X
Out[57]: 
array([(3, 4)], 
      dtype=[('label1', '<i4'), ('label2', '<i4')])

, 2d ( ), 1d :

In [58]: X.shape
Out[58]: (1,)

In [59]: X.dtype
Out[59]: dtype([('label1', '<i4'), ('label2', '<i4')])

; X*2 X.sum() . X y = X*b + error .

, 2d- .

Pandas.

+5

20 , :

from collections import OrderedDict  # Dictionary that remembers insertion order
import numpy as np

dd = OrderedDict()
dd["Var1"] = 10
dd["Var2"] = 20
dd["Var3"] = 30

# make numpy array from dict:
xx = np.array([v for v in dd.values()])  

# make dict() from array:
xx2 = 2*xx
dd2 = OrderedDict((k, v) for (k,v) in zip(dd.keys(), xx2))
0

Source: https://habr.com/ru/post/1614511/


All Articles