【问题标题】:SVM+HOG object detectorSVM+HOG目标检测器
【发布时间】:2021-09-06 16:43:07
【问题描述】:

我在训练 SVM+HOG 对象检测器时遇到了一个问题,这就是我所做的。 我将所有功能都放在了一个名为 features 的列表中并使用了

X_scaler = StandardScaler().fit(features)
scaled_X = X_scaler.transform(features)
rand_state = np.random.randint(0, 100)
X_train, X_test, y_train, y_test = train_test_split(np.array(features), labels, test_size=0.3, random_state=rand_state)

根据sklearn.preprocessing.StandardScalerStandardScaler() 中的这种变换是基于所有训练样本的均值和标准差。那么问题来了,如果我只想在 1 个新看到的样本上测试我训练的 SVM,我该如何应用StandardScaler()?因为我无法仅从 1 个样本中计算平均值和标准差。

据我了解,如果我想在新数据(不是x_test)上测试 SVM,我需要从训练中遵循相同的程序。因此,我尝试从多个新看到的样本中提取 HOG 特征并附加到另一个名为 test_feature 的列表中,然后

X_scaler = StandardScaler().fit(test_feature)
scaled_X = X_scaler.transform(test_feature_feature)

似乎工作正常,SVM产生正确的输出,但是当len(test_feature) == 1,无论我用StandardScaler()转换test_feature还是直接使用y_pred = clf.predict(np.array(test_feature)),输出都是垃圾。

有没有cmets?

【问题讨论】:

    标签: python scikit-learn svm


    【解决方案1】:

    您只需要在训练数据上拟合您的StandardScaler(),否则您的均值和方差将是偏差,因为它们是使用测试数据计算的。安装此变压器后,您可以对测试数据和新样本执行transform(),这将根据计算的均值和方差对其进行缩放。

    你应该:

    1. train_test_split()
    2. fit() 你的 StandardScaler() 使用火车数据
    3. fit() 使用转换后的训练集的模型
    4. transform()你的测试数据
    5. predict()转换后的测试数据

    如下:

    X_train, X_test, y_train, y_test = train_test_split(np.array(features), labels, test_size=0.3, random_state=rand_state)
    
    scaler = StandardScaler()
        
    X_train = scaler.fit_transform(X_train)
    
    clf.fit(X_train, y_train)
    
    X_test = scaler.transform(X_test)
    
    clf.predict(X_train)
    

    【讨论】:

      【解决方案2】:

      只需调用 transform。正如您所说,标准缩放器在转换数据时使用训练集的均值和标准差。已使用训练集计算的相同均值和标准差将应用于新数据。无需重新计算这些参数。

      from sklearn.preprocessing import StandardScaler
      data = [[0, 0], [0, 0], [1, 1], [1, 1]]
      scaler = StandardScaler()
      # calling fit will calculate the mean and std
      print(scaler.fit(data))
      # print out the calcualted mean for example
      print(scaler.mean_)
      # transform a new data point
      print(scaler.transform([[2, 2]]))
      

      【讨论】:

        猜你喜欢
        • 2019-01-03
        • 2017-12-23
        • 2016-05-01
        • 1970-01-01
        • 2016-08-13
        • 1970-01-01
        • 1970-01-01
        • 2012-06-01
        • 2019-12-03
        相关资源
        最近更新 更多