import scipy.io as sio
matfn = './data/mnist-original.mat'
data = sio.loadmat(matfn)
X = np.rot90(data['data'])
Y = np.rot90(data['label'])
X_train, X_test, y_train, y_test = X[:60000], X[60000:], Y[:60000], Y[60000:]
y_train_9 = (y_train == 9)
y_test_9 = (y_test == 9)
from sklearn.linear_model import SGDClassifier
sgd = SGDClassifier(random_state=315)
sgd.fit(X_train, y_train_9)
print(sgd.predict([X[12814], X[1287]]))
from sklearn.model_selection import cross_val_predict
y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3)
# y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3, method= 'decision_function')
print(y_scores)
運行1:
y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3)
運行結果:[ True True True ... False False False]
運行2:
y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3, method= 'decision_function')
運行報錯:
ValueError Traceback (most recent call last)
<ipython-input-11-297d1a694814> in <module>
17 from sklearn.model_selection import cross_val_predict
18 # y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3)
---> 19 y_scores = cross_val_predict(sgd, X_train, y_train_9, cv = 3, method= 'decision_function')
20 print(y_scores)
21
D:\anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in cross_val_predict(estimator, X, y, groups, cv, n_jobs, verbose, fit_params, pre_dispatch, method)
753 prediction_blocks = parallel(delayed(_fit_and_predict)(
754 clone(estimator), X, y, train, test, verbose, fit_params, method)
--> 755 for train, test in cv.split(X, y, groups))
756
757 # Concatenate the predictions
D:\anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
1002 # remaining jobs.
1003 self._iterating = False
-> 1004 if self.dispatch_one_batch(iterator):
1005 self._iterating = self._original_iterator is not None
1006
D:\anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator)
833 return False
834 else:
--> 835 self._dispatch(tasks)
836 return True
837
D:\anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
752 with self._lock:
753 job_idx = len(self._jobs)
--> 754 job = self._backend.apply_async(batch, callback=cb)
755 # A job can complete so quickly than its callback is
756 # called before we get here, causing self._jobs to
D:\anaconda3\lib\site-packages\joblib\_parallel_backends.py in apply_async(self, func, callback)
207 def apply_async(self, func, callback=None):
208 """Schedule a func to be run"""
--> 209 result = ImmediateResult(func)
210 if callback:
211 callback(result)
D:\anaconda3\lib\site-packages\joblib\_parallel_backends.py in __init__(self, batch)
588 # Don't delay the application, to avoid keeping the input
589 # arguments in memory
--> 590 self.results = batch()
591
592 def get(self):
D:\anaconda3\lib\site-packages\joblib\parallel.py in __call__(self)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items]
257
258 def __len__(self):
D:\anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
254 with parallel_backend(self._backend, n_jobs=self._n_jobs):
255 return [func(*args, **kwargs)
--> 256 for func, args, kwargs in self.items]
257
258 def __len__(self):
D:\anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_predict(estimator, X, y, train, test, verbose, fit_params, method)
852 n_classes = len(set(y)) if y.ndim == 1 else y.shape[1]
853 predictions = _enforce_prediction_order(
--> 854 estimator.classes_, predictions, n_classes, method)
855 return predictions, test
856
D:\anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _enforce_prediction_order(classes, predictions, n_classes, method)
898 'is not supported for decision_function '
899 'with imbalanced folds. {}'.format(
--> 900 len(classes), n_classes, recommendation))
901
902 float_min = np.finfo(predictions.dtype).min
ValueError: Only 2 class/es in training fold, but 1 in overall dataset. This is not supported for decision_function with imbalanced folds. To fix this, use a cross-validation technique resulting in properly stratified folds
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/49845.html
上一篇:求助![Errno 10109] get addr info failed,從來沒遇到過這個報錯
下一篇:sprite的使用
