我有数据框,其中给出了 x 和 y 列,我想在它们之间绘制回归模型,主要想法是我应该使用 dash 框架,因为根据 chow 检验,两个回归模型在不同实例值下可能会有差异,基于以下链接:dash 模型
我写了下面的代码:
import pandas as pd
from dash import Dash,html,dcc,callback,Output,Input
from sklearn.linear_model import LinearRegression
import plotly.express as px
data =pd.read_csv("regression.csv")
model =LinearRegression()
print(data)
app = Dash()
# Requires Dash 2.17.0 or later
app.layout = [
html.H1(children='Our regression Model', style={'textAlign':'center'}),
dcc.Dropdown(data.Year.unique(), '2004', id='dropdown-selection'),
dcc.Graph(id='graph-content')
]
@callback(
Output('graph-content', 'figure'),
Input('dropdown-selection', 'value')
)
def scatter_graph(value):
selected =data[data.Year==value]
return px.scatter(selected,x='x',y='y')
@callback(
Output('graph-content', 'figure'),
Input('dropdown-selection', 'value')
)
def Regression_graph(value):
selected =data[data.Year==value]
X =selected['x'].values
X =X.reshape(-1,1)
y =selected['y'].values
model.fit(X,y)
y_predicted =model.predict(X)
return px.line(selected,x='x',y=y_predicted)
if __name__ =='__main__':
app.run(debug=True)
这部分工作正常:
@callback(
Output('graph-content', 'figure'),
Input('dropdown-selection', 'value')
)
def scatter_graph(value):
selected =data[data.Year==value]
return px.scatter(selected,x='x',y='y')
请帮我解决该如何解决?