Thanks to visit codestin.com
Credit goes to github.com

Skip to content

VotingClassifier and roc_auc TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe' and #15056

@KonstantinKlepikov

Description

@KonstantinKlepikov

Description

VotingClassifier
TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'

Steps/Code to Reproduce

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, Normalizer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score

pipePre = Pipeline([
    ('simpleimputer', SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)),
    ('standardscaler', StandardScaler()),
    ('normalizer', Normalizer())
     ])

df_train_x = pipePre.fit_transform(df_train_x)

X_train, X_test, y_train, y_test = train_test_split(df_train_x, df_train_y, test_size = 0.25, random_state=42)

lrg = LinearRegression().fit(X_train, y_train)

rig = Ridge().fit(X_train, y_train)

lreg = LogisticRegression().fit(X_train, y_train)

voting = VotingClassifier(estimators=[('lrg_v', lrg), ('rig_v', rig), 
                                      ('lreg_v', lreg)], voting='hard')
voting_fit = voting.fit(X_train, y_train)

y_pred = voting_fit.predict(X_test)
roc_auc_score(y_test, y_pred)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-506a80086b81> in <module>
----> 1 val_error(voting_fit, X_test, y_test)

<ipython-input-6-0fa46ec754f8> in val_error(model, tested, prediction)
     14         Data, prepaired as tested labels
     15     """
---> 16     y_pred = model.predict(tested)
     17     err = roc_auc_score(prediction, y_pred)
     18     return err

~\Anaconda3\lib\site-packages\sklearn\ensemble\voting.py in predict(self, X)
    302                 lambda x: np.argmax(
    303                     np.bincount(x, weights=self._weights_not_none)),
--> 304                 axis=1, arr=predictions)
    305 
    306         maj = self.le_.inverse_transform(maj)

~\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
    378     except StopIteration:
    379         raise ValueError('Cannot apply_along_axis when any iteration dimensions are 0')
--> 380     res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
    381 
    382     # build a buffer for storing evaluations of func1d.

~\Anaconda3\lib\site-packages\sklearn\ensemble\voting.py in <lambda>(x)
    301             maj = np.apply_along_axis(
    302                 lambda x: np.argmax(
--> 303                     np.bincount(x, weights=self._weights_not_none)),
    304                 axis=1, arr=predictions)
    305 

TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'

scikit-learn 0.21.2 anaconda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions