Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

machine-learning/stock-prediction/.ipynb_checkpoints/stock_prediction-checkpoint.ipynb
12 changes: 6 additions & 6 deletions machine-learning/stock-prediction/stock_prediction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@
" \"\"\"\n",
" # if predicted future price is higher than the current, \n",
" # then calculate the true future price minus the current price, to get the buy profit\n",
" buy_profit = lambda current, true_future, pred_future: true_future - current if pred_future > current else 0\n",
" buy_profit = lambda current, pred_future, true_future: true_future - current if pred_future > current else 0\n",
" # if the predicted future price is lower than the current price,\n",
" # then subtract the true future price from the current price\n",
" sell_profit = lambda current, true_future, pred_future: current - true_future if pred_future < current else 0\n",
" sell_profit = lambda current, pred_future, true_future: current - true_future if pred_future < current else 0\n",
" X_test = data[\"X_test\"]\n",
" y_test = data[\"y_test\"]\n",
" # perform prediction and get prices\n",
Expand Down Expand Up @@ -528,9 +528,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6.6 64-bit",
"display_name": "Python 3",
"language": "python",
"name": "python36664bitea6884f10f474b21a2a2f022451e0d09"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -542,9 +542,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
28 changes: 14 additions & 14 deletions machine-learning/stock-prediction/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def plot_graph(test_df):

def get_final_df(model, data):
"""
This function takes the `model` and `data` dict to
construct a final dataframe that includes the features along
This function takes the `model` and `data` dict to
construct a final dataframe that includes the features along
with true and predicted prices of the testing dataset
"""
# if predicted future price is higher than the current,
# if predicted future price is higher than the current,
# then calculate the true future price minus the current price, to get the buy profit
buy_profit = lambda current, true_future, pred_future: true_future - current if pred_future > current else 0
buy_profit = lambda current, pred_future, true_future: true_future - current if pred_future > current else 0
# if the predicted future price is lower than the current price,
# then subtract the true future price from the current price
sell_profit = lambda current, true_future, pred_future: current - true_future if pred_future < current else 0
sell_profit = lambda current, pred_future, true_future: current - true_future if pred_future < current else 0
X_test = data["X_test"]
y_test = data["y_test"]
# perform prediction and get prices
Expand All @@ -47,16 +47,16 @@ def get_final_df(model, data):
test_df.sort_index(inplace=True)
final_df = test_df
# add the buy profit column
final_df["buy_profit"] = list(map(buy_profit,
final_df["adjclose"],
final_df[f"adjclose_{LOOKUP_STEP}"],
final_df["buy_profit"] = list(map(buy_profit,
final_df["adjclose"],
final_df[f"adjclose_{LOOKUP_STEP}"],
final_df[f"true_adjclose_{LOOKUP_STEP}"])
# since we don't have profit for last sequence, add 0's
)
# add the sell profit column
final_df["sell_profit"] = list(map(sell_profit,
final_df["adjclose"],
final_df[f"adjclose_{LOOKUP_STEP}"],
final_df["sell_profit"] = list(map(sell_profit,
final_df["adjclose"],
final_df[f"adjclose_{LOOKUP_STEP}"],
final_df[f"true_adjclose_{LOOKUP_STEP}"])
# since we don't have profit for last sequence, add 0's
)
Expand All @@ -79,8 +79,8 @@ def predict(model, data):


# load the data
data = load_data(ticker, N_STEPS, scale=SCALE, split_by_date=SPLIT_BY_DATE,
shuffle=SHUFFLE, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE,
data = load_data(ticker, N_STEPS, scale=SCALE, split_by_date=SPLIT_BY_DATE,
shuffle=SHUFFLE, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE,
feature_columns=FEATURE_COLUMNS)

# construct the model
Expand Down Expand Up @@ -129,4 +129,4 @@ def predict(model, data):
if not os.path.isdir(csv_results_folder):
os.mkdir(csv_results_folder)
csv_filename = os.path.join(csv_results_folder, model_name + ".csv")
final_df.to_csv(csv_filename)
final_df.to_csv(csv_filename)