将阵列插入非线性方程

2024-04-20 11:58:58 发布

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

我对Python很陌生。。。我很难将一维数组的内容插入到一个非线性方程中,这样我才能最终绘制出结果。我的代码如下:

import numpy as np
import matplotlib.pyplot as plt

def readfiles(file_list):
    """ read <TAB> delemited files as strings
        ignoring '# Comment' lines """
    data = []
    for fname in file_list:
        data.append(
                    np.genfromtxt(fname,
                                  comments='#',    # skip comment lines
                                  delimiter='\t',
                                  dtype ="|S", autostrip=True).T)
    return data

data = readfiles(['CR1000_rawMeasurements_15m.txt'])

def column(matrix, i):
    return [row[i] for row in matrix]

x = column(data,18)

for i in x:
    thermTemp1_degC = 1/(1.401E-3 + 2.377E-4*np.log(i) + 9.730E-8*np.log(i)**3)-273.15

我所能成功做的就是从我的数据中提取我需要的列。当我运行这个脚本时,会得到“TypeError:Not implemented for this type.”(我的1d数组x现在只是一列零。)如何修复这个问题?你知道吗


Tags: inimportfordatareturndefasnp
1条回答
网友
1楼 · 发布于 2024-04-20 11:58:58

这里有几点需要说明。你知道吗

返回正确的列

您在注释中给出的数组有点奇怪,但是您可以使用numpy检索列:

data = [[ 737055., 0.], [ 737055., 0.], [ 737055., 0.], [ 737055., 0.], [ 737055., 0.], [ 735773., 0.], [ 735773., 0.], [ 735773., 0.]]]

data
=> [[[737055.0, 0.0],
     [737055.0, 0.0],
     [737055.0, 0.0],
     [737055.0, 0.0],
     [737055.0, 0.0],
     [735773.0, 0.0],
     [735773.0, 0.0],
     [735773.0, 0.0]]]

column_0 = np.array(data)[0][:, 0]
column_1 = np.array(data)[0][:, 1]

column_0
=> array([ 737055.,  737055.,  737055.,  737055.,  737055.,  735773.,
           735773.,  735773.])

column_1
=> array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

执行计算

因为x是一个numpy数组(如果使用上面的列代码),所以不需要将其放入for循环中:

thermTemp1_degC = 1/(1.401E-3 + 2.377E-4*np.log(i) + 9.730E-8*np.log(i)**3)-273.15

这里thermTemp1_degC是一个与x大小相同的numpy数组。你知道吗

相关问题 更多 >