组合python列表和numpy数组的问题?

时间:2015-09-17 17:49:40

标签: python arrays list numpy

我有两个python列表和一个numpy数组。 numpy数组看起来像这样:

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

numpy array由以下代码组成:

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是我想要加入两个python列表的numpy数组

第一个python列表是company,如下所示:

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

第二个python列表是state:

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

现在我要做的是形成一个具有以下结构的列表:

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

我编写了这行代码 - final_list = zip(company,state,array1),但这会产生此输出(在数组元素周围添加了array[]):

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

如何加入这些列表和数组,以便形成一个没有上述问题的列表?

2 个答案:

答案 0 :(得分: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)]

答案 1 :(得分:1)

看起来您正在尝试创建结构化数组或其等效列表(元组列表)

为方便起见,我们只需创建一个包含数据的元组列表。我不会详细介绍如何从其他部分(现在)创建它:

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]