{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.16"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":91048,"databundleVersionId":10710872,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":4629.335961,"end_time":"2025-01-21T18:46:22.955789","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-01-21T17:29:13.619828","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"d69e3f7e","cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\n\n# importing the file containing the features for each video.\n\ntrain_df = pd.read_csv('/kaggle/input/beginners-hypothesis-25/BH25/Training_Data/train.csv')\ntrain_df = train_df[:5000]\ntrain_df\n\n\n''' with 10000 videos , it was taking a lot of time to train , so started with 500 videos then 1000 videos , 3000 videos then eventualy 5000 video .\n I was not able to submit the prediction of the model using 10000 videos . so the score on the leaderboard is due to a model trained with 5000 videos only .\n","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:29:16.560109Z","iopub.status.busy":"2025-01-21T17:29:16.559844Z","iopub.status.idle":"2025-01-21T17:29:18.223515Z","shell.execute_reply":"2025-01-21T17:29:18.221991Z"},"papermill":{"duration":1.67039,"end_time":"2025-01-21T17:29:18.224452","exception":false,"start_time":"2025-01-21T17:29:16.554062","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"b6212957","cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\n\n# Define the directory containing the videos\nvideo_dir = \"/kaggle/input/beginners-hypothesis-25/BH25/Training_Data/Train_Videos\"\n\n# List all video files in the directory\nvideo_files = sorted([f for f in os.listdir(video_dir) if f.endswith('.mp4')], key=lambda x: int(x.split('.')[0]))\n\n\nvideo_files = video_files[:5000]\n\n# Process each video file\nvideo_features = []  # To store processed video data\n\ndef process_video(video_path):\n    cap = cv2.VideoCapture(video_path)\n    frames = []\n    while True:\n        ret, frame = cap.read()\n        if not ret:\n            break\n        # Resizing frame to 64x64\n        frame = cv2.resize(frame, (64, 64))\n        frames.append(frame)\n    cap.release()\n    return np.array(frames).flatten()  # Flatten the frames , originally it was of dimension 20*64*64*3\n\nfor video_file in video_files:\n    video_path = os.path.join(video_dir, video_file)\n    features = process_video(video_path)\n    video_features.append(features)\n\n# Convert video features to a NumPy array\nvideo_features = np.array(video_features)\nvideo_features\n","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:29:18.232717Z","iopub.status.busy":"2025-01-21T17:29:18.232467Z","iopub.status.idle":"2025-01-21T17:30:20.868852Z","shell.execute_reply":"2025-01-21T17:30:20.867591Z"},"papermill":{"duration":62.644251,"end_time":"2025-01-21T17:30:20.872018","exception":false,"start_time":"2025-01-21T17:29:18.227767","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"d2b46664","cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder # using label encoders to convert categorial variables to numeric data which the model can easily evaluate. \n\ndef encode_categorical_columns(df, columns): # defining a function to encode a categorial variable to a numeric value\n    label_encoders = {}\n    for col in columns:\n        le = LabelEncoder()\n        df[col] = le.fit_transform(df[col])\n        label_encoders[col] = le\n    return df, label_encoders\n\ncategorical_columns = ['element', 'motion', 'power']\ntrain_df, label_encoders = encode_categorical_columns(train_df, categorical_columns) \ntrain_df","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:30:20.880176Z","iopub.status.busy":"2025-01-21T17:30:20.879944Z","iopub.status.idle":"2025-01-21T17:30:23.799526Z","shell.execute_reply":"2025-01-21T17:30:23.798532Z"},"papermill":{"duration":2.925839,"end_time":"2025-01-21T17:30:23.801059","exception":false,"start_time":"2025-01-21T17:30:20.875220","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"b9b46dab","cell_type":"code","source":"from sklearn.model_selection import train_test_split #spliting the data to make samples separately for traing and testing . Evaluating with new data gives a better realisation of how the model is performing.\n\n\ny_speed = train_df[['speed']]\ny_summary = train_df['video_summary'].apply(lambda x: eval(x))  # Convert string tuples to actual tuples\n\ny_categorical = train_df[categorical_columns]\n\n\nX_train_speed, X_val_speed, y_train_speed, y_val_speed = train_test_split(video_features, y_speed, test_size=0.2, random_state=42)\nX_train_cat, X_val_cat, y_train_cat, y_val_cat = train_test_split(video_features, y_categorical, test_size=0.2, random_state=42)\nX_train_summary, X_val_summary, y_train_summary, y_val_summary = train_test_split(video_features, y_summary, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:30:23.810051Z","iopub.status.busy":"2025-01-21T17:30:23.809741Z","iopub.status.idle":"2025-01-21T17:30:25.240819Z","shell.execute_reply":"2025-01-21T17:30:25.239621Z"},"papermill":{"duration":1.438166,"end_time":"2025-01-21T17:30:25.242723","exception":false,"start_time":"2025-01-21T17:30:23.804557","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"aaaca341","cell_type":"code","source":"from sklearn.svm import SVR #using support vector regression to compensate for any possible non linearity in data .\n\n\nmodel_speed = SVR(kernel='rbf', C=1.0, epsilon=0.1)  # Adjusting the hyperparameters to get better score\n# kernel :- i chose 'rbf(radial basis function)' kernel because there can be a possibility of data having some non-linear character.\n# c (regularisation parameter) :- i set this parameter to a low setting to prevent overfitting of the model and to prevent bad score due to noise in the data and obtain a smooth relation.\n# epsilon :- i set it to a high value of 0.1 , beacause i wanted to neglect any errors that might have arose due to some noise in the data.\n\nmodel_speed.fit(X_train_speed, y_train_speed)\n# i was not able to upload the csv file resulting from svr model for speed due to running out gpu limit . the score on the leaderboard is of linear regression only .  ","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:30:25.252023Z","iopub.status.busy":"2025-01-21T17:30:25.251687Z","iopub.status.idle":"2025-01-21T17:36:01.288982Z","shell.execute_reply":"2025-01-21T17:36:01.287718Z"},"papermill":{"duration":336.048562,"end_time":"2025-01-21T17:36:01.295157","exception":false,"start_time":"2025-01-21T17:30:25.246595","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"2c3fbe35","cell_type":"code","source":"model_speed.score(X_val_speed , y_val_speed) \n#evaluating the score for model_speed , with linear regression it was less than 0.65 , with support vector regression non linear data can be evaluated easily , with svr was able to get scores above 0.75.","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:36:01.305134Z","iopub.status.busy":"2025-01-21T17:36:01.304710Z","iopub.status.idle":"2025-01-21T17:50:20.759417Z","shell.execute_reply":"2025-01-21T17:50:20.757465Z"},"papermill":{"duration":859.467795,"end_time":"2025-01-21T17:50:20.767243","exception":false,"start_time":"2025-01-21T17:36:01.299448","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"f19091f2","cell_type":"code","source":"# i could also use svr here , but it takes a lot of time to train with svr , so i did not change it here .\nfrom sklearn.linear_model import LinearRegression\nx_values, y_values = zip(*y_train_summary)\n\nmodel_summary_x = LinearRegression()\nmodel_summary_x.fit(X_train_summary, x_values)\n\nmodel_summary_y = LinearRegression()\nmodel_summary_y.fit(X_train_summary, y_values)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:50:20.785056Z","iopub.status.busy":"2025-01-21T17:50:20.784621Z","iopub.status.idle":"2025-01-21T17:56:09.388600Z","shell.execute_reply":"2025-01-21T17:56:09.387030Z"},"papermill":{"duration":348.623093,"end_time":"2025-01-21T17:56:09.398235","exception":false,"start_time":"2025-01-21T17:50:20.775142","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"f33db367","cell_type":"code","source":"","metadata":{"papermill":{"duration":0.008987,"end_time":"2025-01-21T17:56:09.416187","exception":false,"start_time":"2025-01-21T17:56:09.407200","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"efd57131","cell_type":"code","source":"#testing several classification methods , linear regression wont give good results because it is classification problem, random forest gave best results.\n#random forest classifier is generally better than svc because it can handle higher dimensional data more easily than svc and decission tree .  \n\n\n'''from sklearn.svm import SVC\n\nmodels_categorical = {}\nfor col in categorical_columns:\n    model = SVC()\n    model.fit(X_train_cat, y_train_cat[col])\n    models_categorical[col] = model'''\nfrom sklearn.ensemble import RandomForestClassifier\nmodels_categorical = {}\nfor col in categorical_columns:   # the model for each feature gets appended in the dictionary in each iteration\n    model = RandomForestClassifier()\n    model.fit(X_train_cat, y_train_cat[col])\n    models_categorical[col] = model\n'''from sklearn import tree\n\nmodels_categorical = {}\nfor col in categorical_columns:\n    model = tree.DecisionTreeClassifier()\n    model.fit(X_train_cat, y_train_cat[col])\n    models_categorical[col] = model'''\n'''from sklearn.linear_model import LogisticRegression\n\n","metadata":{"execution":{"iopub.execute_input":"2025-01-21T17:56:09.435946Z","iopub.status.busy":"2025-01-21T17:56:09.435562Z","iopub.status.idle":"2025-01-21T18:01:38.311986Z","shell.execute_reply":"2025-01-21T18:01:38.311111Z"},"papermill":{"duration":328.892878,"end_time":"2025-01-21T18:01:38.317860","exception":false,"start_time":"2025-01-21T17:56:09.424982","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"5e410bd3","cell_type":"code","source":"models_categorical['element'].score(X_val_cat , y_val_cat.element)\nmodels_categorical['motion'].score(X_val_cat , y_val_cat.motion)\nmodels_categorical['power'].score(X_val_cat , y_val_cat.power)\n# checking the score for categorial model , 0.803 for element , above 0.4 for power and 1.0 for motion","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:01:38.329951Z","iopub.status.busy":"2025-01-21T18:01:38.329524Z","iopub.status.idle":"2025-01-21T18:01:38.709374Z","shell.execute_reply":"2025-01-21T18:01:38.708140Z"},"papermill":{"duration":0.387403,"end_time":"2025-01-21T18:01:38.710924","exception":false,"start_time":"2025-01-21T18:01:38.323521","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"73dac0f0","cell_type":"code","source":"import os\n\ntest_folder = '/kaggle/input/beginners-hypothesis-25/BH25/Testing_Data'\n\ntest_features = []\nvideo_ids = []\n# importing test videos and making the array named test_features which stores the data of each video\n\nfor video_id in sorted(\n    [f for f in os.listdir(test_folder) if f.endswith(\".mp4\")], key=lambda x: int(x.split('.')[0])):\n    video_path = os.path.join(test_folder, video_id)\n    video_ids.append(video_id.split('.')[0]) \n    test_features.append(process_video(video_path))\n\ntest_features = np.array(test_features)  # Shape: (num_test_samples, 20*64*64*3)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:01:38.722251Z","iopub.status.busy":"2025-01-21T18:01:38.722024Z","iopub.status.idle":"2025-01-21T18:02:13.361789Z","shell.execute_reply":"2025-01-21T18:02:13.360511Z"},"papermill":{"duration":34.647959,"end_time":"2025-01-21T18:02:13.363847","exception":false,"start_time":"2025-01-21T18:01:38.715888","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"367e49df","cell_type":"code","source":"# predicting the values for speed and summary using test data \ny_test_speed = model_speed.predict(test_features)\ny_test_summary_x = model_summary_x.predict(test_features)\ny_test_summary_y = model_summary_y.predict(test_features)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:02:13.375941Z","iopub.status.busy":"2025-01-21T18:02:13.375620Z","iopub.status.idle":"2025-01-21T18:46:19.044659Z","shell.execute_reply":"2025-01-21T18:46:19.042790Z"},"papermill":{"duration":2645.677345,"end_time":"2025-01-21T18:46:19.046245","exception":false,"start_time":"2025-01-21T18:02:13.368900","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"0257ce95","cell_type":"code","source":"#  predicting the values for categorical features\ny_categorical = {}\n\nfor col in categorical_columns:\n    y_categorical[col] = models_categorical[col].predict(test_features)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:46:19.059664Z","iopub.status.busy":"2025-01-21T18:46:19.059285Z","iopub.status.idle":"2025-01-21T18:46:22.422826Z","shell.execute_reply":"2025-01-21T18:46:22.421727Z"},"papermill":{"duration":3.372537,"end_time":"2025-01-21T18:46:22.424959","exception":false,"start_time":"2025-01-21T18:46:19.052422","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"04881077","cell_type":"code","source":"# converting the labels for the categorial variables to their actual names \ndecoded_categorical = {}\nfor col, predictions in y_categorical.items():\n    rounded_predictions = np.rint(predictions).astype(int)\n    valid_classes = range(len(label_encoders[col].classes_))\n    rounded_predictions = np.clip(rounded_predictions, min(valid_classes), max(valid_classes))\n    decoded_categorical[col] = label_encoders[col].inverse_transform(rounded_predictions)","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:46:22.437138Z","iopub.status.busy":"2025-01-21T18:46:22.436866Z","iopub.status.idle":"2025-01-21T18:46:22.443238Z","shell.execute_reply":"2025-01-21T18:46:22.442112Z"},"papermill":{"duration":0.014387,"end_time":"2025-01-21T18:46:22.444935","exception":false,"start_time":"2025-01-21T18:46:22.430548","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"a72be75e","cell_type":"code","source":"speed_predictions = y_test_speed.flatten()  # Ensure the shape is (num_samples,)\nsummary_x_predictions = y_test_summary_x.flatten()\nsummary_y_predictions = y_test_summary_y.flatten()","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:46:22.456296Z","iopub.status.busy":"2025-01-21T18:46:22.456074Z","iopub.status.idle":"2025-01-21T18:46:22.460288Z","shell.execute_reply":"2025-01-21T18:46:22.459079Z"},"papermill":{"duration":0.011354,"end_time":"2025-01-21T18:46:22.461545","exception":false,"start_time":"2025-01-21T18:46:22.450191","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"b937aadf","cell_type":"code","source":"# constructing the dataframe to be exported to csv file.\noutput_df = pd.DataFrame({\n    'video_id': video_ids,                      \n    'element': decoded_categorical['element'],  \n    'motion': decoded_categorical['motion'],\n    'power': decoded_categorical['power'],\n    'speed': speed_predictions,               \n    'video_summary': list(zip(summary_x_predictions, summary_y_predictions)) \n})","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:46:22.473044Z","iopub.status.busy":"2025-01-21T18:46:22.472840Z","iopub.status.idle":"2025-01-21T18:46:22.481037Z","shell.execute_reply":"2025-01-21T18:46:22.479525Z"},"papermill":{"duration":0.015707,"end_time":"2025-01-21T18:46:22.481983","exception":false,"start_time":"2025-01-21T18:46:22.466276","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"fc83422f","cell_type":"code","source":"output_df.to_csv(\"submission_10000(only for speed and summary)_12:20_am.csv\", index=False) #exporting the csv file ","metadata":{"execution":{"iopub.execute_input":"2025-01-21T18:46:22.493408Z","iopub.status.busy":"2025-01-21T18:46:22.493199Z","iopub.status.idle":"2025-01-21T18:46:22.521153Z","shell.execute_reply":"2025-01-21T18:46:22.520005Z"},"papermill":{"duration":0.03573,"end_time":"2025-01-21T18:46:22.522598","exception":false,"start_time":"2025-01-21T18:46:22.486868","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"579625d4","cell_type":"code","source":"","metadata":{"papermill":{"duration":0.005353,"end_time":"2025-01-21T18:46:22.532708","exception":false,"start_time":"2025-01-21T18:46:22.527355","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}