Skip to content
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

Add minimize #211

Merged
merged 5 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1717,13 +1717,16 @@ def scatter(self, column_for_x, select=None, overlay=True, fit_line=False, **var
y_labels = self._as_labels(select)

def draw(axis, label, color):
if 'color' in options:
color = options.pop('color')
axis.scatter(xdata, self[label], color=color, **options)
if fit_line:
m,b = np.polyfit(xdata, self[label], 1)
minx, maxx = np.min(xdata),np.max(xdata)
axis.plot([minx,maxx],[m*minx+b,m*maxx+b], color=color)

self._visualize(column_for_x, y_labels, None, overlay, draw, _vertical_x)
x_label = self._as_label(column_for_x)
self._visualize(x_label, y_labels, None, overlay, draw, _vertical_x)

def _visualize(self, x_label, y_labels, ticks, overlay, draw, annotate, width=6, height=4):
"""Generic visualization that overlays or separates the draw function.
Expand Down
18 changes: 16 additions & 2 deletions datascience/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utility functions"""

__all__ = ['percentile', 'plot_cdf_area', 'plot_normal_cdf', 'table_apply']
__all__ = ['percentile', 'plot_cdf_area', 'plot_normal_cdf', 'table_apply',
'minimize']


import numpy as np
Expand All @@ -9,7 +10,8 @@
matplotlib.use('agg', warn=False)
import matplotlib.pyplot as plt
from scipy import stats

from scipy import optimize
import functools

def percentile(p, arr=None):
"""Returns the pth percentile of the input array (the value that is at
Expand Down Expand Up @@ -116,3 +118,15 @@ def table_apply(table, func, subset=None):
df = pd.DataFrame(df).T
tab = Table.from_df(df)
return tab

def minimize(f, start=None, **vargs):
if start is None:
start = [0] * f.__code__.co_argcount

@functools.wraps(f)
def wrapper(args):
return f(*args)

result = optimize.minimize(wrapper, start, **vargs)
return np.round(result.x, 7)

2 changes: 1 addition & 1 deletion datascience/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.5.9'
__version__ = '0.5.10'
3 changes: 3 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ def test_table_apply():
newtab = util.table_apply(tab, lambda a: a+1, subset=['b', 'c'])
assert all(newtab['a'] == tab['a'])
assert all(newtab['b'] == tab['b'] + 1)

def test_minimize():
assert [2, 1] == list(ds.minimize(lambda x, y: (x-2)**2 + (y-1)**2))