python - 如何使用 scikit-learn 将数据转换为适合多类分类任务的格式?

标签 python machine-learning scikit-learn

我正在尝试使用 scikit-learn 对从 Ebay API 获得的数据进行预测。我想知道将此数据转换为可用于 scikit-learn 多类分类问题的格式的最佳方法。它解释在 scikit-learn 网站上导入外部数据的唯一方法似乎是从 svmlight 文件加载,如下所示:

http://scikit-learn.org/stable/datasets/

我想在多种算法上使用这些数据,而不仅仅是 SVM。我使用以下代码将数据存储在 dict 对象中:

from ebaysdk.finding import Connection as Finding
from requests.exceptions import ConnectionError

try:
    api = Finding(appid="my_app_id")
    api_request = {'keywords': 'Samsung Galaxy S5 G900R4 16GB',     'itemFilter': [{'name': 'SoldItemsOnly',
 'value': 'true'}], 'outputSelector': 'SellerInfo', 'GLOBAL-ID': 'EBAY-      GB'}
    response = api.execute('findCompletedItems', api_request)
    #print(response.dict())
except ConnectionError as e:
    print(e)
    print(e.response.dict())

我已经在线搜索了教程或如何执行此操作的解释,但我找不到任何有用的内容,其中详细介绍了如何将数据转换为我需要的格式以及到底是什么格式需要。

请有人给我一些指导,告诉我是否应该使用 svmlight 文件来实现这一目标以及如何去做,或者是否有任何其他关于导入数据的建议。我是机器学习的新手,因为以及 Python 和 scikit-learn,因此我们将不胜感激。

以下是原始数据格式的示例:

{'autoPay': 'true',
  'condition': {'conditionDisplayName': 'Used', 'conditionId': '3000'},
  'country': 'US',
  'galleryURL': 'http://thumbs4.ebaystatic.com/m/mO-HwGeodkgYX6sRbPyFsrg/140.jpg',
  'globalId': 'EBAY-US',
  'isMultiVariationListing': 'false',
  'itemId': '201265198351',
  'listingInfo': {'bestOfferEnabled': 'false',
   'buyItNowAvailable': 'false',
   'endTime': '2015-01-16T00:23:19.000Z',
   'gift': 'false',
   'listingType': 'StoreInventory',
   'startTime': '2015-01-15T15:47:38.000Z'},
  'location': 'Dandridge,TN,USA',
  'paymentMethod': 'PayPal',
  'postalCode': '37725',
  'primaryCategory': {'categoryId': '9355',
   'categoryName': 'Cell Phones & Smartphones'},
  'productId': {'_type': 'ReferenceID', 'value': '182557948'},
  'returnsAccepted': 'true',
  'sellerInfo': {'feedbackRatingStar': 'Turquoise',
   'feedbackScore': '445',
   'positiveFeedbackPercent': '100.0',
   'sellerUserName': 'dadscritter15'},
  'sellingStatus': {'convertedCurrentPrice': {'_currencyId': 'USD',
    'value': '279.99'},
   'currentPrice': {'_currencyId': 'USD', 'value': '279.99'},
   'sellingState': 'Ended'},
  'shippingInfo': {'expeditedShipping': 'true',
   'handlingTime': '2',
   'oneDayShippingAvailable': 'false',
   'shipToLocations': ['US',
    'CA',
    'GB',
    'AU',
    'AT',
    'BE',
    'FR',
    'DE',
    'IT',
    'JP',
    'ES',
    'CH',
    'NL',
    'CN',
    'HK',
    'MX'],
   'shippingServiceCost': {'_currencyId': 'USD', 'value': '0.0'},
   'shippingType': 'FlatDomesticCalculatedInternational'},
  'title': 'Samsung Galaxy S5 SM-G900R4 (Latest Model) 16GB White (U.S. Cellular) Verify ESN',
  'topRatedListing': 'false',
  'viewItemURL': 'http://www.ebay.com/itm/Samsung-Galaxy-S5-SM-G900R4-Latest-Model-16GB-White-U-S-Cellular-Verify-ESN-/201265198351?pt=LH_DefaultDomain_0'},
 {'autoPay': 'true',
  'condition': {'conditionDisplayName': 'Used', 'conditionId': '3000'},
  'country': 'US',
  'galleryURL': 'http://thumbs4.ebaystatic.com/m/mO-HwGeodkgYX6sRbPyFsrg/140.jpg',
  'globalId': 'EBAY-US',
  'isMultiVariationListing': 'false',
  'itemId': '201265198351',
  'listingInfo': {'bestOfferEnabled': 'false',
   'buyItNowAvailable': 'false',
   'endTime': '2015-01-16T00:23:19.000Z',
   'gift': 'false',
   'listingType': 'StoreInventory',
   'startTime': '2015-01-15T15:47:38.000Z'},
  'location': 'Dandridge,TN,USA',
  'paymentMethod': 'PayPal',
  'postalCode': '37725',
  'primaryCategory': {'categoryId': '9355',
   'categoryName': 'Cell Phones & Smartphones'},
  'productId': {'_type': 'ReferenceID', 'value': '182557948'},
  'returnsAccepted': 'true',
  'sellerInfo': {'feedbackRatingStar': 'Turquoise',
   'feedbackScore': '445',
   'positiveFeedbackPercent': '100.0',
   'sellerUserName': 'dadscritter15'},
  'sellingStatus': {'convertedCurrentPrice': {'_currencyId': 'USD',
    'value': '279.99'},
   'currentPrice': {'_currencyId': 'USD', 'value': '279.99'},
   'sellingState': 'Ended'},
  'shippingInfo': {'expeditedShipping': 'true',
   'handlingTime': '2',
   'oneDayShippingAvailable': 'false',
   'shipToLocations': ['US',
    'CA',
    'GB',
    'AU',
    'AT',
    'BE',
    'FR',
    'DE',
    'IT',
    'JP',
    'ES',
    'CH',
    'NL',
    'CN',
    'HK',
    'MX'],
   'shippingServiceCost': {'_currencyId': 'USD', 'value': '0.0'},
   'shippingType': 'FlatDomesticCalculatedInternational'},
  'title': 'Samsung Galaxy S5 SM-G900R4 (Latest Model) 16GB White (U.S. Cellular) Verify ESN',
  'topRatedListing': 'false',
  'viewItemURL': 'http://www.ebay.com/itm/Samsung-Galaxy-S5-SM-G900R4-Latest-Model-16GB-White-U-S-Cellular-Verify-ESN-/201265198351?pt=LH_DefaultDomain_0'}

