【问题标题】:python IndexError: boolean index did not match indexed array along dimension 0; dimension is 32 but corresponding boolean dimension is 112python IndexError:布尔索引与维度0的索引数组不匹配;维度为 32,但对应的布尔维度为 112
【发布时间】:2019-11-19 02:33:19
【问题描述】:

我是 matploblib 和 numpy 的新手,在尝试提取数据时遇到了问题。以下代码导致 IndexError: boolean index did not match indexed array along dimension 0;维度是 32,但对应的布尔维度是 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 numpy


    【解决方案1】:

    你这里有几个问题..

    第一个不要使用 python 关键字作为变量 将其更改为

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

    这个

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

    您的错误是您尝试将具有 112 个值(数据)的布尔数组与 32 个元素数组(汽车)进行比较。所以您的代码应该像这样更改

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

    使用 Pandas 之类的分析库进行基本分析比使用 numpy 更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2020-10-03
      • 2021-08-31
      • 1970-01-01
      • 2019-06-18
      • 2017-11-17
      • 2018-04-01
      相关资源
      最近更新 更多