Python / Numpy:Division给了我一个意想不到的弃用 - 警告

时间:2014-06-04 12:35:34

标签: python numpy types warnings

我正在读取csv中的数据,然后循环它,然后我想将它除以平均值来规范化它但得到警告。代码是:

A = genfromtxt("train.txt", delimiter=';', skip_header=1)
lowid = A[:,1].min(axis=0)
highid = A[:,1].max(axis=0)
X = [] 
Y = []
for i in np.arange(lowid, highid):
    I = A[A[:,1] == i][:, [0,2,3]]
    meanp = np.mean(I[:,1]);
    meanq = np.mean(I[:,2]);

    for j in np.arange(I[:,0].min(axis=0)+2, I[:,0].max(axis=0)):
        weekday = int(I[j,0]) % 7

        # NORMALIZE:
        P   = I[j,1]   / meanp
        pP  = I[j-1,1] / meanp
        ppP = I[j-2,1] / meanp
        X.append([weekday, P, pP, ppP])
        Y.append(I[j,2])

train.txt看起来像这样:

day;itemID;price;quantity
1;1;4.73;6
1;2;7.23;0
1;3;10.23;1
1;4;17.9;0
1;5;1.81;1
1;6;12.39;1
1;7;7.17;1
1;8;7.03;0
1;9;13.61;0
1;10;36.45;1
1;11;24.67;0
1;12;12.04;0
1;13;11.85;0

警告:

    weekday = int(I[j,0]) % 7
DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

    P   = I[j,1]   / meanp
DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

    pP  = I[j-1,1] / meanp
DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

    ppP = I[j-2,1] / meanp
DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

    Y.append(I[j,2])
DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

有什么问题? 感谢

编辑好的,这是一个非常快速的解决方案: j必须是整数类型。我这样修好了:

for j in range(int(I[:,0].min(axis=0))+2, int(I[:,0].max(axis=0))):     
像这样的好解决方案?我是python的新手......

1 个答案:

答案 0 :(得分:3)

好吧,这是一个非常快速的解决方案:j必须是整数类型。 我这样修好了:

for j in range(int(I[:,0].min(axis=0))+2, int(I[:,0].max(axis=0))):   

使用python range函数或明确定义arange的数据类型(感谢@Davidmh):

for j in np.arange(I[:,0].min(axis=0)+2, I[:,0].max(axis=0), dtype=np.int):