2017-08-01 10 views
2

this package tutorial命令に従った関数でデータをプロットしようとしています。データをプロットできません

これはプロットコードです:

def plot_frequency_recency_matrix(model, 
            T=1, 
            max_frequency=None, 
            max_recency=None, 
            title=None, 
            xlabel="Customer's Historical Frequency", 
            ylabel="Customer's Recency", 
            **kwargs): 
    """ 
    Plot recency frequecy matrix as heatmap. 
    Plot a figure of expected transactions in T next units of time by a customer's frequency and recency. 
    Parameters 
    ---------- 
    model: lifetimes model 
     A fitted lifetimes model. 
    T: fload, optional 
     Next units of time to make predictions for 
    max_frequency: int, optional 
     The maximum frequency to plot. Default is max observed frequency. 
    max_recency: int, optional 
     The maximum recency to plot. This also determines the age of the customer. 
     Default to max observed age. 
    title: str, optional 
     Figure title 
    xlabel: str, optional 
     Figure xlabel 
    ylabel: str, optional 
     Figure ylabel 
    kwargs 
     Passed into the matplotlib.imshow command. 
    Returns 
    ------- 
    axes: matplotlib.AxesSubplot 
    """ 
    from matplotlib import pyplot as plt 
    import numpy as np 
    if max_frequency is None: 
     max_frequency = int(model.data['frequency'].max()) 
    if max_recency is None: 
     max_recency = int(model.data['T'].max()) 
    Z = np.zeros((max_recency + 1, max_frequency + 1)) 
    for i, recency in enumerate(np.arange(max_recency + 1)): 
     for j, frequency in enumerate(np.arange(max_frequency + 1)): 
      Z[i, j] = model.conditional_expected_number_of_purchases_up_to_time(T, frequency, recency, max_recency) 
    interpolation = kwargs.pop('interpolation', 'none') 
    ax = plt.subplot(111) 
    PCM = ax.imshow(Z, interpolation=interpolation, **kwargs) 
    plt.xlabel(xlabel) 
    plt.ylabel(ylabel) 
    if title is None: 
     title = 'Expected Number of Future Purchases for {} Unit{} of Time,'. \ 
      format(T, "s"[T == 1:]) + '\nby Frequency and Recency of a Customer' 
    plt.title(title) 
    # turn matrix into square 
    forceAspect(ax) 
    # plot colorbar beside matrix 
    plt.colorbar(PCM, ax=ax) 
    return ax 


def forceAspect(ax, aspect=1): 
    im = ax.get_images() 
    extent = im[0].get_extent() 
    ax.set_aspect(abs((extent[1] - extent[0])/(extent[3] - extent[2]))/aspect) 

しかし、私は実行します。

from lifetimes.plotting import plot_frequency_recency_matrix 

plot_frequency_recency_matrix(bgf) 

私はプロットにしようとしているサンプルデータ:

表示することができますどのように
frequency recency  T 
ID 
1   2 30.43 38.86 
2   1  1.71 38.86 
3   0  0.00 38.86 
4   0  0.00 38.86 
5   0  0.00 38.86 

プロット? ありがとう!

+2

あなたがプログラムを実行するとどうなりますか?エラーがありますか? –

答えて

2

あなたのプロットを表示するためにplt.show()を呼び出す必要があります:

from matplotlib import pyplot as plt 

plot_frequency_recency_matrix(bgf) 
plt.show() 
関連する問題