윤영준 윤영준 2023-09-04
export function
@cdb9153fdf251778cdae9286ea71cceb7b546659
tools/algo/ANOVA.py
--- tools/algo/ANOVA.py
+++ tools/algo/ANOVA.py
@@ -6,6 +6,7 @@
 from tools.algo.humidity import absolute_humidity
 from tools.algo.interpolation import interpolate_value
 from datetime import datetime
+import plotly.express as px
 
 if __name__ == "__main__":
     df_weather = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308310000_f.csv")
@@ -30,5 +31,8 @@
 
     F_statistic, pVal = stats.f_oneway(df_failure[df_failure["불량 여부"] == 0].loc[:,['상대습도', '기온', '절대습도']],
                                        df_failure[df_failure["불량 여부"] == 1].loc[:,['상대습도', '기온', '절대습도']])
-
-    print(F_statistic, pVal)
(파일 끝에 줄바꿈 문자 없음)
+    fig_df = pd.DataFrame(np.transpose([F_statistic, pVal]), index=['상대습도', '기온', '절대습도'], columns=["F_", "pVal"])
+    fig = px.scatter(fig_df)
+    fig.add_hline(y=0.05, line_dash="dash", line_color="red")
+    fig.show()
+    print(fig_df)
(파일 끝에 줄바꿈 문자 없음)
tools/algo/SARIMA.py
--- tools/algo/SARIMA.py
+++ tools/algo/SARIMA.py
@@ -1,9 +1,12 @@
 import pandas as pd
+import numpy as np
 from statsmodels.tsa.statespace.sarimax import SARIMAX
+from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
 import matplotlib.pyplot as plt
 from datetime import datetime, timedelta
 import plotly.express as px
 from tools.algo.humidity import absolute_humidity
+import pickle
 
 
 def sarima(file, col_key='상대습도', future_hours=24):
@@ -34,6 +37,30 @@
 
     forecast_df.to_csv(f"{file.split('.')[0]}_forecast.csv", index=False)
 
+    with open('sarima_model.pkl', 'wb') as pkl_file:
+        pickle.dump(model_fit, pkl_file)
+
+def forecast_from_saved_model(file, future_hours=24):
+    # Load the saved model
+    with open('sarima_model.pkl', 'rb') as pkl_file:
+        loaded_model = pickle.load(pkl_file)
+
+    df = pd.read_csv(file)
+
+    df['관측시각'] = df['관측시각'].apply(lambda x: datetime.strptime(f"{x}", '%Y%m%d%H%M'))
+
+    # Forecast the next 'future_hours' using the loaded model
+    forecast_values = loaded_model.forecast(steps=future_hours)
+    forecast_index = [df['관측시각'].iloc[-1] + timedelta(hours=i) for i in range(1, future_hours + 1)]
+    forecast_df = pd.DataFrame({
+        '관측시각': forecast_index,
+        'forecast': forecast_values
+    })
+
+    forecast_df.to_csv(f"{file.split('.')[0]}_forecast.csv", index=False)
+
+    return forecast_df
+
 if __name__ == "__main__":
     df = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308310000_f.csv")
     ah = absolute_humidity(df["상대습도"], df["기온"])
@@ -49,13 +76,13 @@
     #         df["절대습도"],72,3)
     #     ))
     # fig.show()
-    # log_df = np.log(df["절대습도"])
-    diff_1 = (df["절대습도"].diff(periods=1).iloc[1:])
+    log_df = np.log(df["상대습도"])
+    diff_1 = (log_df.diff(periods=1).iloc[1:])
     diff_2 = diff_1.diff(periods=1).iloc[1:]
-    # plot_acf(diff_2)
-    # plot_pacf(diff_2)
+    plot_acf(diff_2)
+    plot_pacf(diff_2)
     plt.show()
-    model = SARIMAX(df["절대습도"], order=(1,0,2), seasonal_order=(0,1,2,24))
+    model = SARIMAX(df["상대습도"], order=(2,0,2), seasonal_order=(1,1,2,24))
     model_fit = model.fit()
     # ARIMA_model = pm.auto_arima(df['절대습도'],
     #                             start_p=1,
@@ -73,5 +100,5 @@
     print(model_fit.summary())
     df['forecast'] = model_fit.predict(start=-100, end=-1, dynamic=True)
     # df[['절대습도', 'forecast']].plot(figsize=(12, 8))
-    fig = px.line(df[['절대습도', 'forecast']])
+    fig = px.line(df[['상대습도', 'forecast']])
     fig.show()
(파일 끝에 줄바꿈 문자 없음)
Add a comment
List