Skip to content

pandas.plotting.parallel_coordinates resets random seed! #17525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jasonzutty opened this issue Sep 13, 2017 · 5 comments · Fixed by #17730
Closed

pandas.plotting.parallel_coordinates resets random seed! #17525

jasonzutty opened this issue Sep 13, 2017 · 5 comments · Fixed by #17730
Labels
Milestone

Comments

@jasonzutty
Copy link

jasonzutty commented Sep 13, 2017

Code Sample, a copy-pastable example if possible

>>> x = pd.DataFrame(np.zeros((10,10)))
>>> x
     0    1    2    3    4    5    6    7    8    9
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
6  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
7  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
8  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
9  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
>>> pandas.plotting.parallel_coordinates(x,0)
<matplotlib.axes._subplots.AxesSubplot object at 0x10b6b11d0>
>>> random.random()
0.25891675029296335
>>> pandas.plotting.parallel_coordinates(x,0)
<matplotlib.axes._subplots.AxesSubplot object at 0x10b6b11d0>
>>> random.random()
0.25891675029296335


Problem description

A call to the pandas.plotting.parallel_coordinates method resets the random seed

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.2.7
Cython: 0.26
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 5.3.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jasonzutty
Copy link
Author

Checked it on another computer and it resets to the same random seed, yielding the same output from random.random()

Here is the version info for that computer:
INSTALLED VERSIONS

commit: None
python: 2.7.7.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-431.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 28.8.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
statsmodels: None
xarray: None
IPython: 5.3.0
sphinx: 1.5.3
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.2
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.5
boto: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 13, 2017

Thanks for the bug report. Looks like the issue is at

elif color_type == 'random':
import random
def random_color(column):
random.seed(column)
return [random.random() for _ in range(3)]
colors = lmap(random_color, lrange(num_colors))

Could you submit a fix @jasonzutty?

@jreback jreback added this to the Next Major Release milestone Sep 14, 2017
@jasonzutty
Copy link
Author

I'll take a crack at it, just remove the seed?

@TomAugspurger
Copy link
Contributor

Maybe a numpy RandomContext, to get reproducible colors? I haven't looked closely.

@cmazzullo
Copy link
Contributor

Just submitted a pull request with a fix using random.getstate and random.setstate. Here's some output showing that the behavior looks right:

>>> # The random seed isn't reset by the function now:
>>> x = pandas.DataFrame(numpy.zeros((10,10)))
>>> pandas.plotting.parallel_coordinates(x,0)
<matplotlib.axes._subplots.AxesSubplot object at 0x7fc1408b7d68>
>>> random.random()
0.5274572977380592
>>> pandas.plotting.parallel_coordinates(x,0)
<matplotlib.axes._subplots.AxesSubplot object at 0x7fc1408b7d68>
>>> random.random()
0.23087467871474676
>>> # And the function still produces consistent colors:
>>> pandas.plotting._style._get_standard_colors(1, color_type='random')
[[0.8444218515250481, 0.7579544029403025, 0.420571580830845]]
>>> pandas.plotting._style._get_standard_colors(1, color_type='random')
[[0.8444218515250481, 0.7579544029403025, 0.420571580830845]]

@jreback jreback modified the milestones: Next Major Release, 0.21.0 Oct 1, 2017
@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, 0.21.0 Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants