atsuga commited on
Commit
89a96d1
·
verified ·
1 Parent(s): 237c846

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -49
app.py CHANGED
@@ -1,9 +1,10 @@
1
  from fastapi import FastAPI
 
2
  from flask import Flask, render_template, request, jsonify
3
- from fastapi.middleware.wsgi import WSGIMiddleware
4
  import pickle
5
  import numpy as np
6
  import pandas as pd
 
7
 
8
  flask_app = Flask(__name__)
9
 
@@ -14,64 +15,71 @@ with open('model.pkl', 'rb') as model_file:
14
  with open('scaler.pkl', 'rb') as scaler_file:
15
  scaler = pickle.load(scaler_file)
16
 
17
- # Fungsi untuk membentuk windowed data
18
- def create_windowed_data(sell_features, buy_features):
19
- # Membuat DataFrame dengan lag 7 dari fitur sell dan buy
20
- windowed_data = pd.DataFrame({
21
- 'sell-1': [sell_features[6]],
22
- 'sell-2': [sell_features[5]],
23
- 'sell-3': [sell_features[4]],
24
- 'sell-4': [sell_features[3]],
25
- 'sell-5': [sell_features[2]],
26
- 'sell-6': [sell_features[1]],
27
- 'sell-7': [sell_features[0]],
28
- 'buy-1': [buy_features[6]],
29
- 'buy-2': [buy_features[5]],
30
- 'buy-3': [buy_features[4]],
31
- 'buy-4': [buy_features[3]],
32
- 'buy-5': [buy_features[2]],
33
- 'buy-6': [buy_features[1]],
34
- 'buy-7': [buy_features[0]],
35
- })
36
- return windowed_data
37
-
38
  # Fungsi prediksi untuk 7 hari ke depan
