2014-05-21 6 views
5

Desidero esplorare visivamente la relazione tra due variabili. La forma funzionale del rapporto non è visibile in grafici a dispersione densi come questo:Come visualizzare una relazione non lineare in un grafico a dispersione

scatter plot

Come posso aggiungere una lowess liscia per la rappresentazione della dispersione in Python?

Oppure avete altri suggerimenti per esplorare visivamente le relazioni non lineari?

Ho provato quanto segue ma non funzionava correttamente (attingendo a un esempio da Michiel de Hoon):

import numpy as np 
from statsmodels.nonparametric.smoothers_lowess import lowess 
x = np.arange(0,10,0.01) 
ytrue = np.exp(-x/5.0) + 2*np.sin(x/3.0) 

# add random errors with a normal distribution      
y = ytrue + np.random.normal(size=len(x)) 
plt.scatter(x,y,color='cyan') 

# calculate a smooth curve through the scatter plot 
ys = lowess(x, y) 
_ = plt.plot(x,ys,'red',linewidth=1) 

# draw the true values for comparison 
plt.plot(x,ytrue,'green',linewidth=3) 

lowess

Il lowess più uniformi (linee rosse) è strano.

EDIT:

La seguente matrice comprende anche smoothers Lowess (presi da this question sulla CV): enter image description here

Qualcuno ha il codice per tale grafico?

+0

Sembra che tu abbia modificato questa domanda per includere una nuova domanda. Per favore, poni una domanda separata, in modo che le persone possano trovarla. – DSM

+0

Sì, mi dispiace, la nuova domanda è [qui] (http://stackoverflow.com/questions/23800130/scatter-plot-matrix-with-lowess-smoother). – tobip

risposta

9

Dalla documentazione lowess:

Definition: lowess(endog, exog, frac=0.6666666666666666, it=3, delta=0.0, is_sorted=False, missing='drop', return_sorted=True) 

[...] 

Parameters 
---------- 
endog: 1-D numpy array 
    The y-values of the observed points 
exog: 1-D numpy array 
    The x-values of the observed points 

accetta argomenti nella altro ordine. Essa, inoltre, non si limita a restituire y:

>>> lowess(y, x) 
array([[ 0.00000000e+00, 1.13752478e+00], 
     [ 1.00000000e-02, 1.14087128e+00], 
     [ 2.00000000e-02, 1.14421582e+00], 
     ..., 
     [ 9.97000000e+00, -5.17702654e-04], 
     [ 9.98000000e+00, -5.94304755e-03], 
     [ 9.99000000e+00, -1.13692896e-02]]) 

Ma se si chiama

ys = lowess(y, x)[:,1] 

si dovrebbe vedere qualcosa come

example lowess output

13

Si potrebbe anche usare seaborn:

import numpy as np 
import seaborn as sns 

x = np.arange(0, 10, 0.01) 
ytrue = np.exp(-x/5) + 2 * np.sin(x/3) 
y = ytrue + np.random.normal(size=len(x)) 

sns.regplot(x, y, lowess=True) 

enter image description here