machine-learning - SVM 可视化非常随机且不准确
问题描述
def vec(utterance):
embedder = UtteranceEmbedder(utterance)
word2vec = embedder.as_word2vec()
bow = embedder.as_bow_vec()
ret = np.concatenate([word2vec, bow])
return np.pad(ret, [0, 500-len(ret)], "constant")
op = OptionParser()
op.add_option(
"-f", "--file", help="path to file containing utterances to visualize",
action="store", type="string", dest="path"
)
(opt, args) = op.parse_args()
if opt.path is None or (opt.path is not None and len(opt.path)) == 0:
op.error("path to file containing newline separated utterances must be specified")
vectors = []
with open(opt.path) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
for utterance in [x.strip() for x in content]:
vectors.append(vec(utterance))
vectors_reduced = TSNE(n_components=2).fit_transform(np.array(vectors))
X=np.array(vectors_reduced)
y=np.array([0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1])
clf = svm.SVC(decision_function_shape='ovo',class_weight="balanced)
clf.fit(X, y)
话语将是一个短语,我将对话语进行标记,从 Google 300B 训练模型中提取 word2vec 向量,附加一袋词向量并拟合数据。以下是我的训练数据: Input.txt
yea
yeah
yaa
say
ok
okay
no
nope
not interested
dont
cant
cannot
not now
not really
not at the moment
no thank you
sorry no
sorry
not active
正如您所看到的,它是一个简单的对立案例,当我使用 matplotlib 绘制点时,我会尽可能地随机而不是线性分离。
这种完全不准确的事件可能是什么情况。
解决方案
您正在使用带有标准参数的 svc。特别是你的内核是一个 rbf 内核(也称为 gaußian)。SVM 的工作原理有点过于复杂,无法在此处发布,而使用内核则更加复杂。如果你有兴趣,我可以向你推荐麻省理工学院的讲座。
https://www.youtube.com/watch?v=_PwhiWxHK8o
但简而言之,您的高斯核分离仍然是线性的,但在更高维的向量空间中。它将用于分离的数据转换到该空间中,并用超平面将其线性分离。如果您稍后使用支持向量在二维中可视化您的数据,则分离不是线性的,而是在您的“内核向量空间”中。
顺便提一句。您还应该考虑标准化。
推荐阅读
- xml - 在 XML 负载中查找不同的值 | XSL 映射| SOA|BPEL
- c# - 如何访问 Hashtable list/Viewmode.Model 中的值并在 View 中显示?
- db2 - 如何在 DB2 10.5 及更高版本中找到最旧的活动事务?
- reactjs - 自定义 GroupRow 渲染 (mbrn/MaterialTable) React
- oracle - 全局临时表空间分配是否发生在会话开始?
- javascript - 移动菜单样式以显示活动菜单
- javascript - 验证依赖于复选框 JS 的多个文本字段
- android - 如何在 Android 中自动读取短信?
- sql-server - 从 SQL 作业执行 ssis 包时出现错误“返回值未知。进程退出代码为 128。步骤失败”
- c++ - 不可复制的类数据成员的统一初始化导致 gcc 错误