简体   繁体   中英

python IndexError: boolean index did not match indexed array along dimension 0; dimension is 32 but corresponding boolean dimension is 112

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)

you have few issues here..

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

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

this

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

your error is you are trying to compare boolean array which have 112 values(data) with 32 element array(cars).. so your code should change like this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM