import numpy as np
import pandas as pd


def get_grid_points(x, num_grid_points):
    if num_grid_points is None:
        return x.unique()
    else:
        # unique is necessary, because if num_grid_points is too much larger
        # than x.shape[0], there will be duplicate quantiles (even with
        # interpolation)
        return x.quantile(np.linspace(0, 1, num_grid_points)).unique()


def ice(data, column, predict, num_grid_points=None):
    """
    Generate ICE curves for given column and model.

    predict is a function generating the model predictions that accepts a DataFrame with the same number of columns as
    data.

    If num_grid_points is None, column varies over its unique values in data.
    If num_grid_points is not None, column varies over num_grid_pts values, evenly spaced at its quantiles in data.
    """
    x_s = get_grid_points(data[column], num_grid_points)
    ice_data = to_ice_data(data, column, x_s)
    ice_data['ice_y'] = predict(ice_data)

    other_columns = list(data.columns)
    other_columns.remove(column)
    ice_df = ice_data.pivot_table(values='ice_y', index=other_columns, columns=column).T

    return ice_df


def to_ice_data(data, column, x_s):
    """
    Create the DataFrame necessary for ICE calculations
    """
    ice_data = pd.DataFrame(np.repeat(data.values, x_s.size, axis=0), columns=data.columns)
    ice_data[column] = np.tile(x_s, data.shape[0])

    return ice_data

@@ -1,12 +1,82 @@
+from __future__ import division%0A%0Afrom matplotlib import pyplot as plt%0A
 import numpy
@@ -460,16 +460,95 @@
 que()%0A%0A%0A
+def get_quantiles(x):%0A    return np.greater.outer(x, x).sum(axis=1) / x.size%0A%0A%0A
 def ice(
@@ -1129,16 +1129,23 @@
 ice_data
+.values
 )%0A%0A    o
@@ -1301,16 +1301,16 @@
 umn).T%0A%0A
-
     retu
@@ -1321,16 +1321,1262 @@
 ce_df%0A%0A%0A
+def ice_plot(ice_data, frac_to_plot=1., x_quantile=False,%0A             centered=False, centered_quantile=0.,%0A             ax=None, **kwargs):%0A    %22%22%22%0A    Plot the given ICE data%0A%0A    If frace_to_plot is less than one, randomly samples that fraction of ICE%0A    curves to plot%0A%0A    If %60x_quantile%60 is %60True%60, the plotted x-coordinates are quantiles of%0A    %60ice_data.index%60.%0A%0A    If %60centered%60 is true, each ICE curve is is centered to zero at the%0A    percentile (closest to) %60centered_quantile%60.%0A%0A    Keyword arguments are passed to plot(...)%0A    %22%22%22%0A    if not ice_data.index.is_monotonic_increasing:%0A        ice_data = ice_data.sort_index()%0A%0A    if x_quantile:%0A        x = get_quantiles(ice_data.index)%0A    else:%0A        x = ice_data.index%0A%0A    if centered:%0A        quantiles = get_quantiles(ice_data.index)%0A        centered_quantile_iloc = np.abs(quantiles - centered_quantile).argmin()%0A        ice_data = ice_data - ice_data.iloc%5Bcentered_quantile_iloc%5D%0A%0A    if frac_to_plot %3C 1.:%0A        n_cols = ice_data.shape%5B1%5D%0A        icols = np.random.choice(n_cols, size=frac_to_plot * n_cols, replace=False)%0A        ice_data = ice_data.icol(icols)%0A%0A    if ax is None:%0A        _, ax = plt.subplots()%0A%0A    ax.plot(x, ice_data, **kwargs)%0A%0A    return ax%0A%0A%0A
 def to_i

Add basic ICE plot