Problem combining python list and numpy array?

I have two python lists and a numpy array. The numpy array looks like this:

[array([93495052.969556, 98555123.061462])]
[array([1000976814.605984, 998276347.359732])]
[array([6868127850.435482, 6903911250.620625])]
[array([775127467.947004, 802369832.938230])]

This one numpy arrayis formed from the following code:

array1 = []
company = []
state = []

def process_chunk(chuk):

    training_set_feature_list = []
    training_set_label_list = []
    test_set_feature_list = []
    test_set_label_list = []
    np.set_printoptions(suppress=True)

    array2 = []


    # to divide into training & test, I am putting line 10th and 11th in test set
    count = 0
    for line in chuk:
        # Converting strings to numpy arrays
        if count == 9:   
            test_set_feature_list.append(np.array(line[3:4],dtype = np.float))
            test_set_label_list.append(np.array(line[2],dtype = np.float))
            company.append(line[0])
            state.append(line[1])
        elif count == 10:
            test_set_feature_list.append(np.array(line[3:4],dtype = np.float))
            test_set_label_list.append(np.array(line[2],dtype = np.float))

        else:    
            training_set_feature_list.append(np.array(line[3:4],dtype = np.float))
            training_set_label_list.append(np.array(line[2],dtype = np.float))
        count += 1
    # Create linear regression object
    regr = linear_model.LinearRegression()
    # Train the model using the training sets
    regr.fit(training_set_feature_list, training_set_label_list)

    #print test_set_feature_list

    array2.append(np.array(regr.predict(test_set_feature_list),dtype = np.float))
    np.set_printoptions(formatter={'float_kind':'{:f}'.format})
    for items in array2:
        array1.append(items)

array1 is a numpy array that I want to combine with two python lists

The first python list is equal company, which looks like this:

['OT', 'OT', 'OT', 'OT',....]

The second python list is state:

['Alabama', 'Alabama', 'Alabama', 'Alabama', ...]

Now what I'm trying to do is form one list, which has the following structure:

('OT', 'Alabama', 729, 733)
('OT', 'Alabama', 124, 122)
('OT', 'Arizona', 122, 124)

I wrote this line of code - final_list = zip(company,state,array1)but this produces this output (with the addition arrayand []around the elements of the array):

('OT', 'Alabama', array([729, 733]))
('OT', 'Alabama', array([124, 122]))

How to join these lists and an array to form one list that does not have a problem?

+4
2

array1 :

array1 = np.array([np.array([729, 733]), np.array([124, 122]) ...])

( array1.append(), array1 - ). array1 numpy.array -

narray1 = np.array(array1)           #This step not necessary , if array1 is already numpy array , in that case use `array1` instead of `narray1` .

, , -

final_list = zip(company,state,narray1[:,0], narray1[:,1])

-

In [59]: array1 = [np.array([729, 733]), np.array([124, 122])]

In [60]: company = ['OT', 'OT']

In [61]: state = ['Alabama', 'Alabama']

In [62]: narray1 = np.array(array1)

In [63]: final_list = zip(company,state,narray1[:,0], narray1[:,1])

In [65]: final_list
Out[65]: [('OT', 'Alabama', 729, 733), ('OT', 'Alabama', 124, 122)]
+2

, ( )

, . , ():

In [19]: data = [('OT', 'Alabama', 729, 733),
('OT', 'Alabama', 124, 122),
('OT', 'Arizona', 122, 124)]

dtype ( ):

In [23]: dt = np.dtype('S2,S10,i,i')

:

In [24]: A=np.array(data,dtype=dt)

In [25]: A
Out[25]: 
array([('OT', 'Alabama', 729, 733), ('OT', 'Alabama', 124, 122),
       ('OT', 'Arizona', 122, 124)], 
      dtype=[('f0', 'S2'), ('f1', 'S10'), ('f2', '<i4'), ('f3', '<i4')])

:

In [26]: A['f1']
Out[26]: 
array(['Alabama', 'Alabama', 'Arizona'], 
      dtype='|S10')

In [27]: A['f2']
Out[27]: array([729, 124, 122])

:

In [28]: A.tolist()
Out[28]: 
[('OT', 'Alabama', 729, 733),
 ('OT', 'Alabama', 124, 122),
 ('OT', 'Arizona', 122, 124)]

( ) dtype ,

A1 = np.zeros((3,),dtype=dt)
A1['f0']=['OT','OT','OT']
A1['f2']=np.array([729,124,122])
etc

(3,2) , :

A1['f2']=x[:,0]
A1['f3']=x[:,1]
+1

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


All Articles