【问题标题】:How to convert data in to a format suitable to be used for a multi-class classification task, using scikit-learn?如何使用 scikit-learn 将数据转换为适合用于多类分类任务的格式?
【发布时间】:2015-01-28 06:55:52
【问题描述】:

我正在尝试使用 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'}

【问题讨论】:

  • svmlight 文件格式之所以这样调用,是因为它遵循svmlight 程序的约定。这并不限制您使用哪种算法。
  • 所以我想这可能是我需要使用的格式,谢谢。

标签: python machine-learning scikit-learn


【解决方案1】:

如果您有一个 dicts 列表,其中一个 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 的列作为模型的输入。您可能想提取嵌套字典中的数据。

【讨论】:

  • 为了实现多类分类问题的格式,我不需要某种方式来表示每个实例的目标值吗?我怎么能用这个解决方案做到这一点?目标值将是拍卖的最终价格。
  • 价格在嵌套字典之一中,因此您还需要进行一些预处理才能在数据框的列中获取价格。顺便说一句,价格是一个连续变量,所以你想做回归而不是多类分类。不幸的是,获取一些数据并把它交给 sklearn 并不容易。
  • 我正在考虑将价格转换为价格范围,以将它们用于多类分类。如果我要将价格范围添加为一列,sklearn 怎么知道目标是哪一列?
  • 这是有道理的。通常你会使用model.fit(X, y)。在这种情况下y=df['Price_column_name'].
  • 好吧,只是为了确认一下,X 将是一个包含所有其他属性的列的 DataFrame,而 y 将是一个单独的 DataFrame,仅包含 X 中表示的所有实例的目标值的单个列?
猜你喜欢
  • 2015-03-07
  • 2013-06-05
  • 2012-05-18
  • 2016-04-22
  • 2020-01-04
  • 2014-11-19
  • 2018-04-04
相关资源
最近更新 更多