2015-10-14 292 views
0

我是Django和scikit-learn的noob,嘗試使用這些技術創建一個簡單的REST服務器來執行分類。到目前爲止,我只是試圖獲得某種結果來測試控制器是否工作,但該程序似乎沒有檢測到我的LogisticRegression對象的任何屬性。LogisticRegression對象沒有屬性

我的代碼:

Request Method: \t POST 
 
Request URL: \t http://localhost:8000/classify/logistic_regression 
 
Django Version: \t 1.8.4 
 
Exception Type: \t AttributeError 
 
Exception Value: \t 
 
'LogisticRegression' object has no attribute 'fit' 
 
Exception Location: \t /Users/mart/myclassifier/classifierapi/views.py in post, line 31 
 
Python Executable: \t /Users/mart/myclassifier/myclassifiervenv/bin/python 
 
Python Version: \t 2.7.10 
 
Python Path: \t 
 
['/Users/mart/myclassifier', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python27.zip', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/plat-darwin', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/plat-mac', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/plat-mac/lib-scriptpackages', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/lib-tk', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/lib-old', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/lib-dynload', 
 
'/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
 
'/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
 
'/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
 
'/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
 
'/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
 
'/Users/mart/myclassifier/myclassifiervenv/lib/python2.7/site-packages'] 
 
Server time: \t Wed, 14 Oct 2015 02:54:32 +0000

from rest_framework.views import APIView 
 
from .mixins import JSONResponseMixin 
 
from django.http import HttpResponse 
 
import numpy as np 
 
from sklearn import svm 
 
from sklearn.linear_model import LogisticRegression 
 
import json 
 
import pickle 
 

 

 
class LogisticRegression(APIView): 
 

 
    def get(self, request): 
 
     
 
     return HttpResponse("Stub") 
 

 

 
    def post(self, request): 
 
     logreg = LogisticRegression() 
 

 
     array = '{"data":' + request.body + '}' 
 
     #print array 
 
     jobj= json.loads(array) 
 
    
 
     jarray = jobj['data'] 
 
     matrix = np.asarray([[j['GravityX'], j['GravityY'], j['GravityZ'], j['true']] for j in jarray]) 
 

 
     X = matrix[:, :3] 
 
     y = matrix[:, 3] 
 

 
     logreg.fit(X, y)  
 
     
 
     return HttpResponse("test")

而結果(使用郵差與請求主體中的僞數據JSON創建)

我嘗試了其他屬性,並得到相應的no屬性錯誤結果。有任何想法嗎?

回答

0

更改

from sklearn.linear_model import LogisticRegression

import sklearn.linear_model as lm

和使用

logreg = lm.LogisticRegression()

固定它。