-1

I am trying to load columns 1 to 15 of the data.txt file into array X and column 16 into array y, and normalize all 15 columns in X in the for loop and array y in a single statement. Loading is working properly, but after trying to print results of normalization I get this error:

TypeError: 'tuple' object cannot be interpreted as an integer

Please help, the code is being done in python in the Jupyter notebook.

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('data.txt')

X = np.array(data[:, 1:16])
y = np.array(data[:, 16], ndmin=2).T


n = X.shape

for i in range(n):
    X[:, i] = (X[:, i]-np.min(X[:, i])) / (np.max(X[:, i])-np.min(X[:, i]))
y = ( y-np.min(y) ) / ( np.max(y)-np.min(y) )

print(X)
print(y)
1
  • 1
    The .shape of a 2-dimensional array has 2 elements. What i do you expect the loop to iterate over? Commented Sep 14, 2021 at 12:54

1 Answer 1

1

The problem is probably in the loop row for i in range(n): n is X shape, its a tuple, range needs integer as parameter.

Code example of your case:

n = (2,4)
for i in range(n):
    print(i)

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-277-3834a67eeb55> in <module>
      1 n = (2,4)
----> 2 for i in range(n):
      3     print(i)

TypeError: 'tuple' object cannot be interpreted as an integer

In your case, I think you want to iterate over columns, so n = X.shape[1] will fix it.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried but I get the same error coming within the loop and the next statement
TypeError Traceback (most recent call last) <ipython-input-42-c3e34738309b> in <module> 1 for i in range(n): ----> 2 X[:, i] = (X[:, i]-np.min(*X[:, i])) / (np.max(*X[:, i])-np.min(*X[:, i])) 3 y = ( y-np.min(*y) ) / ( np.max(*y)-np.min(*y) ) 4 5 print(X) <__array_function__ internals> in amin(*args, **kwargs)
Add the new code and new Error to the Question itself, It's not readable in comments.
What should I do when someone answers my question? stackoverflow.com/help/someone-answers. Decide if the answer is helpful, and then: -Vote on it - Accept it stackoverflow.com/help/accepted-answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.