2014-05-21 18 views
5

Tôi muốn khám phá trực quan mối quan hệ giữa hai biến. Các hình thức chức năng của mối quan hệ không hiển thị trong biểu đồ phân tán dày đặc như thế này:Cách trực quan hóa mối quan hệ phi tuyến trong một ô phân tán

scatter plot

Làm thế nào tôi có thể thêm một lowess mịn để biểu đồ phân tán bằng Python?

Hoặc bạn có bất kỳ đề xuất nào khác để khám phá trực quan các mối quan hệ phi tuyến tính không?

Tôi đã thử các sau đây nhưng nó đã không làm việc đúng cách (vẽ trên một ví dụ từ 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

Các lowess mượt mà (đường màu đỏ) là lạ.

EDIT:

Ma trận sau cũng bao gồm smoothers lowess (lấy từ this question trên CV): enter image description here

Không ai có mã cho một đồ thị như vậy?

+0

Bạn dường như đã chỉnh sửa câu hỏi này để bao gồm một câu hỏi mới. Thay vào đó, hãy đặt một câu hỏi riêng để mọi người có thể tìm thấy câu hỏi đó. – DSM

+0

Có, xin lỗi, câu hỏi mới là [ở đây] (http://stackoverflow.com/questions/23800130/scatter-plot-matrix-with-lowess-smoother). – tobip

Trả lời

9

Từ các tài liệu 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 

Nó chấp nhận lập luận theo thứ tự khác. Nó cũng không chỉ trả lại 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]]) 

Nhưng nếu bạn gọi

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

bạn sẽ thấy một cái gì đó giống như

example lowess output

13

Bạn cũng có thể sử dụng 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

Các vấn đề liên quan