2016-05-31 416 views
2

我這段代碼運行 '下' 擬合logistic模型數據:AttributeError的: 'numpy.ndarray' 對象沒有屬性

from sklearn import cross_validation 
import numpy as np 
import sys 
import pandas as pd 
import matplotlib.pyplot as plt 
from sklearn import datasets, svm, metrics 

digits = datasets.load_digits() 

X_train, X_test, y_train, y_test = cross_validation.train_test_split(
digits.data, digits.target, test_size=0.4, random_state=0) 

clf = linear_model.LogisticRegression(X_train, y_train) 
clf.fit(X_train, y_train) 

我收到此錯誤:

AttributeError: 'numpy.ndarray' object has no attribute 'lower'

如何使邏輯模型適合?

+1

歡迎來到SO!你能否提供一些關於錯誤的更多細節?錯誤發生在哪裏?哪條線?堆棧跟蹤的樣子是什麼? –

回答

1

您不應將X_trainy_train轉換爲LogisticRegression構造函數。您只需要

... 

clf = linear_model.LogisticRegression() 
clf.fit(X_train, y_train) 
+0

與「lower」相關的錯誤,大寫字母小寫,大概是因爲您將它應用於標準numpy數組而發生的,但它僅適用於字符串,請參閱[documentation](https://docs.python.org/ 2/library/string.html)或[字符數組](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.chararray.lower.html)。 – patrick

+0

@patrick我知道這一點。在OP代碼中,這個錯誤是由LogisticRegression對象的錯誤初始化引起的。 – kvorobiev

+0

我同意,我錯誤地發表了這個評論 - 它的目的是對原文的反應,增加你的解釋,而不是糾正它。 – patrick

相關問題