简体   繁体   English

如何使用两个特征向量训练svm?

[英]How to train svm with using two feature vectors?

I have two numpy arrays(features). 我有两个numpy数组(功能)。 The dimensions of numpy arrays are : numpy数组的尺寸为:

audio=(360,13) ---> Features are extracted from audio files audio =(360,13)--->从音频文件中提取特征

image=(360,5)--> Features are extracted from the spectrogram of these audio files. image =(360,5)->特征是从这些音频文件的频谱图中提取的。

I want to use these two arrays together to train svm classifier. 我想一起使用这两个数组来训练svm分类器。 But I know svm train get just one array. 但是我知道svm train只能得到一个数组。 (svm.train(feature , label)). (svm.train(feature,label))。 I am looking for is there anything like svm.train(audio,image,label) 我正在寻找是否有像svm.train(音频,图像,标签)之类的东西

I also tried to concatenate these two arrays but the dimensions are different. 我也尝试连接这两个数组,但是维数不同。 How can I solve this situation ? 我该如何解决这种情况?

While @Saedeas provided a simple solution, I would suggest going in slightly different way. 虽然@Saedeas提供了一个简单的解决方案,但我建议采用略有不同的方式。

Concatenation is good for homogenous features, it does not work well when data comes from completely different modalities (like audio + video). 串联对于同质功能很有用,当数据来自完全不同的形式(例如音频和视频)时,它不能很好地工作。 However, one can deal with that using simple properties of kernel functions (which are the base of SVMs), namely sum of two kernels is a kernel, so we can define: 但是,可以使用内核函数的简单属性(这是SVM的基础)来解决这一问题,即两个内核的总和是一个内核,因此我们可以定义:

K_{audio x video}(x,y) = a K_{video}(x_{video}, y_{video}) + 
                         (1-a) K_{audio}(x_{audio},y_{audio})

so given kernels for each modality separately, we define a joint kernel on top of it, where a is a hyperparameter ae [0,1] to be adjusted. 因此,分别为每个模态指定内核,我们在其顶部定义一个联合内核,其中a是要调整的超参数ae [0,1]。

Code-wise it might be done in a similar way to what is already suggested: 在代码方面,可以按照与建议的方式类似的方式进行:

# First concat, but only for easier handling
new_data = np.concatenate((audio,image), axis=1)
y = ...

def video_kernel(X, Y):
  ...

def audio_kernel(X, Y):
  ...

# now new kernel
def new_kernel(X, Y, a=0.5):
  return a*audio_kernel(X[:, :13], Y[:, :13]) + (1-a)*video_kernel(X[:, 13:], Y[:, 13:])

svm = SVC(kernel=new_kernel)
svm.fit(new_data, y)

Maybe I'm misreading, but how are the dimensions an issue? 也许我读错了,但是尺寸是个问题吗?

You have 360 samples in each array, one has 13 dimensions, the other has 5. Turn this into a single array of 360 samples with 18 dimensions. 每个数组中有360个样本,一个具有13个维,另一个具有5个维。将其转换为一个具有18个维的360个样本的单个数组。

You may need to normalize the values, but the concatenation should just be: 您可能需要规范化这些值,但是串联应该只是:

new_data = np.concatenate((audio,image), axis=1)

You might normalize the data values by making them zero mean, unit variance (find the mean and variance in each dimension, subtract the means from every sample, and divide by the variances). 您可以通过将数据值设为零均值,单位方差(找到每个维度的均值和方差,从每个样本中减去均值并除以方差)来对数据值进行归一化。

means = np.mean(new_data, axis=0)
vars = np.var(new_data, axis=0)
norm_data = (new_data - means) / vars

Edit: You might still normalize in this way, but I'd use @lejlot's solution. 编辑:您可能仍然以这种方式规范化,但是我将使用@lejlot的解决方案。 The multiple kernel approach makes a lot of sense and is more flexible than this approach. 多核方法比这种方法有意义,并且更灵活。

You can concatenate them. 您可以将它们串联起来。 For example like there: http://scikit-learn.org/stable/modules/pipeline.html#feature-union , or you can train two SVM and work with prediction results. 例如: http : //scikit-learn.org/stable/modules/pipeline.html#feature-union ,或者您可以训练两个SVM并处理预测结果。 Example: http://scikit-learn.org/stable/modules/ensemble.html#voting-classifier . 示例: http : //scikit-learn.org/stable/modules/ensemble.html#voting-classifier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM