![](/img/trans.png)
[英]Training logistic regression using scikit learn for multi-class classification
[英]How to convert data in to a format suitable to be used for a multi-class classification task, using 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的列用作模型的輸入。 您可能要提取嵌套字典中的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.