2015-12-15 12 views
17

में सीबर्न tsplot फ़ंक्शन में मानक विचलन और त्रुटियों के सलाखों Seaborn इसकी त्रुटि बार की गणना कैसे करता है? उदाहरण:पाइथन

import numpy as np; np.random.seed(22) 
import seaborn as sns; sns.set(color_codes=True) 
x = np.linspace(0, 15, 31) 
data = np.sin(x) + np.random.rand(10, 31) + np.random.randn(10, 1) 
ax = sns.tsplot(data=data, err_style="ci_bars") 
plt.show() 

ci_bars (या ci_bands) की गणना कैसे की जाती है?

भी, यह ci_bars शैली जहां त्रुटि सलाखों या बैंड हर बार बिंदु पर मानक विचलन मूल्यों की के अनुरूप में tsplot साजिश बनाने के लिए संभव है? (और नहीं मतलब की मानक त्रुटि, या बूटस्ट्रैप)

+3

की संभावित डुप्लिकेट [ Seaborn tsplot में "त्रुटि बैंड" की गणना कैसे की जाती है?] (Http://stackoverflow.com/questions/29481134/how-are-the-error-bands-in-seaborn-tsplot-calculated) – mwaskom

+1

@mwaskom: my additio नल सवाल यह है कि '' tsplot'' में बार या बैंड को साजिश कैसे करें जो मानक विचलन को प्रतिबिंबित करता है, न कि मानक त्रुटि या बूटस्ट्रैप अनुमान। क्या यह संभव है? मेरे डेटा बूटस्ट्रैप्स के लिए बहुत संकीर्ण हैं और stdev बेहतर प्रतिनिधित्व – lgd

उत्तर

9

के बाद से tsplot समारोह त्रुटि बार मूल्यों सीधे सेट करने के लिए या उन्हें गणना करने के लिए इस्तेमाल किया पद्धति को बदलने के लिए एक रास्ता प्रदान नहीं करता है, एकमात्र समाधान मैंने पाया बंदर पैच करने के लिए था timeseries मॉड्यूल:

import seaborn.timeseries 

def _plot_std_bars(*args, central_data=None, ci=None, data=None, **kwargs): 
    std = data.std(axis=0) 
    ci = np.asarray((central_data - std, central_data + std)) 
    kwargs.update({"central_data": central_data, "ci": ci, "data": data}) 
    seaborn.timeseries._plot_ci_bars(*args, **kwargs) 

def _plot_std_band(*args, central_data=None, ci=None, data=None, **kwargs): 
    std = data.std(axis=0) 
    ci = np.asarray((central_data - std, central_data + std)) 
    kwargs.update({"central_data": central_data, "ci": ci, "data": data}) 
    seaborn.timeseries._plot_ci_band(*args, **kwargs) 

seaborn.timeseries._plot_std_bars = _plot_std_bars 
seaborn.timeseries._plot_std_band = _plot_std_band 

फिर, साथ मानक विचलन त्रुटि सलाखों

ax = sns.tsplot(data, err_style="std_bars", n_boot=0) 

या

का उपयोग साजिश

मानक विचलन बैंड के साथ साजिश करने के लिए।

संपादित करें:

import pandas as pd 
import seaborn as sns 

df = pd.DataFrame.from_dict({ 
    "mean": data.mean(axis=0), 
    "std": data.std(axis=0) 
}).reset_index() 

g = sns.FacetGrid(df, size=6) 
ax = g.map(plt.errorbar, "index", "mean", "std") 
ax.set(xlabel="", ylabel="") 

EDIT2:: this answer इतने पर से प्रेरित होकर, एक और (शायद और अधिक समझदार) दृष्टिकोण tsplot के बजाय निम्न का उपयोग किया जाएगा के बाद से आप कैसे tsplot अपने विश्वास के अंतराल की गणना करता है के बारे में पूछा: यह प्रत्येक समय बिंदु पर bootstrapping to estimate the distribution of the mean value का उपयोग करता है और फिर इन वितरणों से कम और उच्च प्रतिशत मूल्य (जो आत्मविश्वास अंतराल का उपयोग किया जाता है) पाता है। डिफ़ॉल्ट आत्मविश्वास अंतराल 68% है - सामान्य वितरण मानते हुए, माध्य के ± एक मानक विचलन के बराबर है। संबंधित निम्न और उच्च प्रतिशत 16% और 84% हैं। आप ci कीवर्ड तर्क के माध्यम से आत्मविश्वास अंतराल बदल सकते हैं।

+0

धन्यवाद मार्टिन, अच्छा कामकाज। दुर्भाग्यवश पाइथन दुभाषिया '* args' तर्क के ठीक बाद वाक्यविन्यास के बारे में शिकायत करता है – luca

14

Seaborn v0.8.0 (जुलाई 2017) में त्रुटि सलाखों उपयोग करने के लिए ci = "एसडी" रख कर मानक के बजाय सबसे सांख्यिकीय कार्यों में बूटस्ट्रैप विश्वास के अंतराल विचलन दिखाने की क्षमता जोड़ा गया है। तो यह अब पिछले Seaborn संस्करणों मानक विचलन की साजिश रचने के लिए एक समाधान के लिए

sns.tsplot(data=data, ci="sd") 

काम करता है का उपयोग करने के हो सकता है matplotlib errorbar Seaborn tsplot के शीर्ष पर:

import numpy as np; 
import seaborn as sns; 
import pandas as pd 
import matplotlib.pyplot as plt 

# create a group of time series 
num_samples = 90 
group_size = 10 
x = np.linspace(0, 10, num_samples) 
group = np.sin(x) + np.linspace(0, 2, num_samples) + np.random.rand(group_size, num_samples) + np.random.randn(group_size, 1) 
df = pd.DataFrame(group.T, index=range(0,num_samples)) 

# plot time series with seaborn 
ax = sns.tsplot(data=df.T.values) #, err_style="unit_traces") 

# Add std deviation bars to the previous plot 
mean = df.mean(axis=1) 
std = df.std(axis=1) 
ax.errorbar(df.index, mean, yerr=std, fmt='-o') #fmt=None to plot bars only 

plt.show() 

enter image description here