LogisticRegression对象没有属性

时间:2015-10-14 03:14:34

标签: django scikit-learn logistic-regression

我是Django和scikit的小伙伴 - 学习尝试使用这些技术创建一个简单的REST服务器来执行分类。到目前为止,我只是试图获得某种结果来测试控制器的工作情况,但程序似乎没有检测到任何LogisticRegression对象的属性。

我的代码:

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

结果(使用Postman在请求体中使用虚拟数据JSON创建):

Request Method:	POST
Request URL:	http://localhost:8000/classify/logistic_regression
Django Version:	1.8.4
Exception Type:	AttributeError
Exception Value:	
'LogisticRegression' object has no attribute 'fit'
Exception Location:	/Users/mart/myclassifier/classifierapi/views.py in post, line 31
Python Executable:	/Users/mart/myclassifier/myclassifiervenv/bin/python
Python Version:	2.7.10
Python Path:	
['/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:	Wed, 14 Oct 2015 02:54:32 +0000

我尝试了其他属性,并且相应的无属性错误结果。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

更改

from sklearn.linear_model import LogisticRegression

import sklearn.linear_model as lm

并使用

logreg = lm.LogisticRegression()

修好了。