4

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 numpy array is formed from 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 the numpy array that I want to join with two python lists

First python list is company which looks like this:

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

Second python list is state:

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

Now what I am trying to do is form one list which has 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 added array and [] around array elements):

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

How do I join these lists and array so as to form one list which does not have above issue?

3
  • What does array1 look like? Can you do print(array1) and update its result? Commented Sep 17, 2015 at 17:54
  • @AnandSKumar Its shown in my post above Commented Sep 17, 2015 at 17:57
  • @AnandSKumar I have provided my exact code which forms array1 and array2. The function is run multiple times. Commented Sep 17, 2015 at 18:16

2 Answers 2

2

If array1 looks something like -

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

Though given your code (and usage of array1.append() , it looks like array1 is a list) . You can first convert the array1 list into numpy.array as -

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

Then if there are only two values per elements, you can do -

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

Demo -

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)]
Sign up to request clarification or add additional context in comments.

10 Comments

I am getting some weird output structure. It might be because of the way my numpy array is formed. I have updated my post above to include code which form the numpy array
What is the output you are getting after using * ?
Instead of array1 , try using array2 , What is the result of regr.predict(test_set_feature_list) like ?
array2 is temporary array inside my code and array1 is the global array. I am not using array2 because with each iteration of the code it gets refreshed. So before it gets refreshed I add the contents of array2 to global array1
Then you would need to show an exact print of array1 , the array you show does not look like a valid array (pointing to the array in the question) .
|
1

It looks like you are trying to create a structured array, or its list equivalent (a list of tuples)

For convenience, lets just creat a list of tuples with your data. I won't get into the details of how this can be created from the other pieces (for now):

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

Define a compound dtype (one of several possible formats):

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

and the structured array:

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')])

which can be accessed by field name:

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

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

convert back to a list of tuples:

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

You can also create an empty (or zero) array of the right size and dtype, and fill it, row by row, or field by field

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

if the numbers are in a (3,2) integer array, you could use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.