BA STOCK PRICE Project Neural Network Analysis
BA STOCK PRICE Project Neural Network Analysis
scaler = MinMaxScaler()
x_scaled = scaler.fit_transform(X) # Scale features
y_scaled = scaler.fit_transform(y.reshape(-1, 1)) # Scale target
from sklearn.preprocessing import MinMaxScaler
03-10-
0 696.950440 707.549988 740.000000 701.700012 735.000000 35367877
2017
04-10-
1 693.108887 703.650024 710.750000 699.000000 710.150024 3613280
2017
05-10-
2 684.145203 694.549988 704.549988 686.099976 704.549988 2680742
2017
06-10-
3 686.312317 696.750000 702.000000 689.000000 693.900024 1416485
2017
09-10-
4 688.183838 698.650024 702.500000 694.099976 694.099976 951473
2017
# Input Layer
model.add(Input(shape=(5,))) # Takes 5 input features
# Hidden Layers
model.add(Dense(64, activation='relu')) # First hidden layer with 64 neurons
model.add(Dense(32, activation='relu')) # Second hidden layer with 32 neurons
# Output Layer
model.add(Dense(1)) # Output layer for regression
Model: "sequential_4"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━
┃ Layer (type) ┃ Output Shape ┃ Param #
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━
│ dense_11 (Dense) │ (None, 64) │ 384
├─────────────────────────────────┼────────────────────────┼──────────────
│ dense_12 (Dense) │ (None, 32) │ 2,080
├─────────────────────────────────┼────────────────────────┼──────────────
│ dense_13 (Dense) │ (None, 1) │ 33
└─────────────────────────────────┴────────────────────────┴──────────────
Total params: 2,497 (9.75 KB)
Trainable params: 2,497 (9.75 KB)
Non-trainable params: 0 (0.00 B)
# Evaluate model
y_pred = model.predict(x_test)
from sklearn.metrics import mean_squared_error, r2_score
In [15]: print(len(x_train))
print(len(y_train))
print(len(x_test))
print(len(y_test))
y_test
1418
1418
355
355
Out[15]: array([ 648.2655029, 1180.099243 , 1489.949951 , 1103.567749 ,
657.8944702, 846.9360962, 1562.300049 , 743.2063599,
647.2775879, 1116.467896 , 560.5886841, 801.043335 ,
862.052002 , 931.4364014, 663.6768188, 1113.72937 ,
989.9175415, 666.4147949, 1406.274048 , 644.8571777,
643.5131836, 1706.050049 , 1238.163208 , 1291.430298 ,
980.3028564, 1223.488159 , 852.6355591, 1462. ,
1120.855713 , 859.7722168, 873.1038818, 664.0508423,
1280.188721 , 944.4211426, 678.6784058, 1129.453979 ,
1160.136719 , 1249.167358 , 555.8466797, 1131.790039 ,
1116.368408 , 1347.729126 , 1875.949951 , 1155.000366 ,
1326.966431 , 1152.962646 , 1145.597046 , 662.1456299,
653.8986816, 1060.744629 , 1753.650024 , 1558.800049 ,
1840.949951 , 575.9505615, 668.0894165, 655.875061 ,
1258.328613 , 699.2652588, 908.9310303, 615.4669189,
866.2645874, 1562.849976 , 1191.630127 , 689.0210571,
881.1327515, 982.5877686, 1212.75293 , 1157.882935 ,
1182.991943 , 703.9441528, 595.5605469, 1153.608765 ,
1245.084351 , 1217.897095 , 906.1607056, 650.8834229,
867.8504639, 689.5134888, 656.3184814, 1331.807739 ,
1100.583862 , 673.6547852, 1308.272095 , 867.7018433,
1066.923462 , 1408.020874 , 1136.435181 , 1268.271973 ,
983.4252319, 1307.525269 , 867.2318726, 789.446167 ,
1212.554077 , 646.2712402, 1231.29126 , 1148.736694 ,
1260.719482 , 1506.943237 , 697.8587036, 1188.051392 ,
1505.745239 , 662.8687744, 1220.807129 , 717.3359375,
539.9907227, 1569.949951 , 991.0078735, 667.8261108,
877.3164673, 820.3717651, 1501.099976 , 1294.874023 ,
567.8991699, 890.9951782, 650.112793 , 1085.717285 ,
1888.75 , 981.1453247, 1095.305786 , 1183.274536 ,
720.4580688, 1442.849976 , 593.8811035, 755.4478149,
1289.052002 , 821.4125366, 1254.096924 , 594.921875 ,
677.7051392, 644.779541 , 1503.149902 , 542.8063354,
1281.234375 , 1089.743164 , 1494.900024 , 689.4829712,
903.5834961, 735.4254761, 1546.372314 , 1468.050049 ,
1273.516357 , 808.9728394, 961.1229858, 1264.653198 ,
864.4804688, 693.6014404, 680.9586182, 1287.387451 ,
1063.238892 , 1452.199951 , 1744.900024 , 1185.367676 ,
596.6565552, 1438.016968 , 1146.302734 , 996.156189 ,
1392.798218 , 1633.199951 , 1128.609131 , 564.0957031,
833.0592041, 615.8126831, 670.69104 , 664.543396 ,
898.7266235, 1064.147095 , 1466.815186 , 1279.889893 ,
953.1437378, 1843.699951 , 778.4437256, 1290.232422 ,
1674.5 , 656.1688232, 656.6627808, 582.680481 ,
565.8739014, 1121.385742 , 622.1382446, 612.6019897,
866.8097534, 535.4957886, 1622.150024 , 673.1776123,
1257.980835 , 828.0039673, 572.394165 , 1226.162842 ,
975.6793213, 600.4013062, 902.2949219, 1116.816406 ,
1429.532227 , 978.5123291, 1432.726563 , 570.2701416,
1152.565063 , 1166.183105 , 794.6499634, 1288.984741 ,
661.9974976, 674.0202026, 794.2039185, 707.8202515,
752.3254395, 1184.622192 , 665.3804932, 892.4301758,
1342.787964 , 1238.874634 , 822.1063843, 962.6098633,
1391.800049 , 842.7234497, 1127.963013 , 960.5205688,
814.0280762, 1526.408203 , 755.2495728, 1043.769653 ,
1449.89563 , 1179.552734 , 1435.072266 , 694.5876465,
570.319519 , 1268.238281 , 590.9668579, 553.0805054,
558.5634766, 868.6930542, 823.1967773, 556.9827271,
876.6722412, 972.2002563, 994.9633179, 556.9334106,
1317.633301 , 798.8130493, 1274.959839 , 1124.931152 ,
907.8457031, 827.2110596, 1434.199951 , 1253.848022 ,
842.2279053, 696.9504395, 612.5032349, 1123.589355 ,
1103.717407 , 1440.949951 , 1311.793701 , 646.5141602,
1184.921631 , 831.8202515, 1262.263062 , 1074.093872 ,
614.923584 , 610.8803101, 1284.093506 , 693.2565918,
1624.150024 , 1139.024292 , 966.5250854, 994.0310669,
670.8391724, 1503.349976 , 665.3069458, 1427.400024 ,
600.598877 , 1323.822021 , 869.6346436, 1015.489746 ,
1121.800171 , 773.8841553, 658.3869019, 856.3030396,
723.5309448, 671.77771 , 853.2797852, 1256.835693 ,
667.9248657, 682.7929688, 652.1813354, 1450.744141 ,
747.4685059, 704.2888184, 694.7363281, 1737.400024 ,
1232.138062 , 822.7506104, 1433.425171 , 1290.744873 ,
724.7203369, 729.4285889, 982.7808838, 1488.576172 ,
856.6003418, 1255.441406 , 1510.550049 , 677.3978271,
1170.556641 , 1154.403931 , 639.6223755, 1733.75 ,
916.8656006, 665.2575073, 764.2199097, 861.0111694,
1086.065186 , 583.8044434, 1285.964722 , 738.052002 ,
636.4038086, 1608.800049 , 784.6883545, 791.2798462,
1150.676025 , 693.8442993, 776.6595459, 1272.520508 ,
570.5665283, 989.8938599, 899.3708496, 660.2584839,
647.0306396, 613.8862915, 640.7079468, 864.3317261,
1128.459961 , 728.6852417, 840.6418457, 979.0638428,
833.8521729, 835.0911865, 1289.234253 , 1299.715454 ,
1353.917969 , 1188.84668 , 1515.228271 , 853.6762085,
624.2120361, 658.3869019, 767.6395874, 1000.877747 ,
1100.782959 , 611.2189331, 1517.67395 , 868.0983276,
571.9002075, 855.3613281, 817.001709 , 1239.607056 ,
1144.949707 , 657.0578613, 1920.150024 ])
plt.figure(figsize=(10, 6))
plt.plot(dataset['Date'], dataset['Adj Close'], label='Adj Close Price', color='
plt.title('Adjusted Close Price Over Time')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.savefig('closeprice.png')
plt.show()
In [55]: # Training vs Validation Loss
plt.figure(figsize=(10, 6))
plt.plot(history.history['loss'], label='Training Loss', color='blue')
plt.plot(history.history['val_loss'], label='Validation Loss', color='orange')
plt.title('Model Training Performance')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
r2 = r2_score(y_test, y_pred)
print(f"R-Squared (R²): {r2}")
In [ ]: