Skip to content

Had an error while testing MLPClassifier #1

Description

@jspeed-meyers

Great package! Thank you for creating this. I was testing out the MLPClassifier() model with this package--see the complete code below--and was getting an AttributeError: 'list' object has no attribute 'ndim' when I used the package as is. I added some code below to convert the model coefficient elements into numpy arrays and it seemed to work. Am I missing something? Was this an error with the package or was it an error in how I was using the package? Either way, thank you again. Note: I was using Python 3.7.4.

import numpy as np
import sklearn_json as skljson
from sklearn.neural_network import MLPClassifier
from sklearn import datasets

# Load data
df = datasets.load_iris()
X = df.data 
y = df.target 

# Instantiate and fit model
model_fit = MLPClassifier(solver='lbfgs', alpha=1e-5,
	hidden_layer_sizes=(15,), random_state=1).fit(X, y)
file_name = "model.json"

# Convert model to json
skljson.to_json(model_fit, file_name)

# Convert json back to model
deserialized_model = skljson.from_json(file_name)

## THE MISSING PIECE: THE LISTS INSIDE COEFS_ NEED
## TO BE CONVERTED TO ARRAYS
for i, x in enumerate(deserialized_model.coefs_):
	deserialized_model.coefs_[i] = np.array(x)

# This prediction should run if the code really works
deserialized_model.predict(X)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions