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

FLAV10

我有一个吸光度值矩阵,它是从一整套光谱中提取出来的。我称这个矩阵为“specdt”

每行代表特定波长下多个样本的值。我想根据称为“浓度”的单独浓度值数组找到回归的 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”变量;没有运气。

我怀疑这是我试图将行拉入 linregress 的 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

浓度数组和行的维度应该相同。

如果我拉出一列(我调换了specdt), linregress 工作得很好。这是工作代码,如果有帮助的话:

##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
肯尼特姆
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):
    ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 numpy 中的查找值进行数组操作

执行操作后如何从numpy数组中“删除”蒙版?

使用索引列表对 numpy 数组的元素执行操作

如何在J类上执行数组操作?

我将如何使用Dask对NumPy数组切片执行并行操作?

如何拆分numpy数组并在拆分数组上执行某些操作[Python]

使用numpy进行数组重分类

我如何有效地执行这种numpy数组操作?

如何在numpy数组和向量行之间执行包含操作?

如何在4D numpy数组上执行迭代2D操作

如何使用 numpy 进行有效的数组操作?

如何使用numpy方法根据另一个np数组的条件对一个np数组的某些行执行操作?

如何操作以下numpy.where数组

使用Numpy在Python中优化数组操作

在Numba的@guvectorize中使用NumPy数组操作

使用numpy数组元素进行操作

使用numpy sparsey数组优化操作

使用数组操作过滤numpy

如何使用numpy有效执行数十亿次的伯努利提取?

如何仅使用numpy操作基于其他两个numpy数组的条件获取新的numpy数组?

如何使用空数组执行Zip操作

numpy-使用先前值进行数组加法

使用argmax numpy Python进行数组格式化

Python:numpy,pandas,并对先前的数组值(平滑的平均值)执行操作:是否可以不使用FOR循环?EWMA?

如何使用数组索引numpy数组

使用CUDA内核进行数组操作

如何使用for循环运行数组并执行while循环

如何使用花式索引创建Numpy数组

如何使用内存索引访问 numpy 数组?