Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二维浮点列表到二维浮点数组的python转换_Python_Arrays_Numpy - Fatal编程技术网

二维浮点列表到二维浮点数组的python转换

二维浮点列表到二维浮点数组的python转换,python,arrays,numpy,Python,Arrays,Numpy,我试图将一列浮点值读入一组CSV文件的列表中,然后附加到二维列表中,然后将其转换为二维数组,但该数组没有转换为二维浮点值数组(如下所述)。我哪里做错了 import numpy symbols = ['CVX', 'COP', 'MMM', 'BP', 'HMC', 'TM'] AA_lst = [] nSyms = len(symbols) shortest = 99999 for sym in symbols fn = "PricesOf_" + sym + ".csv" c

我试图将一列浮点值读入一组CSV文件的列表中,然后附加到二维列表中,然后将其转换为二维数组,但该数组没有转换为二维浮点值数组(如下所述)。我哪里做错了

import numpy
symbols = ['CVX', 'COP', 'MMM', 'BP', 'HMC', 'TM']
AA_lst = []
nSyms = len(symbols)
shortest = 99999
for sym in symbols
    fn = "PricesOf_" + sym + ".csv"
    col = getCSVcolumn( fn, "Close" )
    print( "type(col)="    + str(type(col)) )     # --> <class 'list'>
    print( "type(col[0])=" + str(type(col[0])) )  # --> <class 'float'>
    shortest = min(shortest,len(col))
    AA_lst.append(col)                            # appended as a row of AA_lst

AA = numpy.array( AA_lst )
print( "type=(AA)="     + str(type(AA)) )         # --> <class 'numpy.ndarray'>
print( "type=(AA[0]="   + str(type(AA[0])) )      # --> <class 'list'>
#print( "type=(AA[0,0]=" + str(type(AA[0,0])) )   # --> Error, too many indices

# fix up dimensions (so rows are all the same length)
AA = numpy.resize( AA, (nSyms, shortest) )
print( "type=(AA)="     + str(type(AA)) )         # --> <class 'numpy.ndarray'>
print( "type=(AA[0]="   + str(type(AA[0])) )      # --> <class 'numpy.ndarray'>
print( "type=(AA[0,0]=" + str(type(AA[0,0])) )    # --> <class 'list'>

# desire something of the form:  array([[1,2,3] [4,5,6] [7,8,9]])
# i.e. expecting type(AA[0,0] to be <class 'float'>, not <class 'list'>
导入numpy
符号=['CVX'、'COP'、'MMM'、'BP'、'HMC'、'TM']
AA_lst=[]
nSyms=len(符号)
最短=99999
对于符号中的符号
fn=“价格”+sym+“.csv”
col=getCSVcolumn(fn,“关闭”)
打印(“类型(列)=”+str(类型(列)))#-->
打印(“类型(列[0])=”+str(类型(列[0]))#-->
最短=最小(最短,长度(列))
附加(列)#作为一行附加
AA=numpy.数组(aalst)
打印(“类型=(AA)=”+str(类型(AA)))#-->
打印(“类型=(AA[0]=”+str(类型(AA[0])))#-->
#打印(“type=(AA[0,0]=”+str(type(AA[0,0])))#-->错误,索引太多
#修正尺寸(使所有行的长度相同)
AA=numpy.resize(AA,(nSyms,最短))
打印(“类型=(AA)=”+str(类型(AA)))#-->
打印(“类型=(AA[0]=”+str(类型(AA[0])))#-->
打印(“类型=(AA[0,0]=”+str(类型(AA[0,0])))#-->
#想要某种形式的东西:数组([[1,2,3][4,5,6][7,8,9]]
#即,期望类型(AA[0,0]为,而不是
  • 确保
    aalst
    中的列表长度相同
  • 考虑使用

  • 将numpy.resize(…)替换为以下内容可解决此问题:


    并将AAA_lst馈送到numpy.array()

    发生这种情况可能是因为您没有二维dtype数组
    float
    ,您有一维dtype数组
    object
    ,因为
    numpy
    不支持异构大小的数组,所以它尽了最大的努力。我在解释代码…符号是从交叉相关的文件名中提取的标记列表Latitions(每个文件名两个符号),没有重复。那么,如何设置以获得二维浮点数组?如果我创建一个同构的矩形列表(并跳过调整大小),AA[I,j]的类型为float。问题已解决。
    AAA_lst = []
    for row in AA_lst:
        AAA_lst.append( row[:shortestUnshifted] )