tim
tim

Reputation: 10176

Python/Numpy: Division gives me an unexpected deprecation-Warning

Im reading data from a csv, then looping it, then I want to divide it by the mean value to normalize it but getting a warning. The code is:

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])

the train.txt looks like this:

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

The warnings:

    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

What is the problem? Thanks

EDIT Okay that was a pretty fast fix myself: The j has got to be of integer type. I fixed it like this:

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

good solution like this? Im new to python...

Upvotes: 2

Views: 4551

Answers (1)

tim
tim

Reputation: 10176

Okay that was a pretty fast fix myself: The j has got to be of integer type. I fixed it like this:

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

using the python range function OR explicitely defining the data-type for arange like this (thanks @Davidmh):

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

Upvotes: 3

Related Questions