绘图:如何使用绘图绘制回归线?

我有一个数据帧df,其列为pm1和pm25。我想显示这两个信号之间的相关性的图表(带有Plotly)。到目前为止,我已经设法显示了散点图,但是我没有设法画出信号之间的相关性的拟合线。到目前为止,我已经尝试过:

denominator=df.pm1**2-df.pm1.mean()*df.pm1.sum()
print('denominator',denominator)
m=(df.pm1.dot(df.pm25)-df.pm25.mean()*df.pm1.sum())/denominator
b=(df.pm25.mean()*df.pm1.dot(df.pm1)-df.pm1.mean()*df.pm1.dot(df.pm25))/denominator
y_pred=m*df.pm1+b


lineOfBestFit = go.Scattergl(
    x=df.pm1,y=y_pred,name='Line of best fit',line=dict(
        color='red',)
)

data = [dataPoints,lineOfBestFit]
figure = go.Figure(data=data)

figure.show()

情节:

绘图:如何使用绘图绘制回归线?

如何正确绘制lineOfBestFit?

tian9085 回答:绘图:如何使用绘图绘制回归线?

对于回归分析,我喜欢使用statsmodels.apisklearn.linear_model。我还喜欢在熊猫数据框中组织数据和回归结果。这是一种以一种干净有序的方式来做您想要的事情的方法:

使用sklearn或statsmdodels进行绘图:

enter image description here

使用sklearn的代码:

from sklearn.linear_model import LinearRegression
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import datetime

# data
np.random.seed(123)
numdays=20

X = (np.random.randint(low=-20,high=20,size=numdays).cumsum()+100).tolist()
Y = (np.random.randint(low=-20,size=numdays).cumsum()+100).tolist()
df = pd.DataFrame({'X': X,'Y':Y})

# regression
reg = LinearRegression().fit(np.vstack(df['X']),Y)
df['bestfit'] = reg.predict(np.vstack(df['X']))

# plotly figure setup
fig=go.Figure()
fig.add_trace(go.Scatter(name='X vs Y',x=df['X'],y=df['Y'].values,mode='markers'))
fig.add_trace(go.Scatter(name='line of best fit',x=X,y=df['bestfit'],mode='lines'))

# plotly figure layout
fig.update_layout(xaxis_title = 'X',yaxis_title = 'Y')

fig.show()

使用统计模型的代码:

import plotly.graph_objects as go
import statsmodels.api as sm
import pandas as pd
import numpy as np
import datetime

# data
np.random.seed(123)
numdays=20

X = (np.random.randint(low=-20,size=numdays).cumsum()+100).tolist()

df = pd.DataFrame({'X': X,'Y':Y})

# regression
df['bestfit'] = sm.OLS(df['Y'],sm.add_constant(df['X'])).fit().fittedvalues

# plotly figure setup
fig=go.Figure()
fig.add_trace(go.Scatter(name='X vs Y',mode='lines'))


# plotly figure layout
fig.update_layout(xaxis_title = 'X',yaxis_title = 'Y')

fig.show()
,

Plotly还带有用于统计模型的本机包装,用于绘制(非)线性线:

引用其文档,网址为:https://plotly.com/python/linear-fits/


import plotly.express as px

df = px.data.tips()
fig = px.scatter(df,x="total_bill",y="tip",trendline="ols")
fig.show()

enter image description here

本文链接:https://www.f2er.com/3160842.html

大家都在问