Python最小二乘估计错误

时间:2018-05-18 18:49:13

标签: python

我有一个数据集,我试图自动分析。数据是一系列实验,我需要得到图的斜率。

示例数据集: Sample data set

除了数据之外,我还有一个标志,指示我试图方便分析的每条曲线的起点(名为CycleVec)。

要查找斜率,我使用了以下代码:

CycleVev - 系统周期列表,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2 ....等等

CycleValue - 我想要分析的周期数

#+3 to cut off the first 3 data points to ignore occasional data error
startIndex = CycleVec.index(CycleValue)+3 

#-20 to cut off the last 20 data points to ignore recorded noise
endIndex = len(CycleVec) - 1 - CycleVec[::-1].index(CycleValue)-20 

ShortTime = TimeVec[startIndex:endIndex]
ShortForce = ForceVec[startIndex:endIndex]

A_Matrix = np.vstack([ShortTime,np.ones(len(ShortTime))]).T
mVal , cVal = np.linalg.lstsq(A_Matrix,ShortForce,rcond)[0]

此代码似乎可以正常工作以获得单个循环斜率。然后,我尝试将其包装成for循环,如下所示:

maxCycles = int(max(CycleVec))

mVector = []
cVector = []

for CycleValue in range(1, maxCycles-1):
    #CycleValue = 22
    startIndex = CycleVec.index(CycleValue)+3
    endIndex = len(CycleVec) - 1 - CycleVec[::-1].index(CycleValue)-20

    ShortTime = TimeVec[startIndex:endIndex]
    ShortForce = ForceVec[startIndex:endIndex]

    A_Matrix = np.vstack([ShortTime,np.ones(len(ShortTime))]).T
    mVal , cVal = np.linalg.lstsq(A_Matrix,ShortForce,rcond)[0]
    mVector.append(mVal)
    cVector.append(cVal)

这打算创建两个斜率和偏移量列表(mVector和cVector)。 (抵消我没有使用过,只是为了完整性而这样做)

当我运行时,我得到一系列错误:

 mVal , cVal = np.linalg.lstsq(A_Matrix,ShortForce,rcond)[0]
 File "C:\Users\Morgan\AppData\Roaming\Python\Python36\site-packages\numpy\linalg\linalg.py", line 1978, in lstsq
_assertNoEmpty2d(a, b)  # TODO: relax this constraint
 File "C:\Users\Morgan\AppData\Roaming\Python\Python36\site-packages\numpy\linalg\linalg.py", line 225, in _assertNoEmpty2d
raise LinAlgError("Arrays cannot be empty")
numpy.linalg.linalg.LinAlgError: Arrays cannot be empty

我不确定该怎么做。我以类似的方式填充了阵列,没有任何问题。有人可以帮忙吗?这是我第一次尝试编写python,虽然我是matlab资深人士。

0 个答案:

没有答案