首页 > 解决方案 > 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 绘制点时,我会尽可能地随机而不是线性分离。三角形表示是,其他是否

这种完全不准确的事件可能是什么情况。

标签: machine-learningscikit-learnsvmsvc

解决方案


您正在使用带有标准参数的 svc。特别是你的内核是一个 rbf 内核(也称为 gaußian)。SVM 的工作原理有点过于复杂,无法在此处发布,而使用内核则更加复杂。如果你有兴趣,我可以向你推荐麻省理工学院的讲座。

https://www.youtube.com/watch?v=_PwhiWxHK8o

但简而言之,您的高斯核分离仍然是线性的,但在更高维的向量空间中。它将用于分离的数据转换到该空间中,并用超平面将其线性分离。如果您稍后使用支持向量在二维中可视化您的数据,则分离不是线性的,而是在您的“内核向量空间”中。

顺便提一句。您还应该考虑标准化


推荐阅读