在numpy中使用中值函数有问题吗

2024-06-15 08:44:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在numpy中使用中值函数时遇到了一个问题。该代码以前在以前的计算机上运行,但当我尝试在新机器上运行时,出现错误“cannot perform reduce with flexible type”。为了解决这个问题,我尝试使用map()函数来确保我的列表是一个浮点值,并得到以下错误消息:无法将字符串转换为浮点值:。你知道吗

再做一些调试的尝试,似乎我的问题是我的输入文件中的行分裂。行的格式是:2456893.248202,4.490,我想在“,”上拆分。但是,当我打印出该行第二列的列表时,我得到 4 . 4 9 0个

所以它似乎在某种程度上分裂了每一个字符或东西,虽然我不知道如何。代码的相关部分在下面,我很感激您的任何想法或想法,并提前表示感谢。你知道吗

def curve_split(fn):
    with open(fn) as f:
        for line in f:
            line = line.strip()
            time,lc = line.split(",")
#debugging stuff 
            g=open('test.txt','w')
            l1=map(lambda x:x+'\n',lc)
            g.writelines(l1)
            g.close()
#end debugging stuff

            return time,lc



if __name__ == '__main__':

    # place where I keep the lightcurve files from the image subtraction
    dirname = '/home/kuehn/m4/kepler/subtraction/detrending'
    files = glob.glob(dirname + '/*lc')
    print(len(files))



# in order to create our lightcurve array, we need to know
# the length of one of our lightcurve files

lc0 = curve_split(files[0])

lcarr = np.zeros([len(files),len(lc0)])

    # loop through every file
for i,fn in enumerate(files):
        time,lc = curve_split(fn)
        lc = map(float, lc)

# debugging
        print(fn[5:58])
        print(lc)
        print(time)
# end debugging

        lcm = lc/np.median(float(lc))
        #lcm = ((lc[qual0]-np.median(lc[qual0]))/
        #    np.median(lc[qual0]))
        lcarr[i] = lcm
        print(fn,i,len(files))

Tags: theinmaplentimenplinefiles