gpt4 book ai didi

python - 尝试在二进制分类上训练 SGDClassifier 时出现位置参数错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:10 27 4
gpt4 key购买 nike

我正在处理 Aurelien Geron's Hands-On ML textbook并且在尝试训练 SGDClassifier 时遇到了困难。

我正在使用 MNIST 手写数字数据并通过 Anaconda 在 Jupyter Notebook 中运行我的代码。我的 anaconda (1.7.0) 和 sklearn (0.20.dev0) 都更新了。我粘贴了用于加载数据的代码,选择前 60k 行,打乱顺序并将所有 5 的标签转换为 1(真),所有其他数字的标签转换为 0(假)。 X_train 和 y_train_5 都是 numpy 数组。

我已将收到的错误消息粘贴到下方。

数据的维度似乎没有问题,我尝试将 X_train 转换为稀疏矩阵(SGDClassifier 的建议格式)和各种 max_iter 值,但每次都得到相同的错误消息。我错过了一些明显的东西吗?我需要使用不同版本的 sklearn 吗?我在网上搜索过,但找不到任何描述 SGDClassifier 类似问题的帖子。如果有任何指示,我将不胜感激。

代码

from six.moves import urllib
from scipy.io import loadmat
import numpy as np
from sklearn.linear_model import SGDClassifier


# Load MNIST data #

from scipy.io import loadmat
mnist_alternative_url = "https://github.com/amplab/datascience-
sp14/raw/master/lab7/mldata/mnist-original.mat"
mnist_path = "./mnist-original.mat"
response = urllib.request.urlopen(mnist_alternative_url)
with open(mnist_path, "wb") as f:
content = response.read()
f.write(content)
mnist_raw = loadmat(mnist_path)
mnist = {
"data": mnist_raw["data"].T,
"target": mnist_raw["label"][0],
"COL_NAMES": ["label", "data"],
"DESCR": "mldata.org dataset: mnist-original",
}


# Assign X and y #

X, y = mnist['data'], mnist['target']


# Select first 60000 numbers #

X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000],
y[60000:]


# Shuffle order #

shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]


# Convert labels to binary (5 or "not 5") #

y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

# Train SGDClassifier #

sgd_clf = SGDClassifier(max_iter=5, random_state=42)
sgd_clf.fit(X_train, y_train_5)

错误信息

---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-10-5a25eed28833> in <module>()
37 # Train SGDClassifier
38 sgd_clf = SGDClassifier(max_iter=5, random_state=42)
---> 39 sgd_clf.fit(X_train, y_train_5)

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in fit(self, X, y, coef_init, intercept_init, sample_weight)
712 loss=self.loss, learning_rate=self.learning_rate,
713 coef_init=coef_init, intercept_init=intercept_init,
--> 714 sample_weight=sample_weight)
715
716

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _fit(self, X, y, alpha, C, loss, learning_rate, coef_init, intercept_init, sample_weight)
570
571 self._partial_fit(X, y, alpha, C, loss, learning_rate, self._max_iter,
--> 572 classes, sample_weight, coef_init, intercept_init)
573
574 if (self._tol is not None and self._tol > -np.inf

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _partial_fit(self, X, y, alpha, C, loss, learning_rate, max_iter, classes, sample_weight, coef_init, intercept_init)
529 learning_rate=learning_rate,
530 sample_weight=sample_weight,
--> 531 max_iter=max_iter)
532 else:
533 raise ValueError(

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _fit_binary(self, X, y, alpha, C, sample_weight, learning_rate, max_iter)
587 self._expanded_class_weight[1],
588 self._expanded_class_weight[0],
--> 589 sample_weight)
590
591 self.t_ += n_iter_ * X.shape[0]

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in fit_binary(est, i, X, y, alpha, C, learning_rate, max_iter, pos_weight, neg_weight, sample_weight)
419 pos_weight, neg_weight,
420 learning_rate_type, est.eta0,
--> 421 est.power_t, est.t_, intercept_decay)
422
423 else:

~\Anaconda3\lib\site-packages\sklearn\linear_model\sgd_fast.pyx in sklearn.linear_model.sgd_fast.plain_sgd()

TypeError: plain_sgd() takes at most 21 positional arguments (25 given)

最佳答案

看来您的 scikit-learn 版本有点过时了。尝试运行:

pip install -U scikit-learn

然后您的代码将运行(有一些轻微的格式更新):

from six.moves import urllib
from scipy.io import loadmat
import numpy as np
from sklearn.linear_model import SGDClassifier
from scipy.io import loadmat

# Load MNIST data #
mnist_alternative_url = "https://github.com/amplab/datascience-sp14/raw/master/lab7/mldata/mnist-original.mat"
mnist_path = "./mnist-original.mat"
response = urllib.request.urlopen(mnist_alternative_url)
with open(mnist_path, "wb") as f:
content = response.read()
f.write(content)
mnist_raw = loadmat(mnist_path)
mnist = {
"data": mnist_raw["data"].T,
"target": mnist_raw["label"][0],
"COL_NAMES": ["label", "data"],
"DESCR": "mldata.org dataset: mnist-original",
}

# Assign X and y #
X, y = mnist['data'], mnist['target']

# Select first 60000 numbers #
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]

# Shuffle order #
shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]

# Convert labels to binary (5 or "not 5") #
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

# Train SGDClassifier #
sgd_clf = SGDClassifier(max_iter=5, random_state=42)
sgd_clf.fit(X_train, y_train_5)

关于python - 尝试在二进制分类上训练 SGDClassifier 时出现位置参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com