- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我第一次尝试使用 ML 和 Python 进行文档分类。
然后根据词袋原理向模型提供数据,如下所示:
vectorizer = CountVectorizer(analyzer = "word",
tokenizer = None,
preprocessor = None,
stop_words = None,
max_features = 5000)
text_features = vectorizer.fit_transform(full_df["processed full text"])
text_features = text_features.toarray()
labels = np.array(full_df['category'])
X_train, X_test, y_train, y_test = train_test_split(text_features, labels, test_size=0.33)
forest = RandomForestClassifier(n_estimators = 100)
forest = forest.fit(X_train, y_train)
y_pred = forest.predict(X_test)
accuracy_score(y_pred=y_pred, y_true=y_test)
到目前为止它运行良好(尽管给我的准确率太高了 99%)。但我现在想在一个全新的文本文档上测试它。如果我对其进行矢量化并执行forest.predict(test)
,它显然会说:
ValueError: Number of features of the model must match the input. Model n_features is 5000 and input n_features is 45
我不知道如何克服这个问题以便能够对全新的文章进行分类。
最佳答案
首先,尽管我的建议可能有效,但我强烈强调这个解决方案具有一些统计和计算结果,您在运行此代码之前需要了解这些结果。假设您有一个初始文本语料库 full_df["processed full text"]
,并且 test
是您想要测试的新文本。然后,使用 full_df
和 test
定义 full_added
文本语料库。
text_features = vectorizer.fit_transform(full_added)
text_features = text_features.toarray()
您可以使用 full_df
作为训练集(X_train = full_df["processed full text"]
和 y_train = np.array(full_df['category '])
)。然后你就可以运行
forest = RandomForestClassifier(n_estimators = 100)
forest = forest.fit(X_train, y_train)
y_pred = forest.predict(test)
当然,在此解决方案中,您已经定义了参数,并且您认为您的模型对新数据具有鲁棒性。
另一件事是,如果您有一个新文本流作为您想要分析的输入,则此解决方案将非常糟糕,因为计算新的 vectorizer.fit_transform(full_added)
会急剧增加。
希望对您有帮助。
关于python - 对新文档进行分类 - 随机森林、词袋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633828/
我们正在运行 MarkLogic 9.0-11 版本 3 节点集群,并且 MarkLogic 安装在“/var/opt/MarkLogic/”目录中,我们创建了“/var/opt/MarkLogic/
我有一片任意高度的森林,大致像这样: let data = [ { "id": 2, "name": "AAA", "parent_id": null, "short_name": "A" },
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 7 年前。 Improve
我有一个巨大的深度字典,代表森林(许多非二叉树),我想处理森林并创建一个包含森林所有可能关系的文本文件,例如给定字典: {'a': {'b': {'c': {}, 'd': {}}, 'g': {}}
在我的 Android 应用程序中,我包含了谷歌地图。现在我想获取有关您周围地区的信息。例如,你是在公园/森林/海滩……所以我基本上想要一个用“水”回答输入坐标 53°33'40.9"N 10°00'
如果我有下表: Member_Key Member_Name col1 Mem1 col2 Mem2 col3 Mem3 col4
继续我的老问题: Writing nested dictionary (forest) of a huge depth to a text file 现在我想把森林遍历写成BFS风格:我有一个巨大的深
我有一个多域环境(事件目录林),例如subdomain1.mydomain.com, subdomain2.mydomain.com 其中 mydomain.com 是根 AD 域 (GC) 和 su
我想知道是否有可能在 Google map 或 Bing Mag 2D/3D map 上恢复地形类型(山脉、森林、水域、平原等...) 。为了根据玩家在现实世界中的位置生成 map !我认为可用 AP
我是一名优秀的程序员,十分优秀!