{"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":41880,"databundleVersionId":5677426,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom numpy.fft import fft\nimport numpy as np\nimport pandas as pd\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-12T10:52:43.821370Z","iopub.execute_input":"2024-01-12T10:52:43.822527Z","iopub.status.idle":"2024-01-12T10:52:45.448863Z","shell.execute_reply.started":"2024-01-12T10:52:43.822456Z","shell.execute_reply":"2024-01-12T10:52:45.447597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_ROOT_DEFOG = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/'\ndefog = pd.DataFrame()\nfor root, dirs, files in os.walk(DATA_ROOT_DEFOG):\n    for name in files:       \n        f = os.path.join(root, name)\n        df_list= pd.read_csv(f)\n        words = name.split('.')[0]\n        df_list['file']= name.split('.')[0]\n        defog = pd.concat([defog, df_list], axis=0)\n        \nkeys = np.arange(len(defog))\ndefog = defog.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=True)\ndefog","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:52:47.565118Z","iopub.execute_input":"2024-01-12T10:52:47.565704Z","iopub.status.idle":"2024-01-12T10:53:43.857179Z","shell.execute_reply.started":"2024-01-12T10:52:47.565665Z","shell.execute_reply":"2024-01-12T10:53:43.855944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 1: Apply FFT\n# Compute the FFT and use the absolute values (magnitudes)\ndefog['FFT_AccV'] = np.abs(np.fft.fft(defog['AccV']))\ndefog['FFT_AccML'] = np.abs(np.fft.fft(defog['AccML']))\ndefog['FFT_AccAP'] = np.abs(np.fft.fft(defog['AccAP']))\n\n# Create the fft_data DataFrame\nfft_data = pd.DataFrame({\n    'FFT_AccV': defog['FFT_AccV'],\n    'FFT_AccML': defog['FFT_AccML'],\n    'FFT_AccAP': defog['FFT_AccAP']\n})\n\n# Step 2: Perform PCA\npca = PCA(n_components=0.95)  # Adjust n_components as needed\nprincipal_components = pca.fit_transform(fft_data)\n\n# Step 3: Prepare Data for Model\n# Assuming you want to predict 'StartHesitation' as your target variable\nX = principal_components\ny = defog['StartHesitation']  # Or any other event type you wish to classify\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Step 4: Train Classification Model\nmodel = RandomForestClassifier(random_state=42)\nmodel.fit(X_train, y_train)\n\n# Predictions and Evaluation\ny_pred = model.predict(X_val)\nprint(classification_report(y_val, y_pred))\n\n# Add additional steps for hyperparameter tuning, cross-validation, etc., as needed.\n","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:42:26.803515Z","iopub.execute_input":"2024-01-12T10:42:26.803860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 1: Apply FFT\n# Compute the FFT and use the absolute values (magnitudes)\ndefog['FFT_AccV'] = np.abs(np.fft.fft(defog['AccV']))\ndefog['FFT_AccML'] = np.abs(np.fft.fft(defog['AccML']))\ndefog['FFT_AccAP'] = np.abs(np.fft.fft(defog['AccAP']))","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:54:02.662290Z","iopub.execute_input":"2024-01-12T10:54:02.662750Z","iopub.status.idle":"2024-01-12T10:54:50.983058Z","shell.execute_reply.started":"2024-01-12T10:54:02.662712Z","shell.execute_reply":"2024-01-12T10:54:50.981677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the fft_data DataFrame\nfft_data = pd.DataFrame({\n    'FFT_AccV': defog['FFT_AccV'],\n    'FFT_AccML': defog['FFT_AccML'],\n    'FFT_AccAP': defog['FFT_AccAP']\n})","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:55:10.405822Z","iopub.execute_input":"2024-01-12T10:55:10.406249Z","iopub.status.idle":"2024-01-12T10:55:10.533685Z","shell.execute_reply.started":"2024-01-12T10:55:10.406216Z","shell.execute_reply":"2024-01-12T10:55:10.532469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 2: Perform PCA\npca = PCA(n_components=0.95)  # Adjust n_components as needed\nprincipal_components = pca.fit_transform(fft_data)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:55:23.768413Z","iopub.execute_input":"2024-01-12T10:55:23.768847Z","iopub.status.idle":"2024-01-12T10:55:25.110735Z","shell.execute_reply.started":"2024-01-12T10:55:23.768815Z","shell.execute_reply":"2024-01-12T10:55:25.109676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3: Prepare Data for Model\n# Assuming you want to predict 'StartHesitation' as your target variable\nX = principal_components\ny = defog['StartHesitation']  # Or any other event type you wish to classify\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:55:50.289480Z","iopub.execute_input":"2024-01-12T10:55:50.290715Z","iopub.status.idle":"2024-01-12T10:55:52.924992Z","shell.execute_reply.started":"2024-01-12T10:55:50.290661Z","shell.execute_reply":"2024-01-12T10:55:52.923640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 4: Train Classification Model\nmodel = RandomForestClassifier(random_state=42)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T10:56:06.483428Z","iopub.execute_input":"2024-01-12T10:56:06.483879Z","iopub.status.idle":"2024-01-12T12:11:10.248789Z","shell.execute_reply.started":"2024-01-12T10:56:06.483844Z","shell.execute_reply":"2024-01-12T12:11:10.247912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Multiclass Prediction\n# Step 1: Apply FFT\ndefog['FFT_AccV'] = np.abs(np.fft.fft(defog['AccV']))\ndefog['FFT_AccML'] = np.abs(np.fft.fft(defog['AccML']))\ndefog['FFT_AccAP'] = np.abs(np.fft.fft(defog['AccAP']))\n\n# Create the fft_data DataFrame\nfft_data = pd.DataFrame({\n    'FFT_AccV': defog['FFT_AccV'],\n    'FFT_AccML': defog['FFT_AccML'],\n    'FFT_AccAP': defog['FFT_AccAP']\n})\n\n# Step 2: Perform PCA\npca = PCA(n_components=0.95)  # Adjust n_components as needed\nprincipal_components = pca.fit_transform(fft_data)\n\n# Step 3: Prepare Data for Model\n# Combine the three target variables into a single multi-class target variable\ny = defog[['StartHesitation', 'Turn', 'Walking']].idxmax(axis=1)\n\nX = principal_components\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Step 4: Train Multi-Class Classification Model\nmodel = RandomForestClassifier(random_state=42)\nmodel.fit(X_train, y_train)\n\n# Predictions and Evaluation\ny_pred = model.predict(X_val)\nprint(classification_report(y_val, y_pred))\n\n# Add additional steps for hyperparameter tuning, cross-validation, etc., as needed.\n","metadata":{},"execution_count":null,"outputs":[]}]}