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!
asked Mar 11, 2019 at 13:54
Ankit Kumar
1,31114 silver badges31 bronze badges
1 Answer 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
answered Mar 11, 2019 at 14:00
denis_lor
6,5574 gold badges34 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
denis_lor
@AnkitKumar In case it helped, mark the answer as accepted so anyone else with the same question can understand the answer worked out
Ankit Kumar
Ya, there's a time limit before which i can't accept it, I was just waiting for that
lang-py
print(y[0])and show us results. In general there's nothing wrong intargets=np.zeros([7000,1,10])andtargets[0,0,y[0]]=1. Just hardcode y[0] to try (for exampley=[[1,2,3,4,5],[1,2,3,4,5]]). The most likely the error is in readingyfrom file.print(y[0]). It gives me[6.]6,0,0,0....... The answer below works by the way, thanks!