-1

Problem

New error on problem:

UserWarning: X has feature names, but LogisticRegression was fitted without feature names
  warnings.warn(UserWarning: X has feature names, but LogisticRegression was fitted without feature names

Goal is to use RandomisedSearchCV for tune parameter, then fit two models, one each for OneVsOneClassifier and OneVsRestClassifier. Then check accuracy performance of each models using tuned parameter from RandomizedSearchCV. Define two models to fit, to fit on digit recognition MNIST dataset for multi-label classification prediction.

I setup a tune hyper=parameters using GridSearchCV, just simple for estimator__C': [0.1, 1, 100, 200] for LogisticRegression. For audit, I print the computed grid parameters. Provide a scaled X-train object to the fit model. Then run the fit model.

Problem was running on Kaggle GPU P100. When I execute code: ovr_grid_search.fit() & ovo_grid_search.fit(), finish running and next step error is this by Adding verbose=1 and error_score="raise" to RandomainedSearchCV classifier, and determined that needed to relocate StandardScaler with MinMaxScaler

Error Error is LogisticRegression fit with out features.

/Users/matthew/opt/anaconda3/lib/python3.9/site-packages/sklearn/base.py:443: 
UserWarning: X has feature names, 
but LogisticRegression was fitted without feature names

Code

from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsOneClassifier, OneVsRestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))

ovr_model = OneVsRestClassifier(LogisticRegression())
ovo_model = OneVsOneClassifier(LogisticRegression())

param_grid = {
    'estimator__C': [0.1, 1, 100, 200]
}

ovr_grid_param = RandomizedSearchCV(ovr_model, param_grid, cv=5, n_jobs=8)
ovo_grid_param = RandomizedSearchCV(ovo_model, param_grid, cv=5, n_jobs=8)

print("OneVsRestClassifier best params: ", ovr_grid_param)
print("OneVsOneClassifier best params: ", ovo_grid_param)

min_max_scaler = preprocessing.MinMaxScaler()
X_train_scaled = min_max_scaler.fit_transform(X_train)

### below code is the problem area **

ovr_grid_param.fit(X_train_scaled, y_train)
ovo_grid_param.fit(X_train_scaled, y_train)

Data The digit recognition MNIST dataset. X_train scaled data

array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

y_train data

33092    5
30563    2
17064    4
16679    9
30712    0
30177    0
11735    3
1785     8
4382     3
21702    7
37516    3
9476     6
4893     5
22117    0
12646    8

RandomisedSearch execution results

OneVsRestClassifier best params:  RandomizedSearchCV(cv=5, error_score='raise',
                   estimator=OneVsRestClassifier(estimator=LogisticRegression()),
                   n_jobs=3,
                   param_distributions={'estimator__C': [0.1, 1, 100, 200],
                                        'estimator__max_iter': [2500, 4500,
                                                                6500, 9500,
                                                                14000]})
OneVsOneClassifier best params:  RandomizedSearchCV(cv=5, error_score='raise',
                   estimator=OneVsOneClassifier(estimator=LogisticRegression()),
                   n_jobs=3,
                   param_distributions={'estimator__C': [0.1, 1, 100, 200],
                                        'estimator__max_iter': [2500, 4500,
                                                                6500, 9500,
                                                                14000]})
manager_matt
  • 395
  • 4
  • 19
  • 1
    Is it actually in an infinite loop or simply taking a long time? MNIST is a big dataset and you're using one-vs-rest and one-vs-one logistic regression, so thats 10 and 90 sub-classifiers, respectively, on CPU-only. Set `verbose=1` to see what it's doing. – Aaron Keesing Mar 12 '23 at 22:22
  • just published the fit(x, y, verbose=1), now setting error_score='raise' – manager_matt Mar 12 '23 at 22:57
  • 1
    No, don't put `verbose=1` in `fit()`, set it on the classifiers and grid search objects. – Aaron Keesing Mar 12 '23 at 23:04
  • ok moved verbose, and error_score="raise" to classifier. my X_train is (12600, 784), I am just using train/test at 0.70 to get past this issue – manager_matt Mar 12 '23 at 23:16

1 Answers1

0

I am unaware of sklearn GPU support https://scikit-learn.org/stable/faq.html#will-you-add-gpu-support

Is there a way to run ‘nvidia-smi’? You can check if there is a process active on P100. Otherwise, it may be just be taking too long because the grid search space is too large. Try increasing the n_jobs parameter to take advantage of more cpus.

Thomas K
  • 25
  • 7
  • Thomas, I added n_jobs=4 for advantage on more cpu's, still waiting... – manager_matt Mar 12 '23 at 19:08
  • GridSearch can take a long time to run. I might suggest trying RandomSearchCV( https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RandomizedSearchCV.html). I tend to find performance is not only faster, but it typically finds better hyperparameters – Thomas K Mar 12 '23 at 19:20
  • 1
    MNIST is a large dataset, and it appears you are running gridsearch on an already intensive task of OneVsRest/One Classifier. I typically run parallel jobs like this with many more cpus. Try creating a development dataset with a size of 10 to see if it will finish quickly – Thomas K Mar 12 '23 at 19:25
  • Perhaps its my paltry internet connection, so I driving out to find a better connection today – manager_matt Mar 12 '23 at 21:41
  • Question for this dataset, should both X and Y be scaled? Or only X? – manager_matt Mar 12 '23 at 21:55
  • You can use another hyperparameter optimization framework like `Optuna`, take a look at the [documentation](https://optuna.org/). It is design to be faster, there is an interesting Medium article on it [here](https://medium.com/towards-data-science/hyper-parameter-optimization-with-optuna-4920d5732edf). – Adrien Riaux Mar 12 '23 at 22:18
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 17 '23 at 16:17