Rich Gray
Rich Gray

Reputation: 201

How to convert data in to a format suitable to be used for a multi-class classification task, using scikit-learn?

I am trying to use scikit-learn to make predictions on data that I have obtained from the Ebay API. I want to know the best way to go about converting this data in to a format which can be used for a multi-class classification problem with scikit-learn. The only way it explains of importing external data on the scikit-learn website seems to be to load from an svmlight file, shown here:

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

I want to use the data on a number of algorithms and not just an SVM. I have the data in a dict object using the following code:

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())

I have searched online for tutorials or an explanation of how to go about doing this, but I can't find anything useful, which goes in to enough detail on how to convert the data to the format I require and what exactly that format needs to be.

Please can someone give me some guidance on if I should use an svmlight file to achieve this and how to go about doing it, or if there are any other suggestions for importing my data in. I am new to Machine Learning, as well as Python and scikit-learn, so any help is appreciated.

Here is an example of the format that the raw data is in:

{'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'}

Upvotes: 0

Views: 735

Answers (1)

elyase
elyase

Reputation: 40973

If you have a list of dicts, with one json/dict representing a product you can do:

>>> 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...

Then you can use the columns of this df as input to your model. You probably want to extract the data in the nested dicts.

Upvotes: 1

Related Questions