import%20marimo%0A%0A__generated_with%20%3D%20%220.13.6%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Anomaly%20Detection%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Anomaly%20detection%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Import%20packages%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Import%20required%20libraries%20for%20data%20manipulation%20and%20Nixtla%20client%20initialization.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20nixtla%20import%20NixtlaClient%0A%20%20%20%20return%20NixtlaClient%2C%20os%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Initialize%20Nixtla%20client%20with%20API%20key%20from%20environment%20variables.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(NixtlaClient%2C%20os)%3A%0A%20%20%20%20NIXTLA_API_KEY%20%3D%20os.environ%5B%22NIXTLA_API_KEY%22%5D%0A%20%20%20%20nixtla_client%20%3D%20NixtlaClient(api_key%3DNIXTLA_API_KEY)%0A%20%20%20%20return%20(nixtla_client%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Load%20dataset%0A%0A%20%20%20%20Now%2C%20let's%20load%20the%20dataset%20for%20this%20tutorial.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Load%20the%20Peyton%20Manning%20Wikipedia%20page%20views%20dataset%20and%20display%20first%2010%20rows.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd)%3A%0A%20%20%20%20%23%20Read%20the%20dataset%0A%20%20%20%20wikipedia%20%3D%20pd.read_csv(%22https%3A%2F%2Fdatasets-nixtla.s3.amazonaws.com%2Fpeyton-manning.csv%22%2C%20parse_dates%3D%5B%22ds%22%5D)%0A%20%20%20%20wikipedia.head(10)%0A%20%20%20%20return%20(wikipedia%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Plot%20the%20time%20series%20data%20to%20visualize%20the%20patterns.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20nixtla_client.plot(wikipedia)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Anomaly%20detection%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Detect%20anomalies%20in%20the%20time%20series%20using%20TimeGPT%20model%20with%20default%20settings.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20anomalies_df%20%3D%20nixtla_client.detect_anomalies(%0A%20%20%20%20%20%20%20%20wikipedia%2C%0A%20%20%20%20%20%20%20%20freq%3D%22D%22%2C%0A%20%20%20%20%20%20%20%20model%3D%22timegpt-1%22%2C%0A%20%20%20%20)%0A%20%20%20%20anomalies_df.head()%0A%20%20%20%20return%20(anomalies_df%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Visualize%20the%20detected%20anomalies%20on%20the%20time%20series%20plot.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(anomalies_df%2C%20nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20nixtla_client.plot(wikipedia%2C%20anomalies_df)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Anomaly%20detection%20with%20exogenous%20features%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Detect%20anomalies%20using%20TimeGPT%20with%20additional%20date-based%20features%20(month%20and%20year).%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20anomalies_df_exogenous%20%3D%20nixtla_client.detect_anomalies(%0A%20%20%20%20%20%20%20%20wikipedia%2C%0A%20%20%20%20%20%20%20%20freq%3D%22D%22%2C%0A%20%20%20%20%20%20%20%20date_features%3D%5B%22month%22%2C%20%22year%22%5D%2C%0A%20%20%20%20%20%20%20%20date_features_to_one_hot%3DTrue%2C%0A%20%20%20%20%20%20%20%20model%3D%22timegpt-1%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(anomalies_df_exogenous%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Plot%20the%20feature%20importance%20weights%20to%20understand%20which%20features%20contribute%20most%20to%20anomaly%20detection.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nixtla_client)%3A%0A%20%20%20%20nixtla_client.weights_x.plot.barh(%0A%20%20%20%20%20%20%20%20x%3D%22features%22%2C%0A%20%20%20%20%20%20%20%20y%3D%22weights%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Compare%20the%20number%20of%20anomalies%20detected%20with%20and%20without%20exogenous%20features.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(anomalies_df%2C%20anomalies_df_exogenous)%3A%0A%20%20%20%20%23%20Without%20exogenous%20features%0A%20%20%20%20print(%22Number%20of%20anomalies%20without%20exogenous%20features%3A%22%2C%20anomalies_df.anomaly.sum())%0A%0A%20%20%20%20%23%20With%20exogenous%20features%0A%20%20%20%20print(%22Number%20of%20anomalies%20with%20exogenous%20features%3A%22%2C%20anomalies_df_exogenous.anomaly.sum())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Visualize%20the%20anomalies%20detected%20using%20the%20model%20with%20exogenous%20features.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(anomalies_df_exogenous%2C%20nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20nixtla_client.plot(wikipedia%2C%20anomalies_df_exogenous)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Modifying%20the%20confidence%20intervals%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Detect%20anomalies%20using%20a%20lower%20confidence%20interval%20(70%25)%20to%20see%20how%20it%20affects%20the%20results.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20anomalies_df_70%20%3D%20nixtla_client.detect_anomalies(wikipedia%2C%20freq%3D%22D%22%2C%20level%3D70)%0A%20%20%20%20return%20(anomalies_df_70%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Compare%20the%20number%20of%20anomalies%20detected%20with%20different%20confidence%20intervals%20(99%25%20vs%2070%25).%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(anomalies_df%2C%20anomalies_df_70)%3A%0A%20%20%20%20%23%20Print%20and%20compare%20anomaly%20counts%0A%20%20%20%20print(%22Number%20of%20anomalies%20with%2099%25%20confidence%20interval%3A%22%2C%20anomalies_df.anomaly.sum())%0A%20%20%20%20print(%22Number%20of%20anomalies%20with%2070%25%20confidence%20interval%3A%22%2C%20anomalies_df_70.anomaly.sum())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Visualize%20the%20anomalies%20detected%20using%20the%2070%25%20confidence%20interval.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(anomalies_df_70%2C%20nixtla_client%2C%20wikipedia)%3A%0A%20%20%20%20nixtla_client.plot(wikipedia%2C%20anomalies_df_70)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b19f2c14cc9e8b84db2e7a2e2fbbbe80c156b40de33050d24cfa1dae89c3215d