Get data points from Seaborn distplot

前端 未结 2 1939
渐次进展
渐次进展 2020-12-05 03:21

I use

sns.distplot 

to plot a univariate distribution of observations. Still, I need not only the chart, but also the data points. How do

2条回答
  •  萌比男神i
    2020-12-05 04:04

    If you want to obtain the kde values of an histogram you can use scikit-learn KernelDensity function instead:

    import numpy as np
    import pandas as pd
    from sklearn.neighbors import KernelDensity
    
    ds=pd.read_csv('data-to-plot.csv')
    X=ds.loc[:,'Money-Spent'].values[:, np.newaxis]
    
    
    kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(X) #you can supply a bandwidth
                                                                  #parameter. 
    
    x=np.linspace(0,5,100)[:, np.newaxis]
    
    log_density_values=kde.score_samples(x)
    density=np.exp(log_density)
    
    array([1.88878660e-05, 2.04872903e-05, 2.21864649e-05, 2.39885206e-05,
           2.58965064e-05, 2.79134003e-05, 3.00421245e-05, 3.22855645e-05,
           3.46465903e-05, 3.71280791e-05, 3.97329392e-05, 4.24641320e-05,
           4.53246933e-05, 4.83177514e-05, 5.14465430e-05, 5.47144252e-05,
           5.81248850e-05, 6.16815472e-05, 6.53881807e-05, 6.92487062e-05,
           7.32672057e-05, 7.74479375e-05, 8.17953578e-05, 8.63141507e-05,
           ..........................
           ..........................
           3.93779919e-03, 4.15788216e-03, 4.38513011e-03, 4.61925890e-03,
           4.85992626e-03, 5.10672757e-03, 5.35919187e-03, 5.61677855e-03])
    

提交回复
热议问题