Skip to content

Commit 6361d73

Browse files
authored
Fix SKLearnClassifier and SKLearnRegressor examples (#21385)
* fix imports and make_classification call * Update examples * Fix transformer example * Remove unnecessary lists and add a clarifying comment * Revert transformer changes * use linear activation for regression
1 parent 764ed95 commit 6361d73

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

keras/src/wrappers/sklearn_wrapper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ class SKLearnClassifier(ClassifierMixin, SKLBase):
235235
scikit-learn model.
236236
237237
``` python
238-
from keras.src.layers import Dense, Input, Model
238+
from keras.layers import Dense, Input
239+
from keras.models import Model
239240
240241
def dynamic_model(X, y, loss, layers=[10]):
241242
# Creates a basic MLP model dynamically choosing the input and
@@ -248,7 +249,7 @@ def dynamic_model(X, y, loss, layers=[10]):
248249
hidden = Dense(layer_size, activation="relu")(hidden)
249250
250251
n_outputs = y.shape[1] if len(y.shape) > 1 else 1
251-
out = [Dense(n_outputs, activation="softmax")(hidden)]
252+
out = Dense(n_outputs, activation="softmax")(hidden)
252253
model = Model(inp, out)
253254
model.compile(loss=loss, optimizer="rmsprop")
254255
@@ -262,7 +263,7 @@ def dynamic_model(X, y, loss, layers=[10]):
262263
from sklearn.datasets import make_classification
263264
from keras.wrappers import SKLearnClassifier
264265
265-
X, y = make_classification(n_samples=1000, n_features=10, n_classes=3)
266+
X, y = make_classification(n_samples=1000, n_features=10)
266267
est = SKLearnClassifier(
267268
model=dynamic_model,
268269
model_kwargs={
@@ -346,7 +347,8 @@ class SKLearnRegressor(RegressorMixin, SKLBase):
346347
scikit-learn model.
347348
348349
``` python
349-
from keras.src.layers import Dense, Input, Model
350+
from keras.layers import Dense, Input
351+
from keras.models import Model
350352
351353
def dynamic_model(X, y, loss, layers=[10]):
352354
# Creates a basic MLP model dynamically choosing the input and
@@ -359,7 +361,7 @@ def dynamic_model(X, y, loss, layers=[10]):
359361
hidden = Dense(layer_size, activation="relu")(hidden)
360362
361363
n_outputs = y.shape[1] if len(y.shape) > 1 else 1
362-
out = [Dense(n_outputs, activation="softmax")(hidden)]
364+
out = Dense(n_outputs)(hidden)
363365
model = Model(inp, out)
364366
model.compile(loss=loss, optimizer="rmsprop")
365367

0 commit comments

Comments
 (0)