[英]IndexError: boolean index did not match indexed array along dimension 1; dimension is 3 but corresponding boolean dimension is 10
[英]python IndexError: boolean index did not match indexed array along dimension 0; dimension is 32 but corresponding boolean dimension is 112
我是 matploblib 和 numpy 的新手,并且在尝试提取数据时遇到了问题。 以下代码导致 IndexError: boolean 索引与维度 0 上的索引数组不匹配; 尺寸为 32,但对应的 boolean 尺寸为 112。请指教!!
使用的数据集: 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)
你这里有几个问题..
第一个不要使用 python 关键字作为变量将其更改为
type = np.unique(data['type'])
这个
types = np.unique(data['type'])
您的错误是您正在尝试将具有 112 个值(数据)的 boolean 数组与 32 个元素数组(汽车)进行比较。所以您的代码应该像这样更改
cars = data[data["category"]=="Cars"]
carspetrol = cars[cars["type"]=="Petrol"]["number"]
并且最好使用 Pandas 之类的分析库进行基本分析,而不是 numpy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.