TypeError:使用 ucimlrepo 时“NoneType”对象不可迭代

问题描述 投票:0回答:2

我想使用 UCI ML Repo 中的成人数据集

为此,我遵循页面中的“import in python”选项,它给出了以下代码:

pip install ucimlrepo

from ucimlrepo import fetch_ucirepo 
  
# fetch dataset 
adult = fetch_ucirepo(id=2) 
  
# data (as pandas dataframes) 
X = adult.data.features 
y = adult.data.targets 
  
# metadata 
print(adult.metadata) 
  
# variable information 
print(adult.variables) 

但这会引发以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/.../playground.ipynb Cell 3 line 4
      1 from ucimlrepo import fetch_ucirepo
      3 # fetch the adult dataset
----> 4 adult = fetch_ucirepo(id=2)
      6 # convert the dataset to a Pandas DataFrame
      7 df = pd.DataFrame(adult.data.features, columns=adult.variables.names, missing_values=["?"])

File ~/anaconda3/envs/myEnv/lib/python3.11/site-packages/ucimlrepo/fetch.py:148, in fetch_ucirepo(name, id)
    142 # alternative usage?: 
    143 # variables.age.role or variables.slope.description
    144 # print(variables) -> json-like dict with keys [name] -> details
    145 
    146 # make nested metadata fields accessible via dot notation
    147 metadata['additional_info'] = dotdict(metadata['additional_info'])
--> 148 metadata['intro_paper'] = dotdict(metadata['intro_paper'])
    150 # construct result object
    151 result = {
    152     'data': dotdict(data),
    153     'metadata': dotdict(metadata),
    154     'variables': variables
    155 }

TypeError: 'NoneType' object is not iterable

我知道我可以下载这个数据库,然后将其作为 pandas df 加载,但这更脏,因为然后我需要做一些额外的解析,而且它看起来不太好(首先加载成人.数据,然后添加特定的标头Adult.names 中的行(在每行中“:”之后分割所有内容之后...)

python dataset typeerror data-analysis
2个回答
0
投票

查看源代码,它本质上进行了以下 API 调用

https://archive.ics.uci.edu/api/dataset?id=2
,并返回

{'status': 200,
 'statusText': 'OK',
 'data': {'uci_id': 2,
  'name': 'Adult',
  'repository_url': 'https://archive.ics.uci.edu/dataset/2/adult',
  'data_url': 'https://archive.ics.uci.edu/static/public/2/data.csv',
  'abstract': 'Predict whether income exceeds $50K/yr based on census data. Also known as "Census Income" dataset. ',
  'area': 'Social Science',
  'tasks': ['Classification'],
  'characteristics': ['Multivariate'],
  'num_instances': 48842,
  'num_features': 14,
  'feature_types': ['Categorical', 'Integer'],
  'demographics': ['Age', 'Income', 'Education Level', 'Other', 'Race', 'Sex'],
  'target_col': ['income'],
  'index_col': None,
  'has_missing_values': 'yes',
  'missing_values_symbol': 'NaN',
  'year_of_dataset_creation': 1996,
  'last_updated': 'Mon Aug 07 2023',
  'dataset_doi': '10.24432/C5XW20',
  'creators': ['Barry Becker', 'Ronny Kohavi'],
  'intro_paper': None,

   ...

请注意,

intro_paper
None
,这就是为什么您会得到
TypeError: 'NoneType' object is not iterable


0
投票

似乎有些数据集已被弃用,但他们没有更新示例代码。

from ucimlrepo import fetch_ucirepo, list_available_datasets
list_available_datasets()

fetch_ucirepo(id=17)

但是仍然有一些数据集可用。有机会的话可以尝试一下其他的。

编辑:

实际上,我在 https://github.com/uci-ml-repo/ucimlrepo/blob/main/src/ucimlrepo/fetch.py 检查了他们的存储库,并且您没有收到错误。因此,如果您分叉存储库,并编辑/删除以下部分,您就可以解决您的问题。

metadata['additional_info'] = dotdict(metadata['additional_info'])
metadata['intro_paper'] = dotdict(metadata['intro_paper'])
© www.soinside.com 2019 - 2024. All rights reserved.