{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyOLgO74YgOWhe7FPCu01t9G"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"vk_TC_S5_sDc"},"outputs":[],"source":["from google.colab import drive\n","drive.mount('/content/drive')|"]},{"cell_type":"code","source":["import pandas as pd\n","import numpy as np\n","\n","from sklearn.preprocessing import LabelEncoder\n","from sklearn.model_selection import train_test_split\n","from sklearn.ensemble import RandomForestRegressor\n","from sklearn.metrics import mean_absolute_error, r2_score"],"metadata":{"id":"wBfUPyKr_20F"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["df = pd.read_csv(\"west_bengal_traffic_synthetic_data.csv\")\n","\n","print(\"Dataset Loaded:\")\n","print(df.head())"],"metadata":{"id":"qSeaC35zAKoN"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["df['timestamp'] = pd.to_datetime(df['timestamp'])\n","df['hour'] = df['timestamp'].dt.hour #Extracting the features\n","df['day'] = df['timestamp'].dt.day\n","df['month'] = df['timestamp'].dt.month\n","\n","df.drop('timestamp', axis=1, inplace=True)"],"metadata":{"id":"keksX9ngAi4U"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#Using Label encoder\n","label_encoders = {}\n","\n","categorical_cols = ['area', 'day_of_week', 'weather']\n","\n","for col in categorical_cols:\n","le = LabelEncoder()\n","df[col] = le.fit_transform(df[col])\n","label_encoders[col] = le"],"metadata":{"id":"7LduTC0VA1L3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["X = df.drop('future_vehicle_count', axis=1)\n","y = df['future_vehicle_count']"],"metadata":{"id":"UIvUnfoaBEXW"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#Splitting th data for train and test\n","split_index = int(len(df) * 0.8)\n","\n","X_train = X[]\n","X_test = X[split_index:]\n","\n","y_train = y[]\n","y_test = y[split_index:]"],"metadata":{"id":"HIH-Kt5mBGbQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#training the model\n","model = RandomForestRegressor(\n","n_estimators=200,\n","max_depth=10,\n","random_state=42\n",")\n","\n","model.fit(X_train, y_train)"],"metadata":{"id":"Xa3lz8VcBvd4"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#prediction on test data\n","preds = model.predict(X_test)"],"metadata":{"id":"bB2aUXRJB6tR"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#Checking the efficiency\n","mae = mean_absolute_error(y_test, preds)\n","r2 = r2_score(y_test, preds)\n","\n","print(\"\\nModel Performance:\")\n","print(\"MAE:\", mae)\n","print(\"R2 Score:\", r2)"],"metadata":{"id":"qzeakDQ2CBsi"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#Sample predict\n","sample = X_test.iloc[0].values.reshape(1, -1)\n","prediction = model.predict(sample)\n","\n","print(\"\\nSample Prediction:\")\n","print(\"Predicted Traffic:\", prediction[0])\n","print(\"Actual Traffic:\", y_test.iloc[0])"],"metadata":{"id":"0oG2GganCYVl"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#Graph and all...\n","import matplotlib.pyplot as plt\n","\n","importance = model.feature_importances_\n","features = X.columns\n","\n","plt.figure()\n","plt.barh(features, importance)\n","plt.title(\"Feature Importance\")\n","plt.show()"],"metadata":{"id":"oRwWvhHfCd0h"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["import joblib\n","\n","joblib.dump(model, \"traffic_model.pkl\")\n","\n","print(\"\\nModel saved successfully!\")"],"metadata":{"id":"eyTKegCmClhl"},"execution_count":null,"outputs":[]}]}