Sklearn Logistic回归形状错误,但x,y形状一致

时间:2019-08-10 00:23:39

标签: python scikit-learn

我得到一个ValueError:当我运行以下命令时发现输入变量的样本数不一致:[20000,1],即使x和y的行值正确。我加载RCV1数据集,获取带有前x个文档的类别索引,为每个类别创建具有相等数量的随机选择的正负的元组列表,然后最终尝试对其中一个类别进行逻辑回归

import sklearn.datasets
from sklearn import model_selection, preprocessing
from sklearn.linear_model import LogisticRegression
from matplotlib import pyplot as plt
from scipy import sparse

rcv1 = sklearn.datasets.fetch_rcv1()

def get_top_cat_indices(target_matrix, num_cats):
    cat_counts = target_matrix.sum(axis=0)
    #cat_counts = cat_counts.reshape((1,103)).tolist()[0]
    cat_counts = cat_counts.reshape((103,))

    #b = sorted(cat_counts, reverse=True)
    ind_temp = np.argsort(cat_counts)[::-1].tolist()[0]

    ind = [ind_temp[i] for i in range(5)]
    return ind

def prepare_data(x, y, top_cat_indices, sample_size):
    res_lst = []

    for i in top_cat_indices:

        # get column of indices with relevant cat
        temp = y.tocsc()[:, i]

        # all docs with labeled category
        cat_present = x.tocsr()[np.where(temp.sum(axis=1)>0)[0],:]
        # all docs other than labelled category
        cat_notpresent = x.tocsr()[np.where(temp.sum(axis=1)==0)[0],:]
        # get indices equal to 1/2 of sample size
        idx_cat = np.random.randint(cat_present.shape[0], size=int(sample_size/2))
        idx_nocat = np.random.randint(cat_notpresent.shape[0], size=int(sample_size/2))
        # concatenate the ids

        sampled_x_pos = cat_present.tocsr()[idx_cat,:]
        sampled_x_neg = cat_notpresent.tocsr()[idx_nocat,:]
        sampled_x = sparse.vstack((sampled_x_pos, sampled_x_neg))

        sampled_y_pos = temp.tocsr()[idx_cat,:]
        sampled_y_neg = temp.tocsr()[idx_nocat,:]
        sampled_y = sparse.vstack((sampled_y_pos, sampled_y_neg))

        res_lst.append((sampled_x, sampled_y))

    return res_lst

ind = get_top_cat_indices(rcv1.target, 5)
test_res = prepare_data(train_x, train_y, ind, 20000)

x, y = test_res[0]
print(x.shape)
print(y.shape)
LogisticRegression().fit(x, y)

是稀疏矩阵的问题还是维数的问题(有2万个样本和47K个特征)

1 个答案:

答案 0 :(得分:1)

运行代码时,出现以下错误:

  

AttributeError:“布尔”对象没有属性“任何”

这是因为y的{​​{1}}需要numpy数组。因此,我将最后一行更改为:

LogisticRegression

然后我得到以下错误:

  

ValueError:此求解器需要数据中至少2个类的样本,但数据仅包含一个类:0

这是因为您的采样代码具有错误。在使用采样索引之前,您需要使用具有该类别的行将y数组子集化。参见下面的代码:

LogisticRegression().fit(x, y.A.flatten())

现在,所有内容都像魅力一样