a-agmon / pu-learn

27 stars 9 forks source link

Invalid classes inferred from unique values of `y`. Expected: [0 1], got [-1 1] #2

Open hcleelove opened 2 years ago

hcleelove commented 2 years ago

when i run the code:

predicted = np.zeros(len(x_data)) learning_iterations = 12 for index in range(learning_iterations): pu_estimator, probs1y1 = fit_PU_estimator(x_data, y_labeled, 0.2, xgb.XGBClassifier()) predicted += predict_PU_prob(x_data, pu_estimator, probs1y1) if(index%3 == 0): print(f'Learning Iteration::{index}/{learning_iterations} => P(s=1|y=1)={round(probs1y1,2)}')

it show:


ValueError Traceback (most recent call last) Input In [13], in <cell line: 3>() 2 learning_iterations = 12 3 for index in range(learning_iterations): ----> 4 pu_estimator, probs1y1 = fit_PU_estimator(x_data, y_labeled, 0.2, xgb.XGBClassifier()) 5 predicted += predict_PU_prob(x_data, pu_estimator, probs1y1) 6 if(index%3 == 0):

Input In [12], in fit_PU_estimator(X, y, hold_out_ratio, estimator) 17 y = np.delete(y, hold_out) 18 # We fit the estimator on the unlabeled samples + (part of the) positive and labeled ones. 19 # In order to estimate P(s=1|X) or what is the probablity that an element is labeled ---> 20 estimator.fit(X, y) 21 # We then use the estimator for prediction of the positive held-out set 22 # in order to estimate P(s=1|y=1) 23 hold_out_predictions = estimator.predict_proba(X_hold_out)

File ~\miniconda3\lib\site-packages\xgboost\core.py:532, in _deprecate_positional_args..inner_f(*args, kwargs) 530 for k, arg in zip(sig.parameters, args): 531 kwargs[k] = arg --> 532 return f(kwargs)

File ~\miniconda3\lib\site-packages\xgboost\sklearn.py:1357, in XGBClassifier.fit(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights, callbacks) 1352 expected_classes = np.arange(self.nclasses) 1353 if ( 1354 self.classes_.shape != expectedclasses.shape 1355 or not (self.classes == expected_classes).all() 1356 ): -> 1357 raise ValueError( 1358 f"Invalid classes inferred from unique values of y. " 1359 f"Expected: {expectedclasses}, got {self.classes}" 1360 ) 1362 params = self.get_xgb_params() 1364 if callable(self.objective):

ValueError: Invalid classes inferred from unique values of y. Expected: [0 1], got [-1 1]

so, how i can deal with the code ?

athieffry commented 2 years ago

See this stack overflow post.

That happens because the class column has to start from 0 (as required since version 1.3.2).
An easy way to solve that is using LabelEncoder from sklearn.preprocssing library.

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)