将一维向量拟合到SVC线性核

2024-09-30 01:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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: Singleton array array(162) cannot be considered a valid collection.


Tags: 数据fromtest图像datanptrain像素
1条回答
网友
1楼 · 发布于 2024-09-30 01:21:11

sklearn希望您的X_列数组是一个二维数组,如(n_示例,1),Y_列是一个1d标签数组,如(n_示例,1)

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

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)

相关问题 更多 >

    热门问题