TypeError:切片索引必须是整数或无或具有索引方法

时间:2017-06-10 08:44:54

标签: python arrays

我遇到如下所示的错误

错误

Traceback (most recent call last):

File "/Users/tej/Desktop/Allfiles/TSAlgorithm/Rots/slidingwindow.py", line 41, in <module>

    segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error)

  File "/Users/tej/Desktop/Allfiles/TSAlgorithm/Rots/segment.py", line 29, in slidingwindowsegment

    result_segment = create_segment(sequence,(seq_range[0],seq_range[1]))

  File "/Users/tej/Desktop/Allfiles/TSAlgorithm/Rots/fit.py", line 15, in regression

    p, error = leastsquareslinefit(sequence,seq_range)

  File "/Users/tej/Desktop/Allfiles/TSAlgorithm/Rots/wrappers.py", line 10, in leastsquareslinefit

    y = array(sequence[seq_range[0]:seq_range[1]+1])

TypeError: slice indices must be integers or None or have an __index__ method

代码

from numpy import arange, array, ones
from numpy.linalg import lstsq

def leastsquareslinefit(sequence,seq_range):

    """Return the parameters and error for a least squares line fit of one segment of a sequence"""

    x = arange(seq_range[0],seq_range[1]+1)
    y = array(sequence[seq_range[0]:seq_range[1]+1])
    A = ones((len(x),2),float)
    A[:,0] = x
    (p,residuals,rank,s) = lstsq(A,y)
    try:
        error = residuals[0]
    except IndexError:
        error = 0.0
    return (p,error)

错误行是

y = array(sequence[seq_range[0]:seq_range[1]+1])

sequence = [2.07879, 1.10188, 0.568894, 0.394302, 0.794052, 0.428718, 0.47567, 0.452418, 0.426734, 0.768852, 0.870319, 1.30586, 0.380166, 0.569896, 0.756903, 0.586858, 0.641127, 0.692155, 1.03697, 1.03722, 1.56939, 0.281659, 0.312457, 1.33759, 1.70201, 1.35018, 1.87431, 1.65084, 1.1157, 0.562875, 0.544611, 0.556123, 0.973077, 0.535477, 0.644177, 0.784439, 0.668507, 0.58048, 0.761036, 1.27403, 0.502488, 0.383824, 0.635096, 0.553752, 0.620955, 0.727158, 0.579525, 0.912165]

seq_range = (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0)

我读取序列和seq_range

的程序
def slidingwindowsegment(sequence, create_segment, compute_error, max_error, seq_range=None):

    if not seq_range:

        f=0.1
        a=0
        b=len(sequence)-1
        seq_range=[x * f for x in range(a, b)]
        seq_range=tuple(seq_range)

        #seq_range = (0,len(sequence)-1)
        #print (type(seq_range))

    start = seq_range[0]
    end = start
    result_segment = create_segment(sequence,(seq_range[0],seq_range[1]))
    while end < seq_range[1]:
        end += 1
        test_segment = create_segment(sequence,(start,end))
        error = compute_error(sequence,test_segment)
        if error <= max_error:
            result_segment = test_segment
        else:
            break

    if end == seq_range[1]:
        return [result_segment]
    else:
        return [result_segment] + slidingwindowsegment(sequence, create_segment, compute_error, max_error, (end-1,seq_range[1]))

1 个答案:

答案 0 :(得分:0)

您的代码行说您正在从sequence索引到0.0访问0.5+1 = 1.5的元素。这显然是不可能的,因为0.01.5不是整数值