- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是机器学习的新手,所以我仍然不明白如何在词袋案例中可视化两个类之间的边界。
我找到了以下示例来绘制数据
plot a document tfidf 2D graph
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
newsgroups_train = fetch_20newsgroups(subset='train',
categories=['alt.atheism', 'sci.space'])
pipeline = Pipeline([
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
])
X = pipeline.fit_transform(newsgroups_train.data).todense()
pca = PCA(n_components=2).fit(X)
data2D = pca.transform(X)
plt.scatter(data2D[:,0], data2D[:,1], c=newsgroups_train.target)
plt.show()
在我的项目中我使用 SVC 估计器
clf = SVC(random_state=241, kernel = 'linear')
clf.fit(X,newsgroups_train.target)
我试过用这个例子 http://scikit-learn.org/stable/auto_examples/svm/plot_iris.html但它在文本分类情况下不起作用
那么我怎样才能将两个类的边界添加到这个图中呢?
谢谢!
最佳答案
问题是您只需选择 2 个特征即可创建二维决策曲面图。我将提供 2 个示例。第一个使用 iris
数据,第二个使用 your
数据。
在这两种情况下,我都只选择了 2 个特征来创建绘图。
from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
def make_meshgrid(x, y, h=.02):
x_min, x_max = x.min() - 1, x.max() + 1
y_min, y_max = y.min() - 1, y.max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
return xx, yy
def plot_contours(ax, clf, xx, yy, **params):
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
out = ax.contourf(xx, yy, Z, **params)
return out
model = svm.SVC(kernel='linear')
clf = model.fit(X, y)
fig, ax = plt.subplots()
# title for the plots
title = ('Decision surface of linear SVC ')
# Set-up grid for plotting.
X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)
plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_ylabel('y label here')
ax.set_xlabel('x label here')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
ax.legend()
plt.show()
from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
newsgroups_train = fetch_20newsgroups(subset='train',
categories=['alt.atheism', 'sci.space'])
pipeline = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer())])
X = pipeline.fit_transform(newsgroups_train.data).todense()
# Select ONLY 2 features
X = np.array(X)
X = X[:, [0,1]]
y = newsgroups_train.target
def make_meshgrid(x, y, h=.02):
x_min, x_max = x.min() - 1, x.max() + 1
y_min, y_max = y.min() - 1, y.max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
return xx, yy
def plot_contours(ax, clf, xx, yy, **params):
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
out = ax.contourf(xx, yy, Z, **params)
return out
model = svm.SVC(kernel='linear')
clf = model.fit(X, y)
fig, ax = plt.subplots()
# title for the plots
title = ('Decision surface of linear SVC ')
# Set-up grid for plotting.
X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)
plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_ylabel('y label here')
ax.set_xlabel('x label here')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
ax.legend()
plt.show()
结果
在第二种情况下,绘图并不好看,因为我们只随机选择了 2 个特征来创建它。让它变得更好的一种方法如下:您可以使用单变量排名方法
(例如方差分析 F 值测试)并从 22464
你最初拥有的。然后使用这些 top-2
您可以创建一个漂亮的分离曲面图。
关于python - 如何使用 scikit-learn 可视化两个类的边界/决策函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50305223/
是否有显示测试用例数量以提供决策/条件覆盖率的工具? 例如: if(x>0) if(x0) 3 个案例足以覆盖决策/条件。 if(x>0) if(x0) 4 个案例足以覆盖决策/条件。 这是真的吗?
我正在尝试找到一种优雅的方式来实现易于维护的决策算法,因为决策的条件可能经常变化。 我将尝试更具体地举一个例子: 假设我正在尝试管理一家餐厅厨房的 cooking 厨师团队。 每个厨师都知道如何 co
我需要一个 Android Activity ,它应该显示一个字段,如带有图像的标题和其下方的几个动态生成的项目(我认为是 1 到 100)。如果我不想让 headsection 滚动,我会使用 Li
我正在编写函数以从值列表中提供最大值(value)。我的问题是如果所有值都相同怎么办?例如, 30,29,34,45 简单。最大值为 45。现在, 20,20,20,20 这里的最大值是20吗?或者没
我需要知道哪个检索事件日志的速度更快,但我在比较中找不到:假设需要查找的所有列都有btree索引,需要查找的json对象中的所有键都有GIN索引。 case 1: ActivityID (in
我需要在我的 iPhone 应用程序中显示一个表格: neither the number of cells nor the contents are known at compile time, b
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
这是针对在 MinGW/Windows 上使用 SDL 的小型游戏项目。 我正在研究一个物理引擎,我的想法是拥有一个Physics::Object,所有物理对象都应该派生自它,并且它会在全局 Phys
我有一个小的 LINQ 查询来填充下拉控件(WinForms Telerik 应用程序),其中的数据行显示两个值(ITNBR 和描述): var query = from i in db.ItemMa
我正在尝试使用 antlr 3 为我的语法生成词法分析器和解析器。有人可以解释这个错误是什么意思吗? error(211): T.g:14:6: [fatal] rule stmt has non-L
partykit包很好地表示了决策树。我遇到的唯一问题是标签很长然后它们重叠。是否可以移动这些标签以防止它(见下图中的蓝色箭头)? library("rpart") library("partykit
所以我环顾四周,似乎找不到合适的解决方案来解决我的问题。 问题 在我的布局中,我希望能够根据数据库中的内容选择在运行时是否存在导航项: 当前布局(导航栏) @Html.Acti
我目前正在创建一个机器学习 jupyter 笔记本作为一个小项目,并希望显示我的决策树。但是,我能找到的所有选项都是导出图形然后加载图片,这相当复杂。 所以想问问有没有办法不用导出加载图形,直接显示我
grammar AdifyMapReducePredicate; PREDICATE : PREDICATE_BRANCH | EXPRESSION ; PREDICA
我是一名优秀的程序员,十分优秀!