Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 如何使用numpy对行数组执行操作?_Python_Arrays_Numpy_Matrix - Fatal编程技术网

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

Python 如何使用numpy对行数组执行操作?,python,arrays,numpy,matrix,Python,Arrays,Numpy,Matrix,我有一个吸光度值矩阵,我从整个光谱中提取。我称之为矩阵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) reg

我有一个吸光度值矩阵,我从整个光谱中提取。我称之为矩阵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
这是我尝试的第一件事

它给了我以下错误:

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

光谱?光谱是光谱的复数。哈哈,谢谢。第1部分,共2部分:PAssuming specdt是一个numpy数组或矩阵,那么row已经是您想要的行了,而不是该行的索引,因此y=specdt[row,:]应该只是y=row。谢谢Chris,我已经编辑了,以显示我到目前为止所做的工作。感谢Kenny花时间。不幸的是,这也给出了错误:文件blah blah,第230行,在vstack return _nx.concatenate[catenate_2d_m for _min tup],0 ValueError:除连接轴之外的所有输入数组维度都必须匹配exactly@FLAV10请在问题中显示完整的堆栈跟踪。诸如此类的文件名是真的吗?@FLAV10看起来你的y应该是列,而不是行?我不这么认为。我没有拉一个特定的列,而是转置了整个数据集:specdt=spectrum_data。我是不是遗漏了什么?这很有趣。果然是41和42;不匹配。我通过计数进行了三次检查,而不仅仅是打印形状。我知道我现在打错了。您的解决方案非常有效。非常感谢你!!!!
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
##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 row, y in enumerate(specdt):
    ...