sklearn 管道拟合:AttributeError:未找到较低值

2024-01-07

我想在 sklearn 中使用管道,如下所示:

corpus = load_files('corpus/train')

stop_words = [x for x in open('stopwords.txt', 'r').read().split('\n')]  # Uppercase!

countvec = CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))

X_train, X_test, y_train, y_test = train_test_split(corpus.data, corpus.target, test_size=0.9,
                                                    random_state=0)
x_train_counts = countvec.fit_transform(X_train)
x_test_counts = countvec.transform(X_test)

k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])

pipeline = Pipeline([
    ('vectorizer',  CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
    ('classifier',  MultinomialNB()) ])

for train_indices, test_indices in k_fold:

    pipeline.fit(x_train_counts, y_train)
    predictions = pipeline.predict(x_test_counts)

但是,我收到此错误:

AttributeError: lower not found

我看过这个帖子:

属性错误:未找到下层;在 scikit-learn 中使用带有 CountVectorizer 的 Pipeline https://stackoverflow.com/questions/33605946/attributeerror-lower-not-found-using-a-pipeline-with-a-countvectorizer-in-scik

但我将字节列表传递给矢量化器,所以这不应该是问题。

EDIT

corpus = load_files('corpus')

stop_words = [x for x in open('stopwords.txt', 'r').read().split('\n')]

X_train, X_test, y_train, y_test = train_test_split(corpus.data, corpus.target, test_size=0.5,
                                                    random_state=0)

k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])

pipeline = Pipeline([
    ('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
    ('classifier', MultinomialNB())])

for train_indices, test_indices in k_fold:
    pipeline.fit(X_train[train_indices], y_train[train_indices])
    predictions = pipeline.predict(X_test[test_indices])

现在我收到错误:

TypeError: only integer arrays with one element can be converted to an index

2ND EDIT

corpus = load_files('corpus')

stop_words = [y for x in open('stopwords.txt', 'r').read().split('\n') for y in (x, x.title())]

k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])

pipeline = Pipeline([
    ('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
    ('classifier', MultinomialNB())])

for train_indices, test_indices in k_fold:
    pipeline.fit(corpus.data, corpus.target)

您没有正确使用管道。您不需要传递矢量化的数据,其想法是管道对数据进行矢量化。

# This is done by the pipeline
# x_train_counts = countvec.fit_transform(X_train)
# x_test_counts = countvec.transform(X_test)

k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])

