1

I have a code that goes as:-

.....
path = 'path_to_csv_file';
file=open(path, "r")
reader = csv.reader(file)
y=np.empty((7000,1))
j=0
for line in reader:
    y[j]=line[0]
    j+=1

....

targets=np.zeros([7000,1,10])

Now, in the first array of targets, I want the y[0]th index to store 1 (y[0] stores integers from 0-9). For that, I wrote:-

targets[0,0,y[0]]=1

But I get an error:-

IndexError: arrays used as indices must be of integer (or boolean) type

When I print y[0], I get:-

[6.]

as the output. What I think is that it's not an integer, so that's probably the source of my error, but I don't know how to fix it. Any help will be appreciated. Thanks!

4
  • Use print(y[0]) and show us results. In general there's nothing wrong in targets=np.zeros([7000,1,10]) and targets[0,0,y[0]]=1. Just hardcode y[0] to try (for example y=[[1,2,3,4,5],[1,2,3,4,5]]). The most likely the error is in reading y from file. Commented Mar 11, 2019 at 13:59
  • @Karls I've already shown the result for print(y[0]). It gives me [6.] Commented Mar 11, 2019 at 14:00
  • Ah ok, I see. And what is the content of your csv file? Commented Mar 11, 2019 at 14:02
  • @Karls It goes like 6,0,0,0....... The answer below works by the way, thanks! Commented Mar 11, 2019 at 14:04

1 Answer 1

1

Have you tried with dtype=int?

y=np.empty((7000,1), dtype=int)
...
targets=np.zeros(([7000,1,10]), dtype=int)

You can check more on the documentation on the usage of numpty.empty and numpty.zeros

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

2 Comments

@AnkitKumar In case it helped, mark the answer as accepted so anyone else with the same question can understand the answer worked out
Ya, there's a time limit before which i can't accept it, I was just waiting for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.