使用放射线更新破折号回调

发布于 2025-02-13 11:44:54 字数 2202 浏览 0 评论 0原文

我是Python编码的新手,所以我对自己的无知表示歉意。我正在尝试创建一个使用标准偏差丢弃异常值的破折号应用程序。用户使用放射性输入选择标准偏差。

我的问题是,我需要对我的代码进行哪些修改,以便使用回调的放射性值更新max_deviations?

导入软件包,清洁数据并定义查询

import dash
import plotly.express as px
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import numpy as np

app = dash.Dash(__name__)
server = app.server

df=pd.read_csv(r'C:\SVS_GIS\POWER BI\CSV_DATA\QSAS2021.csv', encoding='unicode_escape')

#SET DATE OF VALUATION
df['TIME'] = ((pd.to_datetime(df['Sale Date'], dayfirst=True)
             .rsub(pd.to_datetime('01/10/2021', dayfirst=True))
             .dt.days
          )*-1)

df=df[df['TIME'] >= -365]

df = df.query("(SMA >=1 and SMA <= 3) and (LGA==60)")

准备数据框以删除离数器

data = pd.DataFrame(data=df)
x = df.TIME
y = df.CHANGE
mean = np.mean(y)
standard_deviation = np.std(y)
distance_from_mean = abs(y - mean)

应用程序布局

app.layout = html.Div([

html.Label("Standard Deviation Picker:", style={'fontSize':25, 'textAlign':'center'}),
    html.Br(),
       html.Label("1.0 = 68%, 2.0 = 95%, 3.0 = 99.7%", style={'fontSize':15, 
'textAlign':'center'}),
    
html.Div(id="radio_items"),
 dcc.RadioItems(      
options=[{'label': i, 'value': i} for i in [1.0, 2.0, 3.0]],
value=2.0
),          

html.Div([
    dcc.Graph(id="the_graph")]
)])

回调

@app.callback(
    Output("the_graph", "figure"),
    Input("radio_items", 'value')
)
def update_graph(max_deviations):
    not_outlier = distance_from_mean < max_deviations * standard_deviation
    no_outliers = y[not_outlier]
    trim_outliers = pd.DataFrame(data=no_outliers)
    dff = pd.merge(trim_outliers, df, left_index=True, right_index=True)
    return (dff)


    fig = px.scatter(dff, x='TIME', y='CHANGE_y',
                     
                     color ='SMA',
                     trendline='ols',
                     size='PV',
                     height=500,
                     width=800,
                     hover_name='SMA',

                     )
    return dcc.Graph(id='the_graph', figure=fig)


    

if __name__ == '__main__':
    app.run_server(debug=False)

I am fairly new to python coding so I apologize in advance for my ignorance. I am trying to create a Dash App that drops outliers using standard deviation. The user selects a standard deviation using RadioItem inputs.

My question is what amendments do I need to make to my code so that the RadioItem value updates max_deviations using a callback?

Import packages, clean the data and define a query

import dash
import plotly.express as px
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import numpy as np

app = dash.Dash(__name__)
server = app.server

df=pd.read_csv(r'C:\SVS_GIS\POWER BI\CSV_DATA\QSAS2021.csv', encoding='unicode_escape')

#SET DATE OF VALUATION
df['TIME'] = ((pd.to_datetime(df['Sale Date'], dayfirst=True)
             .rsub(pd.to_datetime('01/10/2021', dayfirst=True))
             .dt.days
          )*-1)

df=df[df['TIME'] >= -365]

df = df.query("(SMA >=1 and SMA <= 3) and (LGA==60)")

prepare dataframe for dropping outliers

data = pd.DataFrame(data=df)
x = df.TIME
y = df.CHANGE
mean = np.mean(y)
standard_deviation = np.std(y)
distance_from_mean = abs(y - mean)

app layout

app.layout = html.Div([

html.Label("Standard Deviation Picker:", style={'fontSize':25, 'textAlign':'center'}),
    html.Br(),
       html.Label("1.0 = 68%, 2.0 = 95%, 3.0 = 99.7%", style={'fontSize':15, 
'textAlign':'center'}),
    
html.Div(id="radio_items"),
 dcc.RadioItems(      
options=[{'label': i, 'value': i} for i in [1.0, 2.0, 3.0]],
value=2.0
),          

html.Div([
    dcc.Graph(id="the_graph")]
)])

callback

@app.callback(
    Output("the_graph", "figure"),
    Input("radio_items", 'value')
)
def update_graph(max_deviations):
    not_outlier = distance_from_mean < max_deviations * standard_deviation
    no_outliers = y[not_outlier]
    trim_outliers = pd.DataFrame(data=no_outliers)
    dff = pd.merge(trim_outliers, df, left_index=True, right_index=True)
    return (dff)


    fig = px.scatter(dff, x='TIME', y='CHANGE_y',
                     
                     color ='SMA',
                     trendline='ols',
                     size='PV',
                     height=500,
                     width=800,
                     hover_name='SMA',

                     )
    return dcc.Graph(id='the_graph', figure=fig)


    

if __name__ == '__main__':
    app.run_server(debug=False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-20 11:44:55

您的dcc.radioitems没有ID prop。加上它,并确保它与回调中给出的ID匹配,您应该很好。

Your dcc.RadioItems doesn't have an id prop. Add that, and make sure it matches the ID given in the callback, and you should be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文