- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 csv,结构是CAT1,CAT2,TITLE,URL,CONTENT
, CAT1, CAT2, TITLE ,CONTENT 为中文。
我想用 X(TITLE) 和特征 (CAT1,CAT2) 训练 LinearSVC
或 MultinomialNB
,两者都会出现此错误。下面是我的代码:
PS:我通过这个例子写了下面的代码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
是目标值。我混淆了两件事。
我了解到 TfidfVectorizer
将数据处理为 [data, feature],并且每个数据应该只映射到一个目标。
如果我想预测两种类型的目标,我需要两个不同的目标:
target_C1
所有 C1 值target_C2
具有所有 C2 值。然后使用两个目标和原始数据为每个目标训练两个分类器。
关于python - sklearn 分类器获取 ValueError : bad input shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31306390/
我们的电脑在使用的过程中,有的小伙伴在上网的时候可能就遇到过系统提示:400 bad request的情况。据小编所知这种情况,大致意思就是出现了错误的请求或者请求不能满足。原因是因为我们请求的语法
您可以尝试清除浏览器缓存 访问一下你的FTP看是否可以登陆 成功解决502 Bad Gateway错误 今天登陆博客,显示502 bad gateway,NGINX最烦人的地方就是经常会出现这个
我想要具有 FIFO 的服务器-客户端模型和客户端获取目录路径,但我收到错误“读:错误地址”和“写:错误地址”。 客户端 服务器错误:“读取:地址错误” 客户端错误:“写入:地址错误” 最佳答案 您可
Agda 手册 Inductive Data Types and Pattern Matching状态: To ensure normalisation, inductive occurrences
我正在使用 maven-compiler-plugin:2.3.2 并且每次我对在导入中具有枚举 (ContentType) 的类进行更改时,我需要使 干净,否则它会给我: ERROR] Failed
我想发布我的第一个 Facebook 应用程序,需要一个隐私政策 URL。 我在我的网站上发布了 privacypolicy.html 页面,但是当我在“应用程序详细信息”中配置它时,我收到了下一条消
vscode 1.45.1版本使用克隆存储库时,我收到“Bad credentials”。最近我在github上换了用户名。可能就是这个原因。我如何告诉vs code?
我正在 Mac OS 终端上创建 cron,代码如下: home.cron 的内容: * * * * * /users/username/desktop/forTrump/script.sh 然后我这
我是新手,所以需要任何帮助,当我要求一个例子时,我的教授给我了这段代码,我希望有一个工作模型...... from numpy import loadtxt import numpy as np fr
我使用 linux 服务器已经有一段时间了,通过使用 cifs 挂载到多个 Windows 共享。 到目前为止,我总是在/etc/fstab 中有一行://IPADDRESS/sharename/mn
请大家帮帮我我正在尝试使用 NUTCH 抓取网站,但它给我错误“java.io.IOException: Job failed!” 我正在运行此命令“bin/nutch solrindex http:
我想创建我的基础业务类,例如 EntityBase,以具有一些常见的行为,例如实现用于跟踪对象更改的接口(interface)(IsNew、IsDirty)和 INotifyPropertyChang
我们最近开发了一个基于 SOA 的站点,但是这个站点在负载过重时最终会出现严重的负载和性能问题。我在这里发布了一个与此问题相关的问题: ASP.NET website becomes unrespon
我们的 Azure 功能已开始返回 502 Bad Gateways,但并非所有调用都返回。我没有使用“间歇性”这个词,因为它总是进行相同类型的调用,但现在总是使用相同的数据。 常规配置 Azure
我假设在字典中进行查找时,它需要散列您提供的 key ,然后使用该散列来查找您要查找的对象。 如果是这样,使用较大的对象作为键是否会显着减慢查找速度或产生其他使用字符串或简单数据类型作为键不会遇到的后
我的代码如下: public static final Condition.ActionCondition ACTION_CONDITION_ACTIVATE = new Condit
大家好,我有一个应用程序和一个表单,我要求用户在其中输入地址,并在文本字段下方显示带有标记的谷歌地图,用户可以在其中将标记拖/放到正确的位置。问题是,在显示 map 的开始时,它只是部分显示而不是全部
给定字节矩阵(所有值在内存中都是 1 位),如果其中至少有一个零,则称其为原始列或“坏”列。查找算法,占用 O(1) 额外内存。 如果没有另一个值(如 -1)或另一个重复矩阵来跟踪已经找到的空值,并且
当我创建一个标准类时,我主要这样做: $test = null; $test->id = 1; $test->name = 'name'; 但是在严格模式下我得到一个错误。 显然正确的做法是: $te
我试图理解为什么将 -O2 -march=native 与 GCC 一起使用会比不使用它们时产生更慢的代码。请注意,我在 Windows 7 下使用 MinGW (GCC 4.7.1)。 这是我的代码
我是一名优秀的程序员,十分优秀!