pipeline = Pipeline([
    ('vectorizer',  CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
    ('classifier',  MultinomialNB()) ])

# also you are not using the indices...
for train_indices, test_indices in k_fold:

    pipeline.fit(corpus.data[train_indices], corpus.target[train_indices])
    predictions = pipeline.predict(corpus.data[test_indices])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

sklearn 管道拟合:AttributeError:未找到较低值 的相关文章

  • Cython 函数中的字符串

    我想这样做将字符串传递给 Cython 代码 test py s Bonjour myfunc s test pyx def myfunc char mystr cdef int i for i in range len mystr err
  • 如何读取通过追加行不断更新的文件?

    在我的终端中我正在运行 curl user dhelm 12345 https stream twitter com 1 1 statuses sample json gt raw data txt curl 的输出是实时流式 Twitte
  • [python]没有属性“TessBaseAPI”

    当我编译代码时出现错误 import tessercat api tesseract TessBaseAPI 错误是 AttributeError 模块 对象没有属性 TessBaseAPI 我已经安装了tesseract via pip
  • SQLAlchemy:检查给定值是否在列表中

    问题 在 PostgreSQL 中 检查某个字段是否在给定列表中是使用IN操作员 SELECT FROM stars WHERE star type IN Nova Planet SQLAlchemy 的等价物是什么INSQL查询 我尝试过
  • Python 列表理解不适用于 itertools.groupby 解码

    我正在尝试解码结果itertools groupby到一个值列表中 我的来源是 x 1 2 2 1 6 3 6 5 1 3 最初的方法是使用 for 语句来实现 如下所示 keyfunc itemgetter 0 groups unique
  • 为什么我不能“string”.print()?

    我的理解print 在 Python 和 Ruby 以及其他语言 中 它是字符串 或其他类型 上的方法 因为它的语法非常常用 打印 嗨 works 那么为什么不呢 hi print 在 Python 中或 hi print在红宝石工作 当你
  • 如何在返回的 AJAX 调用上使用 django 模板标签?

    我有一个简单的 AJAX 脚本 它在名为的搜索字段中获取输入的字符串AJAXBox并调用一个视图函数 该函数使用过滤器查询数据库并返回与输入参数匹配的所有 User 对象的查询集 当我使用 django 模板标签迭代查询集时 它不起作用 我
  • 什么时候用==,什么时候用is?

    奇怪的是 gt gt gt a 123 gt gt gt b 123 gt gt gt a is b True gt gt gt a 123 gt gt gt b 123 gt gt gt a is b False Seems a is b
  • 如何将一串Python代码编译成一个可以调用函数的模块?

    在 Python 中 我有一串 Python 源代码 其中包含以下函数 mySrc def foo print foo def bar print bar 我想将这个字符串编译成某种形式类似模块的对象这样我就可以调用代码中包含的函数 这是我
  • 直接打开Spyder还是通过Pythonxy打开?

    之前 我一直在运行PythonSpyder 我总是开始Spyder直接双击其图标 今天突然发现我还有一个东西叫Python x y 我注意到我也可以开始Spyder通过它 这两种方法有什么区别吗 如果不是的话 有什么意义Python x y
  • 将查询参数添加到 URL

    我正在尝试自动从网站下载数据 我需要将动态参数传递到每天更改的站点 html 的结构是表格而不是表单 如何传递参数并从 url 获取结果 这是我尝试过的 它需要在 python 2 7 中 import urllib url https d
  • 如何将字符串方法应用于数据帧的多列

    我有一个包含多个字符串列的数据框 我想使用对数据帧的多列上的系列有效的字符串方法 我希望这样的事情 df pd DataFrame A 123f 456f B 789f 901f df Out 15 A B 0 123f 789f 1 45
  • 如何在 Python 中将 EXR 文件的 float16 转换为 uint8

    我正在使用 OpenEXR 读取 Python 中的 EXR 文件 我有带有半数据 float16 的 R G 和 B 通道 我尝试使用 Numpy 将数据从 float16 转换为 uint8 0 255 颜色 但没有成功 rCh get
  • Python `concurrent.futures`:根据完成顺序迭代 future

    我想要类似的东西executor map 除了当我迭代结果时 我想根据完成的顺序迭代它们 例如首先完成的工作项应该首先出现在迭代中 等等 这样 当且仅当序列中的每个工作项尚未完成时 迭代就会阻塞 我知道如何使用队列自己实现这一点 但我想知道
  • 让 TensorFlow 在 ARM Mac 上使用 GPU

    我已经安装了TensorFlow在 M1 上 ARM Mac 根据这些说明 https github com apple tensorflow macos issues 153 一切正常 然而 模型训练正在进行CPU 如何将培训切换到GPU
  • 使用 plone.api 创建文件的 Python 脚本在设置文件时出现错误 WrongType

    Dears 我正在创建一个脚本python来在Plone站点中批量上传文件 安装是UnifiedInstaller Plone 4 3 10 该脚本读取了一个txt 并且该txt以分号分隔 在新创建的项目中设置文件时出现错误 下面是脚本 f
  • 需要一个从 yaml 文件中提取内容并输出为 csv 文件的脚本

    我对 python 很陌生 但我很感激您帮助指导我创建一个简单的脚本 该脚本读取一堆 yaml 文件 同一目录中的大约 300 个文件 并从 yaml 文件并将其转换为 csv yaml 文件中内容的示例 code 9313 degrees
  • Python模糊字符串匹配作为相关样式表/矩阵

    我有一个文件 其中包含 x 个字符串名称及其关联的 ID 本质上是两列数据 我想要的是一个格式为 x by x 的相关样式表 将相关数据作为 x 轴和 y 轴 但我想要 fuzzywuzzy 库的函数 fuzz ratio x y 作为输出
  • 将字典写入 csv 时遇到问题,其中键作为标题,值作为列

    我有一本字典 看起来像 mydict foo 1 2 bar 3 4 asdf 5 6 我正在尝试将其写入 CSV 文件 使其看起来像 foo bar asdf 1 3 5 2 4 6 我花了最后一个小时寻找解决方案 我发现的最接近的解决方
  • 将自定义属性添加到 Tk 小部件

    我的主要目标是向小部件添加隐藏标签或字符串之类的内容 以在其上保存简短信息 我想到创建一个新的自定义 Button 类 在本例中我需要按钮 它继承所有旧选项 这是代码 form tkinter import class NButton Bu

随机推荐