- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用线性 SVM 和 K Neighbors 分类器来进行词义消歧 (WSD)。这是我用来训练数据的一段数据:
<corpus lang="English">
<lexelt item="activate.v">
<instance id="activate.v.bnc.00024693" docsrc="BNC">
<answer instance="activate.v.bnc.00024693" senseid="38201"/>
<context>
Do you know what it is , and where I can get one ? We suspect you had seen the Terrex Autospade , which is made by Wolf Tools . It is quite a hefty spade , with bicycle - type handlebars and a sprung lever at the rear , which you step on to <head>activate</head> it . Used correctly , you should n't have to bend your back during general digging , although it wo n't lift out the soil and put in a barrow if you need to move it ! If gardening tends to give you backache , remember to take plenty of rest periods during the day , and never try to lift more than you can easily cope with .
</context>
</instance>
<instance id="activate.v.bnc.00044852" docsrc="BNC">
<answer instance="activate.v.bnc.00044852" senseid="38201"/>
<answer instance="activate.v.bnc.00044852" senseid="38202"/>
<context>
For neurophysiologists and neuropsychologists , the way forward in understanding perception has been to correlate these dimensions of experience with , firstly , the material properties of the experienced object or event ( usually regarded as the stimulus ) and , secondly , the patterns of discharges in the sensory system . Qualitative Aspects of Experience The quality or modality of the experience depends less upon the quality of energy reaching the nervous system than upon which parts of the sensory system are <head>activated</head> : stimulation of the retinal receptors causes an experience of light ; stimulation of the receptors in the inner ear gives rise to the experience of sound ; and so on . Muller 's nineteenth - century doctrine of specific energies formalized the ordinary observation that different sense organs are sensitive to different physical properties of the world and that when they are stimulated , sensations specific to those organs are experienced . It was proposed that there are endings ( or receptors ) within the nervous system which are attuned to specific types of energy , For example , retinal receptors in the eye respond to light energy , cochlear endings in the ear to vibrations in the air , and so on .
</context>
</instance>
.....
训练数据和测试数据的区别在于测试数据没有“answer”标签。我已经建立了一个字典来存储窗口大小为 10 的每个实例的“head”词的邻居词。当一个实例有多个时,我只考虑第一个 .我还建立了一个集合来记录训练文件中的所有词汇,这样我就可以为每个实例计算一个向量。 例如,如果总词汇量为 [a,b,c,d,e],并且一个实例包含单词 [a,a,d,d,e],则该实例的结果向量将为 [2 ,0,0,2,1]。这是我为每个单词构建的字典的一部分:
{
"activate.v": {
"activate.v.bnc.00024693": {
"instanceId": "activate.v.bnc.00024693",
"senseId": "38201",
"vocab": {
"although": 1,
"back": 1,
"bend": 1,
"bicycl": 1,
"correct": 1,
"dig": 1,
"general": 1,
"handlebar": 1,
"hefti": 1,
"lever": 1,
"nt": 2,
"quit": 1,
"rear": 1,
"spade": 1,
"sprung": 1,
"step": 1,
"type": 1,
"use": 1,
"wo": 1
}
},
"activate.v.bnc.00044852": {
"instanceId": "activate.v.bnc.00044852",
"senseId": "38201",
"vocab": {
"caus": 1,
"ear": 1,
"energi": 1,
"experi": 1,
"inner": 1,
"light": 1,
"nervous": 1,
"part": 1,
"qualiti": 1,
"reach": 1,
"receptor": 2,
"retin": 1,
"sensori": 1,
"stimul": 2,
"system": 2,
"upon": 2
}
},
......
现在,我只需要从 scikit-learn 向 K Neighbors Classifier 和 Linear SVM 提供输入来训练分类器。 但我不确定应该如何为每个构建特征向量和标签。我的理解是标签应该是“答案”中的实例标签和 senseid 标签的元组。但是我不确定当时的特征向量。我是否应该将来自同一个单词的所有向量分组,在“答案”中具有相同的实例标签和 senseid 标签?但是大约有 100 个单词,每个单词有数百个实例,我应该如何处理?
此外,矢量是一个特征,我需要在以后添加更多特征,例如同义词集、上位词、下位词等。我应该怎么做?
提前致谢!
最佳答案
机器学习问题是一种优化任务,您没有预定义的最佳算法,而是使用不同的方法、参数和数据预处理来摸索最佳结果。因此,您从最简单的任务开始绝对是正确的 - 只使用一个词和它的几种含义。
But I am just not sure how should I build the feature vector and label for each.
您可以只将这些值作为矢量分量。枚举矢量词并在每个文本中写下此类词的编号。如果单词不存在,则输入空值。我稍微修改了您的示例以阐明这个想法:
vocab_38201= {
"although": 1,
"back": 1,
"bend": 1,
"bicycl": 1,
"correct": 1,
"dig": 1,
"general": 1,
"handlebar": 1,
"hefti": 1,
"lever": 1,
"nt": 2,
"quit": 1,
"rear": 1,
"spade": 1,
"sprung": 1,
"step": 1,
"type": 1,
"use": 1,
"wo": 1
}
vocab_38202 = {
"caus": 1,
"ear": 1,
"energi": 1,
"experi": 1,
"inner": 1,
"light": 1,
"nervous": 1,
"part": 1,
"qualiti": 1,
"reach": 1,
"receptor": 2,
"retin": 1,
"sensori": 1,
"stimul": 2,
"system": 2,
"upon": 2,
"wo": 1 ### added so they have at least one common word
}
让我们将其转换为特征向量。枚举所有单词并标记该单词在词汇表中出现了多少次。
from collections import defaultdict
words = []
def get_components(vect_dict):
vect_components = defaultdict(int)
for word, num in vect_dict.items():
try:
ind = words.index(word)
except ValueError:
ind = len(words)
words.append(word)
vect_components[ind] += num
return vect_components
#
vect_comps_38201 = get_components(vocab_38201)
vect_comps_38202 = get_components(vocab_38202)
让我们看看:
>>> print(vect_comps_38201)
defaultdict(<class 'int'>, {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 2, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1})
>>> print(vect_comps_38202)
defaultdict(<class 'int'>, {32: 1, 33: 2, 34: 1, 7: 1, 19: 2, 20: 2, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 27: 2, 28: 1, 29: 1, 30: 1, 31: 1})
>>> vect_38201=[vect_comps_38201.get(i,0) for i in range(len(words))]
>>> print(vect_38201)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> vect_38202=[vect_comps_38202.get(i,0) for i in range(len(words))]
>>> print(vect_38202)
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1]
这些 vect_38201 和 vect38202 是您可以在拟合模型中使用的向量:
from sklearn.svm import SVC
X = [vect_38201, vect_38202]
y = [38201, 38202]
clf = SVC()
clf.fit(X, y)
clf.predict([[0, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2, 1]])
输出:
array([38202])
当然这是一个非常非常简单的例子,只是展示概念。
你能做些什么来改进它?
归一化向量坐标。
使用优秀的工具 Tf-Idf vectorizer从文本中提取数据特征。
添加更多数据。
祝你好运!
关于python - 在 scikit-learn 中实现 K Neighbors Classifier 和 Linear SVM 以进行词义消歧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189865/
我想替换以下特定于 Mozilla 的 CSS 规则: background: -moz-linear-gradient(center top , #F5F5F5, #E4E4E4); 使用标准 li
我正在将 MatLab 程序转换为 Python,我无法理解为什么 scipy.interpolate.interp2d(linear) 给出的结果与 MatLab interp2(linear) 不
我目前在自然语言处理方面开发的应用程序存在严重的性能问题。基本上,对于给定的文本,它会收集各种数据并进行一些数字运算。 对于每一个句子,它的作用完全相同。用于收集统计数据的算法不会随着先前读取的数据而
我正在为一个基本的 Android 计算器开发水平 View ,我有一个名为 linearLayout2 的 LinearLayout > 我试图将它放在 linearLayout3 之上,其中包含数
我为按钮的背景使用了一些 CSS,但它在 Firefox、Chrome 和 IE 中看起来不同。 我正在使用 -webkit-linear-gradient 和 -moz-linear-gradien
TL;DR -webkit-linear-gradient 正在打破 -moz-linear-gradient 听说是个难题,我使用的是带有两个 handle 的 Jquery Slider 插件,我
我突然想到,例如,假设我们有二维 N 点的训练数据。我们知道我们总是可以天真地构建一个决策树,以便我们可以对每个数据点进行分类。 (可能我们过拟合了,深度可以到2N) 但是,我们知道如果数据集是线性可
我对插值函数的一般求根问题感兴趣。 假设我有以下 (x, y)数据: set.seed(0) x = x[i]) & (rroots <= x[i + 1])] ## next piece
我有一个线性优化目标来最大化 EE+FF,其中 EE 和 FF 每个都包含一些 C 和 D。 使用我编写的代码,我可以让求解器找到: EE_quantity: 0, FF_quantity: 7 ..
我一直在寻找这个问题,但我无法理解这个问题的含义。 问题: Write a program in any language to determine how your computer handles
我在 Python 中有一个简单的线性多元回归,如下所示: X_train,X_test,y_train,y_test=train_test_split(x_cols,df['Volume'],tes
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
对于大型稀疏迭代(共轭梯度、MINRES、GMRES 等)线性代数系统求解,有哪些更好的库?我经常编写自己的程序,但我很想知道人们更喜欢哪种“现成的”软件包。我听说过 PETSc、TAUCS、IML+
这是我的全部问题: 信息: *最大限度。总投资:125美元 *支付是购买的单位x支付/单位的总和 *每笔投资成本:买入成本+成本/单位x单位数量(如果您购买至少一个单位) *费用为每笔投资费用之和 限
我有两个变量:x>= 0 和 y 二进制(0 或 1),我有一个常数 z >= 0。如何使用线性约束来描述以下条件: If x = z then y = 1 else y = 0. 我试图通过定义另一
我正在尝试做一些逻辑上应该可以做的事情。但是,我不确定如何在线性规划领域内做到这一点。我正在使用 ZMPL/SCIP,但这对大多数人来说应该是可读的。 set I := {1,2,3,4,5}; pa
我正在为我的期中考试做准备,我正在解决算法书中的一些问题,但似乎无法弄清楚以下问题: 在实数 a 和 b 上找出线性规划的充分必要条件 max: x+y ax + by 0 (a) 是不可行的。 (b
我正在做一个 Java 项目,我必须计算一个多元线性回归,但我希望得到的参数是非负的。是否有现有的商业友好许可图书馆来做这样的事情?我一直在寻找非负最小二乘库,但没有成功。 最佳答案 好吧,我找不到任
密集线性代数在现实世界中的常见应用是什么? 使用线性代数作为人机之间的通用语言,可以轻松描述和高效计算许多问题。尽管这些系统通常需要稀疏矩阵的解,而不是稠密矩阵。违反此规则的常见应用有哪些? 我很好奇
我如何找到 广义特征值、向量 使用 Eigen3 图书馆? 在 Octave ,matlab,特征值函数的形式是:[V, lambda] = eig (A, B) . 我只能找到 this 类(cla
我是一名优秀的程序员,十分优秀!