query = """ SELECT * FROM `stockprediction-434721.stock_data.meta_prices` WHERE CAST(Date AS DATE) >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 YEAR) """ meta_df = client.query(query).to_dataframe() meta_df.head(10) # Check for any missing or null values print(meta_df.isnull().sum()) import pandas as pd # Ensure that Date is in datetime format meta_df['Date'] = pd.to_datetime(meta_df['Date']) # Drop columns that are not necessary for modeling # Adjust this based on your needs meta_df = meta_df.drop(columns=['Adj Close']) # Sort data by Date in ascending order meta_df = meta_df.sort_values(by='Date', ascending=True) # Preview updated dataframes print(meta_df.head()) # Feature Engineering for meta # 1. Moving Averages meta_df['7_day_MA'] = meta_df['Close'].rolling(window=7).mean() meta_df['30_day_MA'] = meta_df['Close'].rolling(window=30).mean() # 2. Volatility (Standard deviation of daily returns over 7 and 30 days) meta_df['7_day_volatility'] = meta_df['Close'].pct_change().rolling(window=7).std() meta_df['30_day_volatility'] = meta_df['Close'].pct_change().rolling(window=30).std() # 3. Lag Features (Previous day's price and volume) meta_df['Previous_Close'] = meta_df['Close'].shift(1) meta_df['Previous_Volume'] = meta_df['Volume'].shift(1) # 4. Daily Returns meta_df['Daily_Return'] = meta_df['Close'].pct_change() # Preview updated dataframe for meta print(meta_df.head()) # Check for missing values in each column for meta print(meta_df.isna().sum()) # Visualize where NaNs occur in meta data import matplotlib.pyplot as plt import seaborn as sns plt.figure(figsize=(10, 6)) sns.heatmap(meta_df.isna(), cbar=False, cmap="viridis") plt.title('meta Data Missing Values') plt.show() # Drop rows with NaN values in the meta dataframe meta_df_cleaned = meta_df.dropna() # Preview the cleaned meta dataframe print(meta_df_cleaned.head()) print(meta_df_cleaned.shape) # Define the filename for the Meta dataframe meta_csv_filename = "meta_cleaned_feature_engineered.csv" # Export the cleaned Meta dataframe to CSV meta_df_cleaned.to_csv(meta_csv_filename, index=False) print(f"Dataframe exported to CSV: {meta_csv_filename}") from google.colab import files # Download the Meta CSV file to your local machine files.download('meta_cleaned_feature_engineered.csv') from sklearn.model_selection import train_test_split # Define features and target variable X_meta = meta_df_cleaned[['7_day_MA', '30_day_MA', '7_day_volatility', '30_day_volatility', 'Previous_Close', 'Previous_Volume', 'Daily_Return']] y_meta = meta_df_cleaned['Close'] # Split the data X_train_meta, X_test_meta, y_train_meta, y_test_meta = train_test_split(X_meta, y_meta, test_size=0.2, random_state=42) # Preview the shapes print(X_train_meta.shape, X_test_meta.shape, y_train_meta.shape, y_test_meta.shape) from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score # Initialize the model model_meta = LinearRegression() # Train the model on the training data model_meta.fit(X_train_meta, y_train_meta) # Predict on the test data y_pred_meta = model_meta.predict(X_test_meta) # Evaluate the model mse_meta = mean_squared_error(y_test_meta, y_pred_meta) r2_meta = r2_score(y_test_meta, y_pred_meta) print("meta Linear Regression Performance:") print(f"Mean Squared Error: {mse_meta}") print(f"R-squared: {r2_meta}") import matplotlib.pyplot as plt import numpy as np # Define the cyberpunk theme colors cyberpunk_blue = '#00FFFF' cyberpunk_red = '#FF007F' cyberpunk_background = '#0D0D0D' # Customize the plot style plt.style.use('dark_background') # Plot for meta stock plt.figure(figsize=(10, 6)) plt.plot(np.arange(len(y_test_meta)), y_test_meta, color=cyberpunk_blue, label='Actual Price', linewidth=2) plt.plot(np.arange(len(y_pred_meta)), y_pred_meta, color=cyberpunk_red, linestyle='--', label='Predicted Price', linewidth=2) plt.title('meta Stock Price - Actual vs Predicted', fontsize=16, color=cyberpunk_blue) plt.xlabel('Date', fontsize=12, color='white') plt.ylabel('Price', fontsize=12, color='white') plt.legend(loc='upper left', fontsize=10) plt.grid(True, color='#333333') plt.gca().set_facecolor(cyberpunk_background) plt.show() from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_squared_error, r2_score # Initialize the model for meta rf_meta = RandomForestRegressor(n_estimators=100, random_state=42) # Train the model on the training data rf_meta.fit(X_train_meta, y_train_meta) # Predict on the test data y_pred_rf_meta = rf_meta.predict(X_test_meta) # Evaluate the model mse_rf_meta = mean_squared_error(y_test_meta, y_pred_rf_meta) r2_rf_meta = r2_score(y_test_meta, y_pred_rf_meta) print("meta Random Forest Performance:") print(f"Mean Squared Error: {mse_rf_meta}") print(f"R-squared: {r2_rf_meta}") # Visualization for Random Forest - meta plt.figure(figsize=(10, 6)) plt.plot(y_test_meta[:250].values, color="cyan", label="Actual Price") plt.plot(y_pred_rf_meta[:250], 'm--', label="Predicted Price") plt.title("meta Stock Price - Actual vs Predicted (Random Forest)", color="cyan") plt.xlabel("Date", color="cyan") plt.ylabel("Price", color="cyan") plt.legend(loc="best") plt.grid(True, linestyle='--', alpha=0.7) plt.gca().set_facecolor("black") plt.gca().spines["bottom"].set_color("cyan") plt.gca().spines["top"].set_color("cyan") plt.gca().spines["left"].set_color("cyan") plt.gca().spines["right"].set_color("cyan") plt.show() # Get feature importance from the Random Forest model importances_meta = rf_meta.feature_importances_ # Create a dataframe for the features and their importance feature_names_meta = X_train_meta.columns importance_df_meta = pd.DataFrame({ 'Feature': feature_names_meta, 'Importance': importances_meta }) # Sort the dataframe by importance importance_df_meta = importance_df_meta.sort_values(by='Importance', ascending=False) # Plot the feature importance plt.figure(figsize=(10, 6)) plt.barh(importance_df_meta['Feature'], importance_df_meta['Importance'], color='cyan') plt.xlabel('Feature Importance', color='cyan') plt.ylabel('Features', color='cyan') plt.title('meta Stock Feature Importance (Random Forest)', color='cyan') plt.gca().set_facecolor('black') plt.gca().spines['bottom'].set_color('cyan') plt.gca().spines['top'].set_color('cyan') plt.gca().spines['left'].set_color('cyan') plt.gca().spines['right'].set_color('cyan') plt.show() from sklearn.ensemble import GradientBoostingRegressor from sklearn.metrics import mean_squared_error, r2_score # Initialize the Gradient Boosting model for meta gb_meta = GradientBoostingRegressor(n_estimators=100, random_state=42) # Train the model on the training data gb_meta.fit(X_train_meta, y_train_meta) # Predict on the test data y_pred_gb_meta = gb_meta.predict(X_test_meta) # Evaluate the model mse_gb_meta = mean_squared_error(y_test_meta, y_pred_gb_meta) r2_gb_meta = r2_score(y_test_meta, y_pred_gb_meta) print("meta Gradient Boosting Performance:") print(f"Mean Squared Error: {mse_gb_meta}") print(f"R-squared: {r2_gb_meta}") # Visualization for Gradient Boosting - meta plt.figure(figsize=(10, 6)) plt.plot(y_test_meta[:250].values, color="cyan", label="Actual Price") plt.plot(y_pred_gb_meta[:250], 'm--', label="Predicted Price") plt.title("meta Stock Price - Actual vs Predicted (Gradient Boosting)", color="cyan") plt.xlabel("Date", color="cyan") plt.ylabel("Price", color="cyan") plt.legend(loc="best") plt.grid(True, linestyle='--', alpha=0.7) plt.gca().set_facecolor("black") plt.gca().spines["bottom"].set_color("cyan") plt.gca().spines["top"].set_color("cyan") plt.gca().spines["left"].set_color("cyan") plt.gca().spines["right"].set_color("cyan") plt.show() from sklearn.model_selection import GridSearchCV # Define the parameter grid for Gradient Boosting param_grid = { 'n_estimators': [100, 200, 300], 'learning_rate': [0.01, 0.1, 0.2], 'max_depth': [3, 5, 7], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4] } # Initialize the model gb_meta = GradientBoostingRegressor(random_state=42) # Initialize GridSearchCV grid_search_meta = GridSearchCV(estimator=gb_meta, param_grid=param_grid, cv=5, n_jobs=-1, verbose=2) # Fit the model to the training data grid_search_meta.fit(X_train_meta, y_train_meta) # Get the best parameters best_params_meta = grid_search_meta.best_params_ print("Best parameters for meta:", best_params_meta) # Evaluate the model with the best parameters best_gb_meta = grid_search_meta.best_estimator_ y_pred_meta = best_gb_meta.predict(X_test_meta) mse_meta = mean_squared_error(y_test_meta, y_pred_meta) r2_meta = r2_score(y_test_meta, y_pred_meta) print(f"meta Gradient Boosting Performance (Tuned):") print(f"Mean Squared Error: {mse_meta}") print(f"R-squared: {r2_meta}") import joblib joblib.dump(best_gb_meta, 'best_gb_meta_model.pkl') from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error, r2_score # Feature scaling scaler = StandardScaler() X_train_meta_scaled = scaler.fit_transform(X_train_meta) X_test_meta_scaled = scaler.transform(X_test_meta) # Define the neural network model model_meta = Sequential([ Dense(64, input_dim=X_train_meta.shape[1], activation='relu'), Dense(32, activation='relu'), Dense(1) # Output layer ]) # Compile the model model_meta.compile(optimizer='adam', loss='mean_squared_error') # Train the model history_meta = model_meta.fit(X_train_meta_scaled, y_train_meta, validation_split=0.2, epochs=50, batch_size=32) # Predict on the test set y_pred_nn_meta = model_meta.predict(X_test_meta_scaled) # Evaluate the performance mse_meta_nn = mean_squared_error(y_test_meta, y_pred_nn_meta) r2_meta_nn = r2_score(y_test_meta, y_pred_nn_meta) print(f"meta Neural Network Performance:") print(f"Mean Squared Error: {mse_meta_nn}") print(f"R-squared: {r2_meta_nn}") # meta Neural Network Predictions Visualization plt.figure(figsize=(10, 6)) plt.plot(y_test_meta[:250].values, color="cyan", label="Actual Price") plt.plot(y_pred_meta[:250], 'm--', label="Predicted Price") plt.title("meta Stock Price - Actual vs Predicted (Neural Network)", color="cyan") plt.xlabel("Date", color="cyan") plt.ylabel("Price", color="cyan") plt.legend(loc="best") plt.grid(True, linestyle="--", alpha=0.7) plt.gca().set_facecolor("black") plt.gca().spines["bottom"].set_color("cyan") plt.gca().spines["top"].set_color("cyan") plt.gca().spines["left"].set_color("cyan") plt.gca().spines["right"].set_color("cyan") plt.show() # Save the Neural Network model for meta in the native Keras format model_meta.save('best_nn_meta_model_tuned.keras') joblib.dump(model_meta, 'linear_reg_meta_model.pkl') joblib.dump(rf_meta, 'random_forest_meta_model.pkl') joblib.dump(best_gb_meta, 'gradient_boost_meta_model.pkl') # Load all Models for meta: from tensorflow.keras.models import load_model # Load Linear Regression model linear_reg_meta_model = joblib.load('linear_reg_meta_model.pkl') # Load Random Forest model random_forest_meta_model = joblib.load('random_forest_meta_model.pkl') # Load Gradient Boosting model gradient_boost_meta_model = joblib.load('gradient_boost_meta_model.pkl') # Load Neural Network model for meta best_nn_meta_model = load_model('best_nn_meta_model_tuned.keras') from google.colab import files # Download the saved models to your local machine files.download('linear_reg_meta_model.pkl') files.download('random_forest_meta_model.pkl') files.download('gradient_boost_meta_model.pkl') files.download('best_nn_meta_model_tuned.keras') import matplotlib.pyplot as plt import numpy as np # Define colors cyberpunk_blue = '#00FFFF' cyberpunk_pink = '#FF1493' # Pink color for Gradient Boosting cyberpunk_background = '#000D0D' random_forest_color = '#FF00FF' # Magenta for Random Forest lstm_color = '#FFFF00' # Yellow for LSTM # Create subplots: 2 rows, 2 columns fig, axs = plt.subplots(2, 2, figsize=(15, 10)) fig.subplots_adjust(hspace=0.6, top=0.70) # Adjusting space between the charts and shifting top margin for title # Title for the entire figure fig.suptitle('Meta Stock Price Prediction - Model Comparison', fontsize=18, color='white') # Table with model performance metrics table_data = [ ["Model", "R-squared", "Mean Squared Error"], ["Linear Regression", round(0.9994, 4), round(5.5953, 4)], ["Random Forest", round(0.9986, 4), round(13.6263, 4)], ["Gradient Boosting", round(0.9993, 4), round(6.4322, 4)], ["LSTM Neural Network", round(0.9388, 4), "--"] # Replacing the LSTM MSE with "--" ] # Add the table without extra space ax_table = fig.add_axes([0.1, 0.78, 0.8, 0.12]) # Shifting the table slightly lower ax_table.axis('off') table = ax_table.table(cellText=table_data, colWidths=[0.3]*3, loc='center', cellLoc='center') table.auto_set_font_size(False) table.set_fontsize(12) table.scale(1, 1.5) # Set table background to black and text to white for key, cell in table.get_celld().items(): cell.set_edgecolor('white') cell.set_text_props(color='white') cell.set_facecolor('black') # Plot 1: Linear Regression axs[0, 0].plot(np.arange(len(y_test_meta[:250])), y_test_meta[:250], color=cyberpunk_blue, label='Actual Price') axs[0, 0].plot(np.arange(len(y_pred_meta[:250])), y_pred_meta[:250], 'm--', label='Predicted Price (LR)', alpha=0.7) axs[0, 0].set_title('Linear Regression', fontsize=12, color='white') axs[0, 0].set_xlabel('Date', fontsize=10, color='white') axs[0, 0].set_ylabel('Price', fontsize=10, color='white') axs[0, 0].legend(loc='upper left') axs[0, 0].grid(True, linestyle='--', alpha=0.7) axs[0, 0].set_facecolor(cyberpunk_background) # Plot 2: Random Forest (Magenta) axs[0, 1].plot(np.arange(len(y_test_meta[:250])), y_test_meta[:250], color=cyberpunk_blue, label='Actual Price') axs[0, 1].plot(np.arange(len(y_pred_rf_meta[:250])), y_pred_rf_meta[:250], color=random_forest_color, label='Predicted Price (RF)', alpha=0.7) axs[0, 1].set_title('Random Forest', fontsize=12, color='white') axs[0, 1].set_xlabel('Date', fontsize=10, color='white') axs[0, 1].set_ylabel('Price', fontsize=10, color='white') axs[0, 1].legend(loc='upper left') axs[0, 1].grid(True, linestyle='--', alpha=0.7) axs[0, 1].set_facecolor(cyberpunk_background) # Plot 3: Gradient Boosting (Pink) axs[1, 0].plot(np.arange(len(y_test_meta[:250])), y_test_meta[:250], color=cyberpunk_blue, label='Actual Price') axs[1, 0].plot(np.arange(len(y_pred_gb_meta[:250])), y_pred_gb_meta[:250], color=cyberpunk_pink, label='Predicted Price (GB)', alpha=0.7) # Pink color axs[1, 0].set_title('Gradient Boosting', fontsize=12, color='white') axs[1, 0].set_xlabel('Date', fontsize=10, color='white') axs[1, 0].set_ylabel('Price', fontsize=10, color='white') axs[1, 0].legend(loc='upper left') axs[1, 0].grid(True, linestyle='--', alpha=0.7) axs[1, 0].set_facecolor(cyberpunk_background) # Plot 4: LSTM Neural Network (Yellow) axs[1, 1].plot(np.arange(len(y_test_meta[:250])), y_test_meta[:250], color=cyberpunk_blue, label='Actual Price') axs[1, 1].plot(np.arange(len(y_pred_nn_meta[:250])), y_pred_nn_meta[:250], color=lstm_color, label='Predicted Price (NN)', alpha=0.7) axs[1, 1].set_title('LSTM Neural Network', fontsize=12, color='white') axs[1, 1].set_xlabel('Date', fontsize=10, color='white') axs[1, 1].set_ylabel('Price', fontsize=10, color='white') axs[1, 1].legend(loc='upper left') axs[1, 1].grid(True, linestyle='--', alpha=0.7) axs[1, 1].set_facecolor(cyberpunk_background) # Display the final dashboard plt.show()