[Python] sketch partial regression chart using dash

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    i have dataframe, in which we are given x and y columns and between them i want to sketch regression model, main idea is that i should use dash framework, as according chow test , there could be difference between two regression model at different instance value,based on following link : dash models

    i wrote following code :

    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)


    this part works fine :

    @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')


    but second decorator for regression plot does not work, here is example : [​IMG]

    please help me how to fix it?

    Continue reading...

Compartilhe esta Página