python - Sklearn LogisticRegressionCV 的类似数组的输入

标签 python pandas scikit-learn logistic-regression

最初,我从 .csv 中读取数据文件,但在这里我从列表构建数据框,以便可以重现问题。目的是使用 LogisticRegressionCV 训练具有交叉验证的逻辑回归模型。 .

indeps = ['M', 'F', 'M', 'F', 'M', 'M', 'F', 'M', 'M', 'F', 'F', 'F', 'F', 'F', 'M', 'F', 'F', 'F', 'F', 'F', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M', 'F', 'F', 'F', 'M', 'F', 'M', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'M', 'M', 'M', 'F', 'M', 'M', 'F', 'F']
dep = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

data = [indeps, dep] 
cols = ['state', 'cat_bins']

data_dict = dict((x[0], x[1]) for x in zip(cols, data))

df = pd.DataFrame.from_dict(data_dict)
df.tail()

    cat_bins    state
45  0.0           F
46  0.0           M
47  0.0           M
48  0.0           F
49  0.0           F


'''Use Pandas' to encode independent variables.  Notice that
 we are returning a sparse dataframe '''

def heat_it2(dataframe, lst_of_columns):
    dataframe_hot = pd.get_dummies(dataframe,
                                   prefix = lst_of_columns,
                                   columns = lst_of_columns, sparse=True,)
    return dataframe_hot

train_set_hot = heat_it2(df, ['state'])
train_set_hot.head(2)

    cat_bins    state_F     state_M
0     1.0         0            1
1     1.0         1            0

'''Use the dataframe to set up the prospective inputs to the model as numpy arrays'''

indeps_hot = ['state_F', 'state_M']

X = train_set_hot[indeps_hot].values
y = train_set_hot['cat_bins'].values

print 'X-type:', X.shape, type(X)
print 'y-type:', y.shape, type(y)
print 'X has shape, is an array and has length:\n', hasattr(X, 'shape'), hasattr(X, '__array__'), hasattr(X, '__len__')
print 'yhas shape, is an array and has length:\n', hasattr(y, 'shape'), hasattr(y, '__array__'), hasattr(y, '__len__')
print 'X does have attribute fit:\n',hasattr(X, 'fit')
print 'y does have attribute fit:\n',hasattr(y, 'fit')

X-type: (50, 2) <type 'numpy.ndarray'>
y-type: (50,) <type 'numpy.ndarray'>
X has shape, is an array and has length:
True True True
yhas shape, is an array and has length:
True True True
X does have attribute fit:
False
y does have attribute fit:
False

因此,回归器的输入似乎具有 .fit 的必要属性方法。它们是 具有正确形状的 numpy 数组X是维度为 [n_samples, n_features] 的数组, 和 y是一个形状为 [n_samples,] 的向量这是文档:

fit(X, y, sample_weight=None)[source]

Fit the model according to the given training data.
Parameters: 

X : {array-like, sparse matrix}, shape (n_samples, n_features)

    Training vector, where n_samples is the number of samples and n_features is the number of features.
  y : array-like, shape (n_samples,)

Target vector relative to X.

....

现在我们尝试拟合回归量:

logmodel = LogisticRegressionCV(Cs =1, dual=False , scoring = accuracy_score, penalty = 'l2')
logmodel.fit(X, y)

...

    TypeError: Expected sequence or array-like, got estimator LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
    intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
    penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
    verbose=0, warm_start=False)

错误消息的来源似乎在 scikits 的 validation.py 模块中,here .

引发此错误消息的唯一代码部分是以下函数片段:

def _num_samples(x):
    """Return number of samples in array-like x."""
    if hasattr(x, 'fit'):
        # Don't get num_samples from an ensembles length!
        raise TypeError('Expected sequence or array-like, got '
                        'estimator %s' % x)
    etc.

问题:由于我们用来拟合模型的参数(Xy)不具有“fit”属性,为什么会出现此错误消息

在带有 scikit-learn 18.01-3 和 pandas 0.19.2-2 的 Canopy 1.7.4.3348(64 位)上使用 python 2.7

谢谢你的帮助:)

最佳答案

问题似乎出在 scoring 参数中。您已通过 accuracy_scoreaccuracy_score 的签名是 accuracy_score(y_true, y_pred[, ...])。但是在模块logistic.py

if isinstance(scoring, six.string_types):
    scoring = SCORERS[scoring]
for w in coefs:
    // Other code
    if scoring is None:
        scores.append(log_reg.score(X_test, y_test))
    else:
        scores.append(scoring(log_reg, X_test, y_test))

因为你已经通过了 accuracy_score,所以它不符合上面的第一行。 并且 scores.append(scoring(log_reg, X_test, y_test)) 用于对估计器进行评分。但正如我上面所说,这里的参数与 accuracy_score 所需的参数不匹配。因此错误。

解决方法:使用 make_scorer LogisticRegressionCV 中的 (accuracy_score) 用于评分或简单地传递字符串 'accuracy'

logmodel = LogisticRegressionCV(Cs =1, dual=False , 
                                scoring = make_scorer(accuracy_score), 
                                penalty = 'l2')

                         OR

logmodel = LogisticRegressionCV(Cs =1, dual=False , 
                                scoring = 'accuracy', 
                                penalty = 'l2')

注意:

这可能是 logistic.py 模块的一部分或 LogisticRegressionCV 文档中的错误,他们应该已经阐明了评分函数的签名。

您可以提交 an issue to the github and see how it goes 完成

关于python - Sklearn LogisticRegressionCV 的类似数组的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151921/

相关文章:

python - 使用 Spotfire 的日期过滤器和相对日期输入框

python - Yfinance - 替代方案?

python - 子集 Pandas 数据框并保留原始大小

python - 如何并行使用 KNNImputer?

python - 将 scikit-learn DecisionTreeClassifier.tree_.value 映射到预测类

python sklearn GradientBoostingClassifier热启动报错

python - 如何向 PySCIPOpt 类添加属性

python - Python/Ubuntu 中的 WiFi 网络 RSSI 级别提取

python - 检查一个数据框的项目是否在另一个数据框中定义的范围内

python - DataFrame.astype() 错误参数