TypeError only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

0 votes

I am working on a function that picks random elements from a training set. I divide the set indices into 11 bins, then create custom probabilities for them. 

bin_probs = [0.5, 0.3, 0.15, 0.04, 0.0025, 0.0025, 0.001, 0.001, 0.001, 0.001, 0.001]

X_train = list(range(2000000))

train_probs = bin_probs * int(len(X_train) / len(bin_probs)) # extend probabilities across bin elements
train_probs.extend([0.001]*(len(X_train) - len(train_probs))) # a small fix to match number of elements
train_probs = train_probs/np.sum(train_probs) # normalize
indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)
out_images = X_train[indices.astype(int)] # this is where I get the error

This is the error:

TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

What is the mistake I am doing here? Can someone help me solve this?

May 8, 2022 in Python by Kichu
• 19,050 points
2,338 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

+1 vote
0 answers

TypeError: only integer scalar arrays can be converted to a scalar index

import cv2 import numpy as np import matplotlib.pyplot as ...READ MORE

Dec 18, 2019 in Python by sandeep
• 130 points
7,189 views
0 votes
0 answers

TypeError: only size-1 arrays can be converted to Python scalars

This is my code for key in bboxes: bbox ...READ MORE

May 19, 2020 in Python by Rumia
• 120 points

edited May 19, 2020 by Gitika 1,330 views
0 votes
1 answer

Pyplot error "only size-1 arrays can be converted to Python scalars"

You can plot the chart by taking ...READ MORE

answered Jan 28, 2019 in Python by Omkar
• 69,210 points
9,170 views
0 votes
0 answers

Python error "only size-1 arrays can be converted to Python scalars"

I'm trying to plot the exponential and ...READ MORE

Jul 9, 2019 in Python by JTM
1,578 views
0 votes
0 answers

Pyplot error "only size-1 arrays can be converted to Python scalars"

I am getting the following error while ...READ MORE

Nov 28, 2019 in Python by Sumit
• 130 points
3,562 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
695 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,547 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP