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

时间:2018-08-13 15:18:04

标签: python machine-learning scikit-learn classification gradient-descent

我正在研究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)

1 个答案:

答案 0 :(得分:2)

您的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)