Ad

Troubles With For Loop, .append, Np.asarray

- 1 answer

From an excelsheet, I import various columns and I write:

import numpy as np
totaloutput = []

inputdata = np.stack(various columns)
for "number of variables in columns" in inputdata:
     calculate several numpy.ndarray-type arrays
     output = np.column_stack(several numpy.ndarray-type arrays)
totaloutput.append(output)

When I print totaloutput, I get:

[array([['0.8', '4.0', '0.5', '5.0', 'X','Y', '16.0',
    '345.0', '285.0', '0.5843940254127079', '0.3583943421752271'],
   ['0.8', '4.0', '0.5', '5.0', 'X','Y', '17.0',
    '345.0', '285.0', '0.36329780170652354', '0.22314099222162737'],
   [etc], 
   [etc]],dtype='<U32'), array([['1.2', '4.0', '0.5', '5.0', 'X', 'Y',
   '16.0',
   '345.0', '15.0', '0.787996644827825', '0.48299132454894594'],
   [etc],
   [etc]],dtype='<U32'),

The type of the output according to type(totaloutput) is a list. However, to be able to export the data, I have to manage to get the data in the following shape:

[['0.800000011920929' '3.5' '1.0' '4.0' 'X', 'Y', '15.0'
'345.0' '285.0' '0.6222837267695641' '0.37663730483688007']
['0.800000011920929' '3.5' '1.0' '4.0'  'X', 'Y', '15.0'
'345.0' '285.0' '1.4079677072051757' '0.8500865690052523'][etc][etc]]

I thought I solved it by saying:

totaloutput = np.asarray(totaloutput)
totaloutput = np.reshape((totaloutput, ((len(inputdata)),11))

Whenever I extend the amount of inputdata (which is the goal of the script, to automatically calculate for a large amount of data), np.asarray does not seem to work anymore. I've found someone having problems with this too.

I can confirm this by printing totaloutput before np.asarray(totaloutput) and afterwards and state that both prints are the same. (the unwished array=...dtype thing) The weird thing is, when after np.asarray(totaloutput) I print the type, it does says numpy.ndarray

With a smaller inputdata, after np.asarray(totaloutput), it generates a neat numpy.ndarray output.

I've tried lots of other ways to get a right output, I've seen that

for i in range(len(inputdata)):
    print(totaloutput.item(i))

Is exactly what I need, but whenever I try:

for i in range(len(inputdata)):
    finaloutput = (totaloutput.item(i))

I get

totaloutput = (totaloutput.item(i))
AttributeError: 'str' object has no attribute 'item'

Ad

Answer

I am assuming your problem is you have list of numpy array, you want to stack it into one list?

If it is correct. Have tried np.vstack()

Numpy Example

Ad
source: stackoverflow.com
Ad