adriangb / scikeras

Scikit-Learn API wrapper for Keras.
https://www.adriangb.com/scikeras/
MIT License
239 stars 47 forks source link

TypeError: OneHotEncoder.__init__() got an unexpected keyword argument 'sparse' #316

Closed Fake45mar closed 5 months ago

Fake45mar commented 5 months ago

There is a problem in the file venv/lib/pythonX/site-packages/scikeras/utils/transformers.py

I use 'KerasClassifier' to be able to wrap my tensorflow model and put it into sklearn Voting Classifier.

The issue is in this segment:

if _is_categorical_crossentropy(self.loss):
            encoders["multiclass"] = make_pipeline(
                TargetReshaper(),
                OneHotEncoder(
                    **sparse**=False, dtype=keras_dtype, categories=self.categories
                ),
            )

I think it should be:

if _is_categorical_crossentropy(self.loss):
            encoders["multiclass"] = make_pipeline(
                TargetReshaper(),
                OneHotEncoder(
                    **sparse_output**=False, dtype=keras_dtype, categories=self.categories
                ),
            )

Please, take a look.

Thank you!

adriangb commented 5 months ago

What version of scikit-learn are you using?

Fake45mar commented 5 months ago

@adriangb , '1.4.1.post1'

jbeno commented 5 months ago

Same here. Using scikit-learn 1.4.1.post1 with scikeras 0.12.0.

TypeError: OneHotEncoder.__init__() got an unexpected keyword argument 'sparse'

From Scikit page: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

"sparse_outputbool, default=True When True, it returns a scipy.sparse.csr_matrix, i.e. a sparse matrix in “Compressed Sparse Row” (CSR) format.

New in version 1.2: sparse was renamed to sparse_output"

Screen Shot 2024-04-03 at 6 15 30 PM
adriangb commented 5 months ago

Could you please try #317 (pip install -U 'git+https://github.com/adriangb/scikeras.git@keras-3') and see if it fixes your problem? Thanks!

jbeno commented 5 months ago

Thanks for the quick response! Just tested and did not see that error with this combination:

Previously, I was seeing the error with this combination:

This seemed to require an update to keras 3.2.0, and then I had to update tensorflow for compatibility. Not a bad thing, been wanting to upgrade, but did have to update some of my tf.keras references after this to be just keras.

adriangb commented 5 months ago

Thank you for trying and confirming compatibility. Indeed I plan on releasing SciKeras 0.13.0 only with compatibility with Keras 3. As an unpaid maintainer I don’t have the resources to support multiple major versions of dependencies unfortunately.

adriangb commented 5 months ago

This should be fixed now. Please let me know if this is still not fixed with SciKeras 0.13.0. Thanks!

ellysabbath commented 2 weeks ago

def clean_text(df1,column,max_len,vocab_size): stemmer=PorterStemmer()

corpus=[]
for text in df1[column]:
    text=re.sub("[^a-zA-Z]",' ',text)
    text=text.lower()
    text=text.split()
    text=[stemmer.stem(w) for w in text if w not in stp]
    text=" ".join(text)
    corpus.append(text)

    OneHotEncoder_word=[ OneHotEncoder(input_text=word,n=vocab_size) for word in corpus]
    pad=pad_sequences(sequences= OneHotEncoder_word,max_len=max_len,padding='pre')
    return pad

x_train=clean_text(df1,'Comment',vocab_size=10000,max_len=298)

TypeError Traceback (most recent call last) Cell In[100], line 1 ----> 1 clean_text(df1, 'Comment', vocab_size=10000, max_len=298)

Cell In[99], line 13, in clean_text(df1, column, max_len, vocab_size) 10 text=" ".join(text) 11 corpus.append(text) ---> 13 OneHotEncoder_word=[ OneHotEncoder(input_text=word,n=vocab_size) for word in corpus] 14 pad=pad_sequences(sequences= OneHotEncoder_word,max_len=max_len,padding='pre') 15 return pad

Cell In[99], line 13, in (.0) 10 text=" ".join(text) 11 corpus.append(text) ---> 13 OneHotEncoder_word=[ OneHotEncoder(input_text=word,n=vocab_size) for word in corpus] 14 pad=pad_sequences(sequences= OneHotEncoder_word,max_len=max_len,padding='pre') 15 return pad

TypeError: OneHotEncoder.init() got an unexpected keyword argument 'input_text'

adriangb commented 2 weeks ago

@ellysabbath please open a new issue and provide a self contained reproducible example to report a bug. Thanks!