1

i am new to matploblib and numpy and have faced issues trying to extracting the data. the following codes results in IndexError: boolean index did not match indexed array along dimension 0; dimension is 32 but corresponding boolean dimension is 112. pls advise!!

dataset used: https://data.gov.sg/dataset/monthly-motor-vehicle-population-by-type-of-fuel-used

import numpy as np
import matplotlib.pyplot as plt
title = "motor-vehicle-population-statistics-by-type-of-fuel-used."
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt("data/motor-vehicle-population-statistics-by-type-of-fuel-used.csv",
                      dtype=("datetime64[Y]","U100","U110",int),
                      delimiter=",",
                      names=True)

years = np.unique(data["month"])
category = np.unique(data['category'])
type = np.unique(data['type'])

cars = data[data["category"]=="Cars"]["number"]
carspetrol = cars[data["type"]=="Petrol"]["number"]
# print(cars)
print(carspetrol)
1
  • In my experience, whenever the whole error string indicated in the title of this question is raised (IndexError: boolean index did not match indexed array along dimension 0; dimension is X but corresponding boolean dimension is Y), it means you are trying to do an operation which requires two arrays to have the same dimensions, but they don't. Commented Jul 9, 2023 at 14:39

1 Answer 1

2

you have few issues here..

first one don't use python keywords as variables change this

type = np.unique(data['type'])

to this

types = np.unique(data['type'])

your error is you are trying to compare a boolean array which has 112 values (data) with a 32-elements array (cars). So your code should change like this

cars = data[data["category"]=="Cars"]
carspetrol = cars[cars["type"]=="Petrol"]["number"]

furthermore, it is better to use analytical library like Pandas to do the basic analytics than numpy.

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

Comments

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.