sklearn分类器获取ValueError:输入形状错误 [英] sklearn classifier get ValueError: bad input shape

查看:85
本文介绍了sklearn分类器获取ValueError:输入形状错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv,结构是
CAT1,CAT2,TITLE,URL,内容,CAT1,CAT2,TITLE,CONTENT是中文。

I have a csv, struct is CAT1,CAT2,TITLE,URL,CONTENT, CAT1, CAT2, TITLE ,CONTENT are in chinese.

我想用X(TITLE)训练 LinearSVC MultinomialNB 和feature(CAT1,CAT2)都出现此错误。以下是我的代码:

I want train LinearSVC or MultinomialNB with X(TITLE) and feature(CAT1,CAT2), both get this error. below is my code:

PS:我通过以下示例编写以下代码 scikit-learn text_analytics

PS: I write below code through this example scikit-learn text_analytics

import numpy as np
import csv
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline

label_list = []

def label_map_target(label):
    ''' map chinese feature name to integer  '''
    try:
        idx = label_list.index(label)
    except ValueError:
        idx = len(label_list)
        label_list.append(label)

    return idx


c1_list = []
c2_list = []
title_list = []
with open(csv_file, 'r') as f:
    # row_from_csv is for shorting this example
    for row in row_from_csv(f):
        c1_list.append(label_map_target(row[0])
        c2_list.append(label_map_target(row[1])
        title_list.append(row[2])

data = np.array(title_list)
target = np.array([c1_list, c2_list])
print target.shape
# (2, 4405)
target = target.reshape(4405,2)
print target.shape
# (4405, 2)

docs_train, docs_test, y_train, y_test = train_test_split(
   data, target, test_size=0.25, random_state=None)

# vect = TfidfVectorizer(tokenizer=jieba_tokenizer, min_df=3, max_df=0.95)
# use custom chinese tokenizer get same error
vect = TfidfVectorizer(min_df=3, max_df=0.95)
docs_train= vect.fit_transform(docs_train)

clf = LinearSVC()
clf.fit(docs_train, y_train)

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-904eb9af02cd> in <module>()
      1 clf = LinearSVC()
----> 2 clf.fit(docs_train, y_train)

C:\Python27\lib\site-packages\sklearn\svm\classes.pyc in fit(self, X, y)
    198 
    199         X, y = check_X_y(X, y, accept_sparse='csr',
--> 200                          dtype=np.float64, order="C")
    201         self.classes_ = np.unique(y)
    202 

C:\Python27\lib\site-packages\sklearn\utils\validation.pyc in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric)
    447                         dtype=None)
    448     else:
--> 449         y = column_or_1d(y, warn=True)
    450         _assert_all_finite(y)
    451     if y_numeric and y.dtype.kind == 'O':

C:\Python27\lib\site-packages\sklearn\utils\validation.pyc in column_or_1d(y, warn)
    483         return np.ravel(y)
    484 
--> 485     raise ValueError("bad input shape {0}".format(shape))
    486 
    487 

ValueError: bad input shape (3303, 2)


推荐答案

感谢@meelo,我解决了这个问题。
如他所说:在我的代码中, data 是一个特征向量, target 是目标值。我混合了两件事。

Thanks to @meelo, I solved this problem. As he said: in my code, data is a feature vector, target is target value. I mixed up two things.

我了解到 TfidfVectorizer 将数据处理为[数据,特征]和每个数据应该只映射到一个目标。

I learned that TfidfVectorizer processes data to [data, feature], and each data should map to just one target.

如果我要预测两个类型的目标,则需要两个不同的目标:

If I want to predict two type targets, I need two distinct targets:


  1. target_C1 并具有所有C1值

  2. target_C2 具有所有C2值。

  1. target_C1 with all C1 value
  2. target_C2 with all C2 value.

然后使用两个目标和原始数据为每个目标训练两个分类器。

Then use the two targets and original data to train two classifier for each target.

这篇关于sklearn分类器获取ValueError:输入形状错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