-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualiser.py
More file actions
58 lines (46 loc) · 1.64 KB
/
Copy pathvisualiser.py
File metadata and controls
58 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import load_model
# Import from your Database Manager
from db_manager import load_from_db
# Load data from SQL
print("🔌 Querying Database for Visualization...")
df = load_from_db('NVDA')
# Safety Check
if df.empty:
print("❌ Error: Database is empty!")
exit()
# Preprocess
df['close'] = pd.to_numeric(df['close'], errors='coerce')
df['volume'] = pd.to_numeric(df['volume'], errors='coerce')
df['SMA20'] = df['close'].rolling(window=20).mean()
df.dropna(inplace=True)
features = ['close', 'SMA20', 'volume']
data_values = df[features].values
# Load Model & Scale
model = load_model('models/nvda_cnn_lstm_v2.keras')
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data_values)
close_scaler = MinMaxScaler(feature_range=(0, 1))
close_scaler.fit(data_values[:, 0].reshape(-1, 1))
# Prepare Test Data
test_inputs = scaled_data[len(scaled_data) - 100 - 60:]
x_test = []
for i in range(60, len(test_inputs)):
x_test.append(test_inputs[i-60:i, :])
x_test = np.array(x_test)
# Predict
predicted_prices = model.predict(x_test)
predicted_prices = close_scaler.inverse_transform(predicted_prices)
real_prices = data_values[len(data_values) - 100:, 0]
# Plot
plt.figure(figsize=(14, 7))
plt.plot(real_prices, color='black', label='Actual Price (Source: SQLite)', linewidth=2)
plt.plot(predicted_prices, color='green', label='AI Prediction', linewidth=2)
plt.title('NVIDIA Forecast: Powered by SQL Data Warehouse')
plt.xlabel('Time')
plt.ylabel('Price ($)')
plt.legend()
plt.show()