我正在尝试为数据集构建svm分类器,我知道svm分类器可用于2D数组,但是此代码不起作用,因为程序将newtemp2
视为3D数组,所以我想知道我必须为我的数据使用svm classifier
做些什么。
train_setfeat = []
train_setlabel = []
newtemp2=[]
for vector in newtemp:
newtemp2.append(np.reshape(vector, (431, 19)))
#convert each vector to 2d array
j = 0
for vector in newtemp2:
if j < 2100: # 70 % for train
train_setfeat.append(vector)
train_setlabel.append(classlabels[j])
j += 1
else:
break
test_setfeat = []
test_setlabel = []
j = 0
for vector in newtemp2:
if j < 2997 and j >= 2100: #20 % for test
test_setfeat.append(vector)
test_setlabel.append(classlabels[j])
if j>= 3000:
break
j += 1
classifier1 = svm.SVC(kernel='linear')
classifier1.fit(train_setfeat, train_setlabel)
#sample of newtemp data
newtemp =[
(0.05,0.0,0.0,0.02,0.0),
(0.0,0.0,0.0,0.02,0.0),
(0.05,0.0,0.0)]
如果找到单词,则数据集中的每个句子都表示为矢量0.0,否则将单词的权重
答案 0 :(得分:0)
在使用列表和numpy数组组合创建训练集时遇到一些问题。
尝试这部分代码,应通过使用以下代码替换第3-5行来解决您的问题:
N=len(newtemp)
newtemp2=np.empty(N,431,19)
i=0;
for vector in newtemp:
newtemp2[i,:]=np.reshape(vector, (431, 19)))
i+=1
您可以对其余代码执行相同的操作