使用Seaborn和Statsmodels在一个图中显示数据和模型预测

2024-04-29 15:16:40 发布

您现在位置:Python中文网/ 问答频道 /正文

Seaborn是一个很好的软件包,可以用漂亮的输出进行高级绘图。然而,我正在努力使用Seaborn覆盖来自外部拟合模型的数据和模型预测。在这个例子中,我在Statsmodels中拟合模型,这些模型对于Seaborn来说太复杂了,无法开箱即用,但我认为问题更为普遍(即,如果我有模型预测,并且希望使用Seaborn可视化它们和数据)。

让我们从导入和数据集开始:

import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.formula.api as smf
import patsy
import itertools
import matplotlib.pyplot as plt

np.random.seed(12345)

# make a data frame with one continuous and two categorical variables:
df = pd.DataFrame({'x1': np.random.normal(size=100),
                     'x2': np.tile(np.array(['a', 'b']), 50),
                     'x3': np.repeat(np.array(['c', 'd']), 50)})

# create a design matrix using patsy:
X = patsy.dmatrix('x1 * x2 * x3', df)

# some random beta weights:
betas = np.random.normal(size=X.shape[1])

# create the response variable as the noisy linear combination of predictors:
df['y'] = np.inner(X, betas) + np.random.normal(size=100)

我们在包含所有预测变量及其相互作用的statsmodels中拟合一个模型:

# fit a model with all interactions
fit = smf.ols('y ~ x1 * x2 * x3', df).fit()
print(fit.summary())

由于在这种情况下,我们指定了所有变量的组合,并且我们的模型预测是线性的,所以只要在包含模型预测的数据框中添加一个新的“预测”列就足够了。但是,这不是很一般(假设我们的模型是非线性的,所以我们希望我们的曲线图显示平滑曲线),所以我用所有预测值的组合创建了一个新的数据框,然后生成预测:

# create a new dataframe of predictions, using pandas' expand grid:
def expand_grid(data_dict):
    """ A port of R's expand.grid function for use with Pandas dataframes.

    from http://pandas.pydata.org/pandas-docs/stable/cookbook.html?highlight=expand%20grid

    """
    rows = itertools.product(*data_dict.values())
    return pd.DataFrame.from_records(rows, columns=data_dict.keys())


# build a new matrix with expand grid:

preds = expand_grid(
                {'x1': np.linspace(df['x1'].min(), df['x1'].max(), 2),
                 'x2': ['a', 'b'],
                 'x3': ['c', 'd']})
preds['yhat'] = fit.predict(preds)

preds数据帧如下所示:

  x3        x1 x2      yhat
0  c -2.370232  a -1.555902
1  c -2.370232  b -2.307295
2  c  3.248944  a -1.555902
3  c  3.248944  b -2.307295
4  d -2.370232  a -1.609652
5  d -2.370232  b -2.837075
6  d  3.248944  a -1.609652
7  d  3.248944  b -2.837075

由于Seaborn plot命令(与R中的ggplot2命令不同)似乎只接受一个和一个数据帧,因此我们需要将我们的预测合并到原始数据中:

# append to df:
merged = df.append(preds)

现在我们可以将模型预测与数据一起绘制出来,将连续变量x1作为x轴:

# plot using seaborn:
sns.set_style('white')
sns.set_context('talk')
g = sns.FacetGrid(merged, hue='x2', col='x3', size=5)
# use the `map` method to add stuff to the facetgrid axes:
g.map(plt.plot, "x1", "yhat")
g.map(plt.scatter, "x1", "y")
g.add_legend()
g.fig.subplots_adjust(wspace=0.3)
sns.despine(offset=10);

enter image description here

到目前为止还不错。现在假设我们没有测量连续变量x1,我们只知道另外两个(分类)变量(即,我们有一个2x2阶乘设计)。在这种情况下,我们如何根据数据绘制模型预测?

fit = smf.ols('y ~ x2 * x3', df).fit()
print(fit.summary())

preds = expand_grid(
                {'x2': ['a', 'b'],
                 'x3': ['c', 'd']})
preds['yhat'] = fit.predict(preds)
print(preds)

# append to df:
merged = df.append(preds)

我们可以用sns.pointplot或类似的方法绘制模型预测,如下所示:

# plot using seaborn:
g = sns.FacetGrid(merged, hue='x3', size=4)
g.map(sns.pointplot, 'x2', 'yhat')
g.add_legend();
sns.despine(offset=10);

enter image description here

或者像这样使用sns.factorplot的数据:

g = sns.factorplot('x2', 'y', hue='x3', kind='point', data=merged)
sns.despine(offset=10);
g.savefig('tmp3.png')

enter image description here

但我不知道如何生成类似于第一个图的图(即使用plt.plot的模型预测线,使用plt.scatter的数据点的分散)。原因是我试图用作x轴的x2变量是一个字符串/对象,所以pyplot命令不知道如何处理它们。


Tags: 数据模型importdfasnpseaborngrid
1条回答
网友
1楼 · 发布于 2024-04-29 15:16:40

正如我在评论中提到的,我有两种方式来考虑这样做。

首先是定义一个函数来进行匹配,然后绘制并传递给FacetGrid.map

import pandas as pd
import seaborn as sns
tips = sns.load_dataset("tips")

def plot_good_tip(day, total_bill, **kws):

    expected_tip = (total_bill.groupby(day)
                              .mean()
                              .apply(lambda x: x * .2)
                              .reset_index(name="tip"))
    sns.pointplot(expected_tip.day, expected_tip.tip,
                  linestyles=["--"], markers=["D"])

g = sns.FacetGrid(tips, col="sex", size=5)
g.map(sns.pointplot, "day", "tip")
g.map(plot_good_tip, "day", "total_bill")
g.set_axis_labels("day", "tip")

enter image description here

第二个是计算预测值,然后将它们与一个附加变量合并到数据框中,该变量标识什么是数据和什么是模型:

tip_predict = (tips.groupby(["day", "sex"])
                   .total_bill
                   .mean()
                   .apply(lambda x: x * .2)
                   .reset_index(name="tip"))
tip_all = pd.concat(dict(data=tips[["day", "sex", "tip"]], model=tip_predict),
                    names=["kind"]).reset_index()

sns.factorplot("day", "tip", "kind", data=tip_all, col="sex",
               kind="point", linestyles=["-", "--"], markers=["o", "D"])

enter image description here

相关问题 更多 >