- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 SKLearn 0.20.2 来创建管道,同时使用新的 ColumnTransformer 功能。我的问题是,当我运行分类器时: clf.fit(x_train, y_train)
我不断收到错误:
ValueError:除串联轴之外的所有输入数组维度必须完全匹配
我有一列名为text
的文本 block 。我的所有其他专栏本质上都是数字。我正在尝试在我的管道中使用 Countvectorizer,我认为这就是问题所在。非常感谢您的帮助。
运行管道并检查 x_train/y_train 后,如果有帮助的话,它看起来像这样(省略通常显示在左列中的行号,并且文本列的运行高度高于图像中显示的高度)。
<小时/>
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
# plus other necessary modules
# mapped to column names from dataframe
numeric_features = ['hasDate', 'iterationCount', 'hasItemNumber', 'isEpic']
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median'))
])
# mapped to column names from dataframe
text_features = ['text']
text_transformer = Pipeline(steps=[
('vect', CountVectorizer())
])
preprocessor = ColumnTransformer(
transformers=[('num', numeric_transformer, numeric_features),('text', text_transformer, text_features)]
)
clf = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', MultinomialNB())
])
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.33)
clf.fit(x_train,y_train)
最佳答案
如果您运行此代码,Vadim 是正确的
numeric_features = ['hasDate', 'iterationCount', 'hasItemNumber', 'isEpic']
numeric_transformer = SimpleImputer(strategy='median')
num = numeric_transformer.fit_transform(df[numeric_features])
# num.shape
# (3, 4)
text_features = ['text']
text_transformer = CountVectorizer()
text = text_transformer.fit_transform(df[text_features])
print(text_transformer.get_feature_names())
print(text.toarray())
输出将如下所示。
['text']
[[1]]
这是由于我不止一次遇到的文本处理过程中的一些故障。
如果您将 text_features 定义为字符串而不是单元素列表
text_features = 'text'
text_transformer = CountVectorizer()
text = text_transformer.fit_transform(df[text_features])
print(text_transformer.get_feature_names())
print(text.toarray())`
变成这样
['123', '16118', '17569', '456', '8779', '9480']
[[0 0 1 0 1 0]
[0 1 0 0 0 1]
[1 0 0 1 0 0]]
这就是你想要的。
将列名称作为列表使得 CountVectorizer 由于某种原因只能看到一项
关于python - sklearn : Text and Numeric features with ColumnTransformer has value error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54541490/
我以默认数字 js 格式存储数字和货币格式。我允许用户根据他们的区域设置拥有自己的格式。我正在尝试弄清楚如何将他们的自定义格式转换回默认格式,以便一致地存储它,以防他们更改语言环境。 例如设置电流:
我以默认数字 js 格式存储数字和货币格式。我允许用户根据他们的区域设置拥有自己的格式。我正在尝试弄清楚如何将他们的自定义格式转换回默认格式,以便一致地存储它,以防他们更改语言环境。 例如设置电流:
我正在为 as.POSIXct.numeric 苦苦挣扎。以下代码有效: dates <- structure(c(1649285787, 1651134684), class = c("POSIXc
我正在使用 Azavea Numeric Scala 库进行通用数学运算。但是,我无法将它们与 Scala Collections API 一起使用,因为它们需要 scala Numeric,并且
我在仔细阅读文档时注意到,Numeric 类有一个方法 arg,其解释为“如果值为正,则返回 0,否则返回 pi。直接在这个上面的docs 是一个名为 angle 的方法,其解释相同。 我不明白这个方
我试图总结两列并获得错误:二进制运算符的非数字参数。但是,这两列都是数字。因此,我不明白为什么会收到此错误消息。谢谢你的帮助? data_all% mutate(total='5min'+'10min
我正在尝试创建六个站点上物种丰富度的热图。 我有一个站点与物种的矩阵,其中有数字丰度数据。 但是,当我运行代码时,R返回一个错误,指出我的矩阵是非数字的。 谁能想到这个?我感到难过。 导出的数据框链接
我正在使用一个返回 float 的java函数 private float getEventScaledX(float eventX, float resX) { return (eventX
我有一个函数接收 SearchCriteria 的集合对象:列名或键、运算符( = ### && :letter_date <= ### 我在两个日期之间查找。我不能两次使用相同的参数名称,因此我在遍
我正在尝试将值插入到表中,但我不断收到错误消息 ORA-01858: a non-numeric character was found where a numeric was expected 这是
sort 提供两种数字排序。这是来自手册页: -g, --general-numeric-sort compare according to general numerica
我遇到了一个奇怪的问题,将日期列与 SYSDATE 进行比较会产生以下错误: 01858. 00000 - "a non-numeric character was found where a nu
这是表格 CREATE TABLE Employee (EmpID number(5) primary key, SIN Number(9) Not null, Las
在 bash 中按 alt + numeric 会得到 (arg [numeric]) 那是什么? (这类问题更适合问人,而不是试图“猜测”正确的术语以通过互联网搜索文档)。 最佳答案 您要搜索的字词
当我执行准备好的语句时,我得到了 ORA-01858:在需要数字的地方发现了一个非数字字符。当我搜索一些论坛时,他们说这是因为日期数据类型。我准备好的声明是 insert into OPRS_ZON
我正在使用 Elasticsearch Java API 创建索引并编写搜索查询。 索引是在各种字段上创建的。字段之一是创建索引的数字(整数)。 现在我们得到的输入是字符串的形式。我们必须在所有字段中
我想重新映射启用 Numlock 的数字小键盘,使其表现得像关闭 Numlock 的数字小键盘,包括能够在按住 Shift/Ctrl 的情况下扩展选择范围。 我遇到的问题如下 Numpad8::Up
所以我有一个训练集,其中一个属性的域如下: A = {Type1, Type2, Type3, ... ,Type5} 如果域保持这种形式,我就无法应用线性回归,因为数学假设不可能成立,例如: H =
为什么我会通过 Java 应用程序执行此查询从 Oracle 收到上述 SQL 错误? SELECT * FROM TestSchema.TestTable WHERE userid = :userI
我在 JSP 中添加了以下代码,并在将实体保存到数据库时出现一些异常。 $('.validateWeight').click(function(event) { var id = event.tar
我是一名优秀的程序员,十分优秀!