【问题标题】:Inconsistent number of samples error in SVM accuracy calculationSVM精度计算中的样本数不一致错误
【发布时间】:2021-12-06 16:05:23
【问题描述】:

我正在尝试使用拉普拉斯内核(作为预计算内核)计算 SVM 的准确度分数。但是,当我尝试计算准确度分数时,出现如下错误。

我的代码:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
from sklearn.metrics.pairwise import laplacian_kernel

#Load the iris data
iris_data = load_iris()

#Split the data and target
X = iris_data.data
y = iris_data.target

#Convert X and y to a numpy array
X = np.array(X)
y = np.array(y)

#Perform train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42, shuffle=True)

#Using Laplacian kernel - https://scikit-learn.org/stable/modules/metrics.html#laplacian-kernel
K = np.array(laplacian_kernel(X_train, gamma=.5))
svm = SVC(kernel='precomputed').fit(K, np.ravel(y_train))
pred_y = svm.predict(K)

#Print accuracy score - here is where the error is happening.
print(accuracy_score(y_test, pred_y))

当我运行此代码时,我收到如下所示的错误:

Traceback (most recent call last):
  File "/Users/user/Desktop/Research/Src/Laplace.py", line 36, in <module>
    print(accuracy_score(y_test, pred_y))
  File "/Users/user/miniforge3/envs/user_venv/lib/python3.8/site-packages/sklearn/utils/validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "/Users/user/miniforge3/envs/user/lib/python3.8/site-packages/sklearn/metrics/_classification.py", line 202, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/Users/user/miniforge3/envs/user/lib/python3.8/site-packages/sklearn/metrics/_classification.py", line 83, in _check_targets
    check_consistent_length(y_true, y_pred)
  File "/Users/user/miniforge3/envs/user/lib/python3.8/site-packages/sklearn/utils/validation.py", line 262, in check_consistent_length
    raise ValueError("Found input variables with inconsistent numbers of"
ValueError: Found input variables with inconsistent numbers of samples: [45, 105]

那么我该如何解决这个错误呢?

【问题讨论】:

  • 请不要使用带有虹膜数据的本地文件,而是使用 scikit-learn 中的虹膜数据发布一个完全可重现的示例。另外,我们在代码中有标准的 cmets 表示法,我们不使用任何我们喜欢的东西(已编辑)。
  • @desertnaut:使用可重现的示例编辑了代码。

标签: python numpy machine-learning scikit-learn svm


【解决方案1】:

您使用具有 105 个元素的火车输入计算了 pred_y,而 y_test 有 45 个元素。

你需要添加一个步骤:

#user3046211's code

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
from sklearn.metrics.pairwise import laplacian_kernel

#Load the iris data
iris_data = load_iris()

#Split the data and target
X = iris_data.data
y = iris_data.target

#Convert X and y to a numpy array
X = np.array(X)
y = np.array(y)

#Perform train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42, shuffle=True)

#Using Laplacian kernel - https://scikit-learn.org/stable/modules/metrics.html#laplacian-kernel
K = np.array(laplacian_kernel(X_train, gamma=.5))
svm = SVC(kernel='precomputed').fit(K, np.ravel(y_train))
pred_y = svm.predict(K)

#Print accuracy score - here is where the error is happening.
print(accuracy_score(y_test, pred_y))

# NEW CODE STARTS HERE
K_test = np.array(laplacian_kernel(X=X_test,Y=X_train, gamma=.5))
pred_y_test = svm.predict(K_test)
print(accuracy_score(y_test, pred_y_test))

【讨论】:

  • 我添加了你提到的行,但它抛出了一个值错误 ValueError: X.shape[1] = 45 should be equal to 105,即训练时的样本数。你能用我的代码展示一下,这样会更清楚吗?
  • 我插入了你所有的代码;新代码刚刚结束。我最初添加的问题是内核需要预先计算测试和推理训练数据之间的成对距离。
猜你喜欢
  • 2013-07-27
  • 2018-12-01
  • 2016-09-10
  • 1970-01-01
  • 2015-12-05
  • 2016-04-23
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多