39
- def predict_7_days(windowed_data, linear_model, scaler):
40
- # Normalisasi data fitur
41
- normalized_data = scaler.transform(windowed_data.values)
42
-
43
- # Ambil baris terakhir sebagai input awal
44
- last_row = normalized_data[-1].reshape(1, -1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Inisialisasi untuk menyimpan prediksi
47
  predictions = []
48
 
49
  # Iterasi untuk 7 hari ke depan
50
  for _ in range(7):
51
- # Prediksi nilai normalisasi untuk hari berikutnya
52
  predicted_value_normalized = linear_model.predict(last_row)
53
-
54
- # Inverse transform hasil prediksi untuk mendapatkan nilai asli
55
- predicted_value = scaler.inverse_transform(
56
- np.hstack([last_row[0, 2:], predicted_value_normalized[0]]).reshape(1, -1)
57
- )[:, -2:] # Ambil hanya kolom 'sell' dan 'buy'
58
-
59
- # Simpan hasil prediksi
60
  predictions.append(predicted_value[0])
 
 
 
 
61
 
62
- # Perbarui `last_row` untuk prediksi berikutnya
63
- new_row_normalized = np.hstack([last_row[0, 2:], predicted_value_normalized[0]])
64
- last_row = new_row_normalized.reshape(1, -1)
65
-
66
- # Buat DataFrame untuk hasil prediksi
67
  predictions_df = pd.DataFrame(
68
- predictions,
69
- columns=['sell', 'buy'],
70
  index=pd.date_range(start=pd.Timestamp.today(), periods=7)
71
  )
72
 
73
  # Harga terakhir
74
- last_price = scaler.inverse_transform(normalized_data[-1].reshape(1, -1))[:, -2:]
75
 
76
  # Hitung persentase perubahan harian
77
  predictions_df['sell_change'] = predictions_df['sell'].pct_change().fillna(0) * 100
@@ -96,11 +104,8 @@ def predict():
96
  sell_features = data['sell_features']
97
  buy_features = data['buy_features']
98
 
99
- # Bentuk data menjadi windowed_data
100
- windowed_data = create_windowed_data(sell_features, buy_features)
101
-
102
  # Prediksi harga untuk 7 hari ke depan
103
- predictions_df, total_sell_change, total_buy_change, last_price = predict_7_days(windowed_data, linear_model, scaler)
104
 
105
  # Membuat hasil prediksi untuk respons JSON
106
  predictions_result = []
 
1
  from fastapi import FastAPI
2
+ from asgiref.wsgi import WsgiToAsgi
3
  from flask import Flask, render_template, request, jsonify
 
4
  import pickle
5
  import numpy as np
6
  import pandas as pd
7
+ from fastapi.middleware.wsgi import WSGIMiddleware
8
 
9
  flask_app = Flask(__name__)
10
 
 
15
  with open('scaler.pkl', 'rb') as scaler_file:
16
  scaler = pickle.load(scaler_file)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # Fungsi prediksi untuk 7 hari ke depan
19
+ def predict_7_days(sell_features, buy_features, linear_model, scaler):
20
+ # Data dari frontend
21
+ # Ambil nilai input (pastikan semuanya adalah angka)
22
+ sell_1 = float(sell_features[6])
23
+ sell_2 = float(sell_features[5])
24
+ sell_3 = float(sell_features[4])
25
+ sell_4 = float(sell_features[3])
26
+ sell_5 = float(sell_features[2])
27
+ sell_6 = float(sell_features[1])
28
+ sell_7 = float(sell_features[0])
29
+
30
+ buy_1 = float(buy_features[6])
31
+ buy_2 = float(buy_features[5])
32
+ buy_3 = float(buy_features[4])
33
+ buy_4 = float(buy_features[3])
34
+ buy_5 = float(buy_features[2])
35
+ buy_6 = float(buy_features[1])
36
+ buy_7 = float(buy_features[0])
37
+
38
+ # Menyusun data jual dan beli sebagai pasangan (sell, buy)
39
+ data = np.array([
40
+ [sell_1, buy_1],
41
+ [sell_2, buy_2],
42
+ [sell_3, buy_3],
43
+ [sell_4, buy_4],
44
+ [sell_5, buy_5],
45
+ [sell_6, buy_6],
46
+ [sell_7, buy_7]
47
+ ])
48
+
49
+ # Normalisasi data menggunakan scaler (menormalkan pasangan jual dan beli)
50
+ normalized_data = scaler.transform(data)
51
+
52
+ # Buat DataFrame dari nilai yang telah dinormalisasi
53
+ # Kolom disesuaikan untuk 7 fitur jual dan 7 fitur beli
54
+ last_row = pd.DataFrame(
55
+ normalized_data.flatten().reshape(1, -1),
56
+ columns=['sell-1', 'sell-2', 'sell-3', 'sell-4', 'sell-5', 'sell-6', 'sell-7', 'buy-1', 'buy-2', 'buy-3', 'buy-4', 'buy-5', 'buy-6', 'buy-7']
57
+ )
58
 
 
59
  predictions = []
60
 
61
  # Iterasi untuk 7 hari ke depan
62
  for _ in range(7):
63
+ # Prediksi nilai untuk hari berikutnya
64
  predicted_value_normalized = linear_model.predict(last_row)
65
+ predicted_value = scaler.inverse_transform(predicted_value_normalized.reshape(-1, 2))
66
+
67
+ # Simpan prediksi
 
 
 
 
68
  predictions.append(predicted_value[0])
69
+
70
+ # Update input untuk iterasi berikutnya
71
+ new_row_normalized = np.hstack([last_row.iloc[0, 2:], predicted_value_normalized[0]])
72
+ last_row = pd.DataFrame(new_row_normalized.reshape(1, -1), columns=last_row.columns)
73
 
74
+ # Transformasikan prediksi menjadi DataFrame untuk visualisasi
 
 
 
 
75
  predictions_df = pd.DataFrame(
76
+ predictions,
77
+ columns=['sell', 'buy'],
78
  index=pd.date_range(start=pd.Timestamp.today(), periods=7)
79
  )
80
 
81
  # Harga terakhir
82
+ last_price = scaler.inverse_transform(normalized_data[-1].reshape(-1, 2))
83
 
84
  # Hitung persentase perubahan harian
85
  predictions_df['sell_change'] = predictions_df['sell'].pct_change().fillna(0) * 100
 
104
  sell_features = data['sell_features']
105
  buy_features = data['buy_features']
106
 
 
 
 
107
  # Prediksi harga untuk 7 hari ke depan
108
+ predictions_df, total_sell_change, total_buy_change, last_price = predict_7_days(sell_features, buy_features, linear_model, scaler)
109
 
110
  # Membuat hasil prediksi untuk respons JSON
111
  predictions_result = []