{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":90765,"databundleVersionId":10583383,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:27:51.206635Z","iopub.execute_input":"2025-02-15T19:27:51.206964Z","iopub.status.idle":"2025-02-15T19:27:51.605447Z","shell.execute_reply.started":"2025-02-15T19:27:51.206940Z","shell.execute_reply":"2025-02-15T19:27:51.604450Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load the dependencies ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Input, LSTM, Dropout, Dense, Activation","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:27:51.606791Z","iopub.execute_input":"2025-02-15T19:27:51.607344Z","iopub.status.idle":"2025-02-15T19:28:07.252784Z","shell.execute_reply.started":"2025-02-15T19:27:51.607272Z","shell.execute_reply":"2025-02-15T19:28:07.251848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/directional-forecasting-cryptocurrencies/train.csv')\ndf_test=pd.read_csv('/kaggle/input/directional-forecasting-cryptocurrencies/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:07.254395Z","iopub.execute_input":"2025-02-15T19:28:07.255064Z","iopub.status.idle":"2025-02-15T19:28:13.460688Z","shell.execute_reply.started":"2025-02-15T19:28:07.255032Z","shell.execute_reply":"2025-02-15T19:28:13.459787Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Data Preprocessing**","metadata":{}},{"cell_type":"code","source":"# Check Dataset\ndf.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.462080Z","iopub.execute_input":"2025-02-15T19:28:13.462453Z","iopub.status.idle":"2025-02-15T19:28:13.495182Z","shell.execute_reply.started":"2025-02-15T19:28:13.462410Z","shell.execute_reply":"2025-02-15T19:28:13.494211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.495951Z","iopub.execute_input":"2025-02-15T19:28:13.496213Z","iopub.status.idle":"2025-02-15T19:28:13.514390Z","shell.execute_reply.started":"2025-02-15T19:28:13.496191Z","shell.execute_reply":"2025-02-15T19:28:13.513501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert timestamp to datetime (if needed, adjust the unit if it's not in milliseconds)\ndf['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')  # or 's' for seconds\n# Set timestamp as index\ndf = df.set_index('timestamp')\n\ndf_test['timestamp'] = pd.to_datetime(df_test['timestamp'], unit='ms')  # or 's' for seconds\n# Set timestamp as index\ndf_test = df_test.set_index('timestamp')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.515559Z","iopub.execute_input":"2025-02-15T19:28:13.515829Z","iopub.status.idle":"2025-02-15T19:28:13.873746Z","shell.execute_reply.started":"2025-02-15T19:28:13.515807Z","shell.execute_reply":"2025-02-15T19:28:13.872845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.874552Z","iopub.execute_input":"2025-02-15T19:28:13.874832Z","iopub.status.idle":"2025-02-15T19:28:13.895301Z","shell.execute_reply.started":"2025-02-15T19:28:13.874807Z","shell.execute_reply":"2025-02-15T19:28:13.894089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.896566Z","iopub.execute_input":"2025-02-15T19:28:13.896953Z","iopub.status.idle":"2025-02-15T19:28:13.924110Z","shell.execute_reply.started":"2025-02-15T19:28:13.896910Z","shell.execute_reply":"2025-02-15T19:28:13.923008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ta","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:13.927654Z","iopub.execute_input":"2025-02-15T19:28:13.927987Z","iopub.status.idle":"2025-02-15T19:28:21.791414Z","shell.execute_reply.started":"2025-02-15T19:28:13.927963Z","shell.execute_reply":"2025-02-15T19:28:21.790057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Feature Engineering (Example: creating lagged features)\ndef create_features(df, lags=7):\n    # Lagged Features (as before)\n    for i in range(1, lags + 1):\n        df[f'close_lag_{i}'] = df['close'].shift(i)\n\n    # Moving Averages\n    df['close_ma_5'] = df['close'].rolling(window=5).mean()  # 5-day moving average\n    df['close_ma_10'] = df['close'].rolling(window=10).mean() # 10-day moving average\n    df['close_ma_20'] = df['close'].rolling(window=20).mean() # 20-day moving average\n\n    # Volatility (using rolling standard deviation)\n    df['close_volatility_5'] = df['close'].rolling(window=5).std()\n    df['close_volatility_10'] = df['close'].rolling(window=10).std()\n    df['close_volatility_20'] = df['close'].rolling(window=20).std()\n\n\n    # Example: RSI (Relative Strength Index) - Requires ta library (pip install ta)\n    try:\n      from ta.momentum import RSIIndicator\n      rsi_indicator = RSIIndicator(close=df['close'], window=14) # 14-day RSI is common\n      df['rsi_14'] = rsi_indicator.rsi()\n    except ImportError:\n      print(\"ta library not found. Install it with: pip install ta\")\n\n\n    return df\n\ndf = create_features(df, lags=7)  # Create features (adjust lags as needed)\ndf_test = create_features(df_test, lags=7)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:21.793323Z","iopub.execute_input":"2025-02-15T19:28:21.793645Z","iopub.status.idle":"2025-02-15T19:28:22.891829Z","shell.execute_reply.started":"2025-02-15T19:28:21.793617Z","shell.execute_reply":"2025-02-15T19:28:22.890606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handle missing values (after creating lags)\ndf = df.dropna()  # Or use imputation methods if you prefer\ndf_test = df_test.dropna()\nrow_id=df_test['row_id']\ndf_test = df_test.drop('row_id', axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:22.892933Z","iopub.execute_input":"2025-02-15T19:28:22.893268Z","iopub.status.idle":"2025-02-15T19:28:23.626413Z","shell.execute_reply.started":"2025-02-15T19:28:22.893228Z","shell.execute_reply":"2025-02-15T19:28:23.625483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Feature Scaling (MinMaxScaler is common for neural networks)\nscaler = MinMaxScaler()\nnumerical_cols = ['open', 'high', 'low', 'close', 'volume', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_volume', 'taker_buy_quote_volume'] + [col for col in df.columns if 'lag' in col] # Include lagged features\ndf.loc[:, numerical_cols] = scaler.fit_transform(df[numerical_cols])  # Use .loc\n\ndf_test.loc[:, numerical_cols] = scaler.fit_transform(df_test[numerical_cols])  # Use .loc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:23.627349Z","iopub.execute_input":"2025-02-15T19:28:23.627601Z","iopub.status.idle":"2025-02-15T19:28:24.620474Z","shell.execute_reply.started":"2025-02-15T19:28:23.627580Z","shell.execute_reply":"2025-02-15T19:28:24.619547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare data for the model\nX = df.drop('target', axis=1)\ny = df['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:24.621517Z","iopub.execute_input":"2025-02-15T19:28:24.621909Z","iopub.status.idle":"2025-02-15T19:28:24.796101Z","shell.execute_reply.started":"2025-02-15T19:28:24.621871Z","shell.execute_reply":"2025-02-15T19:28:24.795254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Data Splitting\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, shuffle=False) # Important: shuffle=False for time series","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:24.797072Z","iopub.execute_input":"2025-02-15T19:28:24.797377Z","iopub.status.idle":"2025-02-15T19:28:25.106667Z","shell.execute_reply.started":"2025-02-15T19:28:24.797352Z","shell.execute_reply":"2025-02-15T19:28:25.105614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Model Building (LSTM Example - you can experiment with other architectures)\n\n# Get the number of features (columns in X_train)\nn_features = X_train.shape[1]\n\nmodel = keras.Sequential([\n    Input(shape=(n_features, 1)),\n    LSTM(50, activation='relu'),\n    Dropout(0.2),\n    Dense(1),                       # Output layer with 1 neuron\n    Activation('sigmoid')           # Sigmoid activation for binary classification\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:25.107591Z","iopub.execute_input":"2025-02-15T19:28:25.107847Z","iopub.status.idle":"2025-02-15T19:28:25.230221Z","shell.execute_reply.started":"2025-02-15T19:28:25.107826Z","shell.execute_reply":"2025-02-15T19:28:25.229383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshape input data for LSTM (samples, timesteps, features)\nX_train = X_train.values.reshape(X_train.shape[0], X_train.shape[1], 1)\nX_test = X_test.values.reshape(X_test.shape[0], X_test.shape[1], 1)    \ndf_test = df_test.values.reshape(df_test.shape[0], df_test.shape[1], 1) # If df_test is also a DataFrame, keep .values, otherwise remove it as well","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:25.231097Z","iopub.execute_input":"2025-02-15T19:28:25.231409Z","iopub.status.idle":"2025-02-15T19:28:25.449250Z","shell.execute_reply.started":"2025-02-15T19:28:25.231383Z","shell.execute_reply":"2025-02-15T19:28:25.448159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Model Compilation and Training\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Use binary_crossentropy\nhistory = model.fit(X_train, y_train, epochs=1, batch_size=32, validation_data=(X_test, y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:28:25.450250Z","iopub.execute_input":"2025-02-15T19:28:25.450559Z","iopub.status.idle":"2025-02-15T19:39:08.667530Z","shell.execute_reply.started":"2025-02-15T19:28:25.450535Z","shell.execute_reply":"2025-02-15T19:39:08.666337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model Evaluation\nloss, accuracy = model.evaluate(X_test, y_test, verbose=0)\nprint(f\"Test Loss: {loss}\")\nprint(f\"Test Accuracy: {accuracy}\")\n\n# Prediction\nprobabilities = model.predict(X_test) # Get probabilities\npredictions = (probabilities > 0.5).astype(int)  # Convert probabilities to 0 or 1 (threshold at 0.5)\n\n# Save the model\nmodel.save(\"crypto_forecasting_model.h5\")\n\n# To load the model later:\n# loaded_model = keras.models.load_model(\"crypto_forecasting_model.h5\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:39:08.668973Z","iopub.execute_input":"2025-02-15T19:39:08.669340Z","iopub.status.idle":"2025-02-15T19:40:09.054373Z","shell.execute_reply.started":"2025-02-15T19:39:08.669266Z","shell.execute_reply":"2025-02-15T19:40:09.053133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probabilities = model.predict(df_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:40:09.055469Z","iopub.execute_input":"2025-02-15T19:40:09.055754Z","iopub.status.idle":"2025-02-15T19:42:27.827543Z","shell.execute_reply.started":"2025-02-15T19:40:09.055729Z","shell.execute_reply":"2025-02-15T19:42:27.826373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = (probabilities > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:42:27.829802Z","iopub.execute_input":"2025-02-15T19:42:27.830124Z","iopub.status.idle":"2025-02-15T19:42:27.835820Z","shell.execute_reply.started":"2025-02-15T19:42:27.830096Z","shell.execute_reply":"2025-02-15T19:42:27.835002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(row_id))\nprint(len(predictions))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:42:27.836741Z","iopub.execute_input":"2025-02-15T19:42:27.837024Z","iopub.status.idle":"2025-02-15T19:42:27.856778Z","shell.execute_reply.started":"2025-02-15T19:42:27.836993Z","shell.execute_reply":"2025-02-15T19:42:27.855668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make sure row_id and predictions are 1D:\n\nif isinstance(row_id, pd.DataFrame) or isinstance(row_id, pd.Series):\n    row_id = row_id.values.ravel()  # Convert pandas Series/DataFrame to 1D NumPy array\nelif isinstance(row_id, np.ndarray) and row_id.ndim > 1:\n    row_id = row_id.ravel()  # Flatten NumPy array if it's multi-dimensional\n\nif isinstance(predictions, pd.DataFrame) or isinstance(predictions, pd.Series):\n    predictions = predictions.values.ravel()\nelif isinstance(predictions, np.ndarray) and predictions.ndim > 1:\n    predictions = predictions.ravel()\n\nsubmission_df = pd.DataFrame({'row_id': row_id, 'target': predictions})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:42:27.857806Z","iopub.execute_input":"2025-02-15T19:42:27.858230Z","iopub.status.idle":"2025-02-15T19:42:27.882421Z","shell.execute_reply.started":"2025-02-15T19:42:27.858195Z","shell.execute_reply":"2025-02-15T19:42:27.881238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Save to CSV\nsubmission_df.to_csv('sample.csv', index=False)  # index=False to avoid saving the DataFrame index\n\nprint(\"sample.csv created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:42:27.883499Z","iopub.execute_input":"2025-02-15T19:42:27.883873Z","iopub.status.idle":"2025-02-15T19:42:28.500647Z","shell.execute_reply.started":"2025-02-15T19:42:27.883821Z","shell.execute_reply":"2025-02-15T19:42:28.499658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"my_trained_model.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:44:17.023329Z","iopub.execute_input":"2025-02-15T19:44:17.023703Z","iopub.status.idle":"2025-02-15T19:44:17.044480Z","shell.execute_reply.started":"2025-02-15T19:44:17.023673Z","shell.execute_reply":"2025-02-15T19:44:17.043410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#num_predictions = len(df_test)\n#predictions_original_scale = np.random.rand(num_predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T19:42:28.819362Z","iopub.status.idle":"2025-02-15T19:42:28.819678Z","shell.execute_reply":"2025-02-15T19:42:28.819556Z"}},"outputs":[],"execution_count":null}]}