我的实验需要将短文本、情感分数、文本字符长度三种特征输入到模型里训练,但找到的代码是只输入了文本数据,在网上也没有找到结合多个特征的详细代码。
通过请教学姐后,我自己修修改改终于不报错了。如果各位觉得我写的哪里有问题或有更好的方法,请一定要告诉我哇🥺🙌
首先,我的csv数据集格式:
body,label,emotion,length
为什么更新了进不去,3,0.1020974195144213,9
输入数据,对body进行分词和去除停用词后,将各特征存在列表中:
corpus.append((words, item[1], item[2], item[3]))
由于我的数据量大,所以还要多进程读取数据。接着,将各特征向量化并组合到一起:
vec, label = vc.load_w2v(corpus)
在load_w2v方法中,我load的是tencent-ailab-embedding模型,对文本进行向量化:
word_vecs.append(w2v_model[word])
将各特征都转换成相同的dtype,不然会报错;将item[2]和item[3]先合并到一个temp1列表中,再将temp1与文本向量水平concatenate,便成功将多个特征合并到一起了:
word_vecs_ = np.array(word_vecs, dtype='float')
lla = np.array(item[1], dtype='float')
temp.extend([item[2], item[3]])
temp1 = np.array(temp, dtype='float')
fe = np.concatenate([np.mean(word_vecs_, axis=0), temp1], axis=0)
vector.append(fe)
temp.clear()
label.append(lla)
将得到的vec和label输入到模型中:
def classify_svm(self,vec,label):
vec_ = np.array(vec)
X_train, X_test, y_train, y_test = train_test_split(vec_, label, test_size=0.2, random_state=1)
……