Skip to content

Commit

Permalink
Finished training and deploying
Browse files Browse the repository at this point in the history
  • Loading branch information
roychaadit committed Nov 9, 2024
1 parent 320435c commit e02acf2
Show file tree
Hide file tree
Showing 2 changed files with 1,636 additions and 79 deletions.
105 changes: 26 additions & 79 deletions combined_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
st.pyplot(fig)


st.subheader('Closing Price vs Time Chart (MOVING AVERAGE 100 (R) AND 200 (G))')
st.subheader('MOVING AVERAGE 100 (R) AND 200 (G)')

ma100 = df.Close.rolling(100).mean()
ma200 = df.Close.rolling(200).mean()
Expand All @@ -42,97 +42,44 @@
testing = pd.DataFrame(df['Close'][int(len(df) * 0.7):int(len(df))])

scaler = MinMaxScaler(feature_range=(0,1))

training_array = scaler.fit_transform(training)

# Splitting training data into x_train and y_train

# x_train = []
# y_train = []
model = load_model('/Users/aaditroychowdhury/Documents/CS 222/Main branch/main-project-shmoney/keras_model.h5')

# for i in range(100, training_array.shape[0]):

# x_train.append(training_array[i - 100 : i])
# y_train.append(training_array[i, 0])

# x_train, y_train = np.array(x_train), np.array(y_train)

filename = 'random_forest_model.joblib'
model = joblib.load(filename)
past_100_days = training.tail(100)
final_df = pd.concat([past_100_days, testing], ignore_index=True)
input_data = scaler.fit_transform(final_df)

# Ensure 'Tomorrow' column is created by shifting 'Close' by -1
df["Tomorrow"] = df["Close"].shift(-1)

# Check if 'Tomorrow' column was successfully created
if "Tomorrow" in df.columns:
# Drop rows where 'Tomorrow' has NaN values (last row will have NaN after shift)
df = df.dropna(subset=["Tomorrow"])
x_test = []
y_test = []

# Now safely compare 'Tomorrow' with 'Close'
df["Target"] = (df["Tomorrow"] > df["Close"]).astype(int)
else:
st.error("The 'Tomorrow' column was not created.")
for i in range(100, input_data.shape[0]):



if "Target" in df.columns:
predictions = model.predict(df)

st.subheader("Predictions vs Original")
fig2 = plt.figure(figsize=(12, 6))
plt.plot(df["Target"], 'b', label='Original Price')
plt.plot(predictions, 'r', label='Predicted Price')
plt.legend()
st.pyplot(fig2)
else:
st.error("The 'Target' column was not created. Unable to make predictions.")
x_test.append(input_data[i - 100 : i])
y_test.append(input_data[i, 0])



if "Target" in df.columns:
# Exclude 'Tomorrow' and 'Target' columns for prediction
features = df.drop(["Tomorrow", "Target"], axis=1)
predictions = model.predict(features)
# ... rest of the plotting code ...
st.subheader("Predictions vs Original")
fig2 = plt.figure(figsize= (12, 6))
plt.plot(df["Target"], 'b', label='Original Price')
plt.plot(predictions, 'r', label='Predicted Price')

st.pyplot(fig2)

st.write("DataFrame columns:", df.columns)

# past_100_days = training.tail(100)
# final_df = pd.concat([past_100_days, testing], ignore_index=True)
# input_data = scaler.fit_transform(final_df)
x_test, y_test = np.array(x_test), np.array(y_test)


# x_test = []
# y_test = []

# for i in range(100, input_data.shape[0]):

# x_test.append(input_data[i - 100 : i])
# y_test.append(input_data[i, 0])

# x_test, y_test = np.array(y_test), np.array(y_test)
# x_test = x_test.reshape(1, -1)
# y_test = y_test.reshape(1, -1)
# y_predicted = model.predict(x_test)
# scaler = scaler.scale_
# Now you can make predictions
y_predicted = model.predict(x_test)
scaler = scaler.scale_

# scale_factor = 1 / scaler[0]
# y_predicted = y_predicted * scale_factor
# y_test = y_test * scale_factor
scale_factor = 1 / scaler[0]
y_predicted = y_predicted * scale_factor
y_test = y_test * scale_factor

# final figure

# st.subheader("Predictions vs Original")
# fig2 = plt.figure(figsize= (12, 6))
# plt.plot(y_test, 'b', label='Original Price')
# plt.plot(y_predicted, 'r', label='Predicted Price')
st.subheader("Predictions vs Original")
fig2 = plt.figure(figsize= (12, 6))
plt.plot(y_test, 'b', label='Original Price')
plt.plot(y_predicted, 'r', label='Predicted Price')

# plt.xlabel('Time')
# plt.ylabel('Price')
# plt.legend()
# st.pyplot(fig2)
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig2)
Loading

0 comments on commit e02acf2

Please sign in to comment.