在尝试训练SGDClassifier进行二元分类时出现位置参数错误

我正在学习Aurelien Geron的《Hands-On ML》教材,但在尝试训练SGDClassifier时遇到了困难。

我使用的是MNIST手写数字数据,并通过Anaconda在Jupyter Notebook中运行代码。我的Anaconda版本是1.7.0,sklearn版本是0.20.dev0,都已更新。我已经粘贴了用来加载数据、选择前60,000行、打乱顺序以及将所有数字5的标签转换为1(True),其他数字转换为0(False)的代码。X_train和y_train_5都是numpy数组。

我已经在下面粘贴了错误消息。

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

代码

from six.moves import urllibfrom scipy.io import loadmatimport  numpy as npfrom  sklearn.linear_model  import SGDClassifier# Load MNIST data #from scipy.io import loadmatmnist_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)

错误消息

---------------------------------------------------------------------------TypeErrorTraceback (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 urllibfrom scipy.io import loadmatimport numpy as npfrom sklearn.linear_model  import SGDClassifierfrom 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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注