question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jsonpickle doesn't work with sgdclassifier

See original GitHub issue

Description

Error given

Steps/Code to Reproduce

import numpy as np
import jsonpickle
from sklearn.linear_model import SGDClassifier
clf = SGDClassifier(loss='log', penalty='l2', alpha=1e-4, n_iter=100, random_state=42, n_jobs=-1)
np.random.seed(13)
x = np.random.rand(10,5)
y = np.random.randint(low=0, high=2, size=10)
clf.fit(x, y)
clf2 = jsonpickle.decode(jsonpickle.encode(clf))

Expected Results

get the same clf2 as clf

Actual Results

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python2.7/site-packages/jsonpickle/__init__.py", line 149, in decode
    return unpickler.decode(string, backend=backend, keys=keys)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 26, in decode
    return context.restore(backend.decode(string), reset=reset)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 127, in restore
    value = self._restore(obj)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 169, in _restore
    return restore(obj)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 283, in _restore_object
    return self._restore_object_instance(obj, cls)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 348, in _restore_object_instance
    return self._restore_object_instance_variables(obj, instance)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 393, in _restore_object_instance_variables
    instance = self._restore_state(obj, instance)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 398, in _restore_state
    state = self._restore(obj[tags.STATE])
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 169, in _restore
    return restore(obj)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 451, in _restore_dict
    data[k] = self._restore(v)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 169, in _restore
    return restore(obj)
  File "python2.7/site-packages/jsonpickle/unpickler.py", line 192, in _restore_reduce
    if f == tags.NEWOBJ or f.__name__ == '__newobj__':
AttributeError: 'dict' object has no attribute '__name__'

Versions

>>> import sys; print("Python", sys.version)
('Python', '2.7.8 (default, Aug  3 2017, 16:13:26) \n[GCC 5.4.0 20160609]')
>>> import numpy; print("NumPy", numpy.__version__)
('NumPy', '1.13.1')
>>> import scipy; print("SciPy", scipy.__version__)
('SciPy', '0.19.1')
>>> import sklearn; print("Scikit-Learn", sklearn.__version__)
('Scikit-Learn', '0.19.0')

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fedorzhcommented, Oct 11, 2017

@jnothman I actually filed the issue to both as I wasn’t sure on the source of the problem: after debugging it seemed the serialization only doesn’t work with scikit, but works with numpy/scipy. @davvid from jsonpickle team fixed the issue: https://github.com/jsonpickle/jsonpickle/issues/184 . I suspect scikit is doing something which converts a one-dimensional numpy array into a float.

@TomDLT, @lesteve We have the scikit object as a member in a hierarchy of classes, and would prefer to have a readable serialized format for those classes (json). It doesn’t matter as much for the classifier itself.

0reactions
lestevecommented, Oct 11, 2017

Recommendation for next time: please say you have opened an issue in the other project tracker and add a link.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jsonpickle in subclass changes data type - Stack Overflow
Problem "disapears" when in gui.py I change import data to from data import * (with other code refactoring) but I'm not sure why......
Read more >
jsonpickle Documentation — jsonpickle 1.4.3.dev0+g5b8d3ea ...
Python library for serializing any arbitrary object graph into JSON. Warning. jsonpickle can execute arbitrary Python code. Do not load jsonpickles from ...
Read more >
SGD Classification Example with SGDClassifier in Python
Scikit-learn API provides the SGDClassifier class to implement SGD method for classification problems. The SGDClassifier applies regularized ...
Read more >
Custom Resources | Modela Documentation - Modela.ai
Documentation for all custom resource definitions.
Read more >
O2O优惠卷Thu Aug 16 11:09:25 CST 2018-天池实验室-实时在线的 ...
close the figure at the end, so we don't get a duplicate. 19. # of the last plot ... from sklearn.linear_model import SGDClassifier,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found