【问题标题】:Fitting a 1d vector to SVC linear kernel将一维向量拟合到 SVC 线性核
【发布时间】:2020-11-07 17:05:57
【问题描述】:

我正在尝试将 SVC 与线性内核一起用于图像识别任务。我当前的数据是一个 2x5 矩阵

[['Face 1' 'Face2' 'Face 3' 'Face 4' 'Face 5']
 ['229.0' '230.0' '231.0' '230.0' '230.0']]

我的第二行是我的 X 特征,它们是来自不同图像的像素强度值。

我的第一行是我的 Y 标签,它们是从中提取像素的面部图像。 我正在尝试不惜一切代价将我的数据输入 SVC。

我正在尝试的是:

    m_array = [[229, 230, 231, 230, 230]]
    faces = []
    faces = np.asarray(['Face 1', 'Face2', 'Face 3', 'Face 4', 'Face 5']).reshape(-1, 5)
    
    data = np.stack((faces, m_array)).reshape(2, 5)
    df = pd.DataFrame(data)
    X = data[1, :]
    Y = data[0, :]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)


from sklearn.svm import SVC
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)

我想测试这些功能的识别率,但我得到了错误:

TypeError: 单例数组 array(162) 不能被认为是有效的 收藏。

【问题讨论】:

    标签: python python-3.x numpy machine-learning scikit-learn


    【解决方案1】:

    sklearn 期望您的 X_train 数组是二维数组,例如 (n_examples, 1),而 Y_train 是一维标签数组,例如 (n_examples, )。

    我重新格式化了您的代码以删除一些不必要的步骤并解决问题:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.model_selection import train_test_split
    from sklearn.svm import SVC
    
    m_array = np.array([229, 230, 231, 230, 230])[:, np.newaxis]
    faces = np.array(['Face 1', 'Face2', 'Face 3', 'Face 4', 'Face 5'])
    
    X_train, X_test, y_train, y_test = train_test_split(m_array, faces, test_size = 0.20)
    
    svclassifier = SVC(kernel='linear')
    svclassifier.fit(X_train, y_train)
    

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 2014-04-10
      • 2014-07-06
      • 1970-01-01
      • 2023-01-05
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多