如何使用numpy对行数组执行操作?

2024-04-25 02:02:54 发布

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

我有一个吸光度值矩阵,我从一大堆光谱中提取出来。我称这个矩阵为“specdt”

每行表示特定波长下多个样本的值。我想根据一个单独的浓度值数组“concentration”,找出回归的r^2值

以下是我目前掌握的情况:

regression = []
for row in specdt:
    x = Concentration
    y = specdt[row,:]
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
    regression.append(r_value**2)

regression_n = numpy.asarray(regression)
numpy.savetxt("r2_2.csv", regression_n, delimiter=",")

我得到一个错误:

Traceback (most recent call last):
   file "blah blah", line 42, in <module>
   y = specdt[row,:]
InexError: arrays used as indices must be of integer (or boolean) type

我怀疑这是因为“row”不是整数,所以我尝试迭代一个“t”变量;运气不好。你知道吗

我怀疑这是我试图将行拉入linregresse的y值的方式,但我似乎找不到其他方法来实现这一点。你知道吗

任何建议都非常感谢!你知道吗

编辑:我应该提一下

y = row

是我第一个尝试的。你知道吗

它给了我以下错误:

Traceback (most recent call last):
  File "C:\Users\ME\Downloads\Personal\Spectrometer\test\Spectrum3.py", line 42, in <module>
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
  File "C:\Python27\lib\site-packages\scipy\stats\_stats_mstats_common.py", line 92, in linregress
    ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
  File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 2432, in cov
    X = np.vstack((X, y))
  File "C:\Python27\lib\site-packages\numpy\core\shape_base.py", line 230, in vstack
    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

concentration数组和行的维度应该相同。你知道吗

如果我拉出一个列(我转置了specdt),linregresse的工作会非常出色。这是工作代码,如果这有帮助的话:

##take only column 26 or the values for 2268; print stuff
#Absorbance2268 = spectral_data[:, 25]

#print(Absorbance2268.shape)
#print(Absorbance2268)
#
##manual entry of concentration values + array info
#conc =[0,0,0,0,0,6,6,6,6,6,6,6,6,6,6,8,8,8,8,8,10,10,10,10,10,4,4,4,4,4]
#Concentration = numpy.asarray(conc)
#
#print(Concentration.shape)
#print(Concentration)
#
##performing linear regression.
#x = Concentration
#y = Absorbance2268
#
#slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
#
#print "r-squared:", r_value**2

Tags: inpynumpyforvaluelibstatsline
1条回答
网友
1楼 · 发布于 2024-04-25 02:02:54
for y in specdt:    # < -
    x = Concentration
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

for循环已经给出了行本身的内容。如果需要行索引,请使用

for row, y in enumerate(specdt):
    ...

相关问题 更多 >