最佳答案

如果您有一个字典列表,其中一个 json/dict 代表您可以执行的产品:

>>> df = pd.DataFrame([dict1, dict2])
>>> df
autoPay condition   country galleryURL  globalId    isMultiVariationListing itemId  listingInfo location    paymentMethod   postalCode  primaryCategory productId   returnsAccepted sellerInfo  sellingStatus   shippingInfo    title   topRatedListing viewItemURL
0   true    {u'conditionId': u'3000', u'conditionDisplayNa...   US  http://thumbs4.ebaystatic.com/m/mO-HwGeodkgYX6...   EBAY-US false   201265198351    {u'listingType': u'StoreInventory', u'gift': u...   Dandridge,TN,USA    PayPal  37725   {u'categoryId': u'9355', u'categoryName': u'Ce...   {u'_type': u'ReferenceID', u'value': u'1825579...   true    {u'feedbackRatingStar': u'Turquoise', u'positi...   {u'currentPrice': {u'_currencyId': u'USD', u'v...   {u'expeditedShipping': u'true', u'shipToLocati...   Samsung Galaxy S5 SM-G900R4 (Latest Model) 16G...   false   http://www.ebay.com/itm/Samsung-Galaxy-S5-SM-G...
1   false   {u'conditionId': u'3000', u'conditionDisplayNa...   US  http://thumbs4.ebaystatic.com/m/mO-HwGeodkgYX6...   EBAY-US false   201265198351    {u'listingType': u'StoreInventory', u'gift': u...   Dandridge,TN,USA    PayPal  37725   {u'categoryId': u'9355', u'categoryName': u'Ce...   {u'_type': u'ReferenceID', u'value': u'1825579...   true    {u'feedbackRatingStar': u'Turquoise', u'positi...   {u'currentPrice': {u'_currencyId': u'USD', u'v...   {u'expeditedShipping': u'true', u'shipToLocati...   Samsung Galaxy S5 SM-G900R4 (Latest Model) 16G...   false   http://www.ebay.com/itm/Samsung-Galaxy-S5-SM-G...

然后您可以使用此 df 的列作为模型的输入。您可能想提取嵌套字典中的数据。

关于python - 如何使用 scikit-learn 将数据转换为适合多类分类任务的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181197/

相关文章:

python-3.x - 选择反射(reflect) python 中每个结果类别影响因素的分类模型

python - 为现有的 python 模块编写扩展

python - Sqlalchemy+ Elixir : How query with a ManyToMany relationship?

python - Qubole 中的宽数据 PySpark 机器学习

machine-learning - 使用 Keras 的 CIFAR-10 数据集

python - 如何根据 Scikit Learn 决策树中的分类变量拆分节点?

python 扭曲: retrieve a deferred's execution time

python - 我如何在Python中获得一个随机日期以及3天后的日期?

python - 神经网络的输入大小不匹配

python - Balanced_accuracy 不是 scikit-learn 中的有效评分值