{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "qmBKOQx4783m" }, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.metrics import accuracy_score, classification_report" ] }, { "cell_type": "code", "source": [ "file_path = \"/content/electricity.csv\"\n", "data = pd.read_csv(file_path)" ], "metadata": { "id": "uPLyiFpw-Mq3" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "data.info()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oL-xXlvy-ZLl", "outputId": "dda52986-4081-490e-bbe7-f114103ef28a" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "RangeIndex: 45312 entries, 0 to 45311\n", "Data columns (total 9 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 date 45312 non-null float64\n", " 1 day 45312 non-null object \n", " 2 period 45312 non-null float64\n", " 3 nswprice 45312 non-null float64\n", " 4 nswdemand 45312 non-null float64\n", " 5 vicprice 45312 non-null float64\n", " 6 vicdemand 45312 non-null float64\n", " 7 transfer 45312 non-null float64\n", " 8 class 45312 non-null object \n", "dtypes: float64(7), object(2)\n", "memory usage: 3.1+ MB\n" ] } ] }, { "cell_type": "code", "source": [ "data.head(), data.tail()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-91jmy1g-lL9", "outputId": "885c6254-53cd-473c-a77e-5abfc6b43ddf" }, "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "( date day period nswprice nswdemand vicprice vicdemand transfer \\\n", " 0 0.0 b'2' 0.000000 0.056443 0.439155 0.003467 0.422915 0.414912 \n", " 1 0.0 b'2' 0.021277 0.051699 0.415055 0.003467 0.422915 0.414912 \n", " 2 0.0 b'2' 0.042553 0.051489 0.385004 0.003467 0.422915 0.414912 \n", " 3 0.0 b'2' 0.063830 0.045485 0.314639 0.003467 0.422915 0.414912 \n", " 4 0.0 b'2' 0.085106 0.042482 0.251116 0.003467 0.422915 0.414912 \n", " \n", " class \n", " 0 b'UP' \n", " 1 b'UP' \n", " 2 b'UP' \n", " 3 b'UP' \n", " 4 b'DOWN' ,\n", " date day period nswprice nswdemand vicprice vicdemand \\\n", " 45307 0.9158 b'7' 0.914894 0.044224 0.340672 0.003033 0.255049 \n", " 45308 0.9158 b'7' 0.936170 0.044884 0.355549 0.003072 0.241326 \n", " 45309 0.9158 b'7' 0.957447 0.043593 0.340970 0.002983 0.247799 \n", " 45310 0.9158 b'7' 0.978723 0.066651 0.329366 0.004630 0.345417 \n", " 45311 0.9158 b'7' 1.000000 0.050679 0.288753 0.003542 0.355256 \n", " \n", " transfer class \n", " 45307 0.405263 b'DOWN' \n", " 45308 0.420614 b'DOWN' \n", " 45309 0.362281 b'DOWN' \n", " 45310 0.206579 b'UP' \n", " 45311 0.231140 b'DOWN' )" ] }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "source": [ "data = data.drop(columns=['date'])" ], "metadata": { "id": "T1FZym90-oI8" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "data = pd.get_dummies(data, columns=['day'], prefix='day')" ], "metadata": { "id": "4J2DpzhT-tBC" }, "execution_count": 6, "outputs": [] }, { "cell_type": "code", "source": [ "X = data.drop(columns=['class'])\n", "y = data['class']" ], "metadata": { "id": "NrgeoBNd-xLy" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "from sklearn.preprocessing import LabelEncoder\n", "le = LabelEncoder()\n", "y_encoded = le.fit_transform(y)" ], "metadata": { "id": "LEfwdL5Z-1ki" }, "execution_count": 8, "outputs": [] }, { "cell_type": "code", "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded)" ], "metadata": { "id": "n1rbHlbz_Isl" }, "execution_count": 9, "outputs": [] }, { "cell_type": "code", "source": [ "model = RandomForestClassifier(n_estimators=100, random_state=42)\n", "model.fit(X_train, y_train)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OwUeKsVD_bPM", "outputId": "38350735-dfc7-496f-ea69-00ee207bade7" }, "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "RandomForestClassifier(random_state=42)" ] }, "metadata": {}, "execution_count": 10 } ] }, { "cell_type": "code", "source": [ "y_pred = model.predict(X_test)" ], "metadata": { "id": "2SLxRDDd_iFH" }, "execution_count": 11, "outputs": [] }, { "cell_type": "code", "source": [ "accuracy = accuracy_score(y_test, y_pred)\n", "print(f\"Model Accuracy: {accuracy:.2f}\")\n", "\n", "print(\"Classification Report:\")\n", "print(classification_report(y_test, y_pred, target_names=le.classes_))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MMUwJ1x__kw6", "outputId": "038415e4-74ee-49bb-bea9-2fc5649bcb57" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model Accuracy: 0.85\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " b'DOWN' 0.86 0.89 0.88 5215\n", " b'UP' 0.85 0.80 0.82 3848\n", "\n", " accuracy 0.85 9063\n", " macro avg 0.85 0.85 0.85 9063\n", "weighted avg 0.85 0.85 0.85 9063\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "accuracy = accuracy_score(y_test, y_pred)\n", "print(f\"Model Accuracy on Test Set: {accuracy:.2f}\")\n", "print(\"Classification Report:\")\n", "print(classification_report(y_test, y_pred, target_names=le.classes_))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "H414Ttnf_1zh", "outputId": "1958ff35-21cb-4367-f99c-e9ff752f99ef" }, "execution_count": 13, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model Accuracy on Test Set: 0.85\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " b'DOWN' 0.86 0.89 0.88 5215\n", " b'UP' 0.85 0.80 0.82 3848\n", "\n", " accuracy 0.85 9063\n", " macro avg 0.85 0.85 0.85 9063\n", "weighted avg 0.85 0.85 0.85 9063\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "from xgboost import XGBClassifier\n", "from sklearn.linear_model import LogisticRegression" ], "metadata": { "id": "5hpdaFd8AG_I" }, "execution_count": 14, "outputs": [] }, { "cell_type": "code", "source": [ "!pip install scikit-learn==1.0.2\n", "!pip install xgboost --upgrade\n", "\n", "model = XGBClassifier(n_estimators=500, random_state=42)\n", "model.fit(X_train, y_train)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9RyBCF_sAMti", "outputId": "072aa23a-7b25-4b6a-e00b-4b053fe16f32" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: scikit-learn==1.0.2 in /usr/local/lib/python3.10/dist-packages (1.0.2)\n", "Requirement already satisfied: numpy>=1.14.6 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.26.4)\n", "Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.13.1)\n", "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.4.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (3.5.0)\n", "Requirement already satisfied: xgboost in /usr/local/lib/python3.10/dist-packages (2.1.3)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from xgboost) (1.26.4)\n", "Requirement already satisfied: nvidia-nccl-cu12 in /usr/local/lib/python3.10/dist-packages (from xgboost) (2.23.4)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from xgboost) (1.13.1)\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "XGBClassifier(base_score=None, booster=None, callbacks=None,\n", " colsample_bylevel=None, colsample_bynode=None,\n", " colsample_bytree=None, device=None, early_stopping_rounds=None,\n", " enable_categorical=False, eval_metric=None, feature_types=None,\n", " gamma=None, grow_policy=None, importance_type=None,\n", " interaction_constraints=None, learning_rate=None, max_bin=None,\n", " max_cat_threshold=None, max_cat_to_onehot=None,\n", " max_delta_step=None, max_depth=None, max_leaves=None,\n", " min_child_weight=None, missing=nan, monotone_constraints=None,\n", " multi_strategy=None, n_estimators=500, n_jobs=None,\n", " num_parallel_tree=None, random_state=42, ...)" ] }, "metadata": {}, "execution_count": 15 } ] }, { "cell_type": "code", "source": [ "y_pred = model.predict(X_test)\n", "\n", "accuracy = accuracy_score(y_test, y_pred)\n", "print(f\"Model Accuracy: {accuracy:.2f}\")\n", "\n", "print(\"Classification Report:\")\n", "print(classification_report(y_test, y_pred, target_names=le.classes_))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "80phZNiEAVZ5", "outputId": "65e22213-91f5-4c27-c1a0-b4eaf04db020" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model Accuracy: 0.84\n", "Classification Report:\n", " precision recall f1-score support\n", "\n", " b'DOWN' 0.86 0.88 0.87 5215\n", " b'UP' 0.83 0.80 0.81 3848\n", "\n", " accuracy 0.84 9063\n", " macro avg 0.84 0.84 0.84 9063\n", "weighted avg 0.84 0.84 0.84 9063\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "model = LogisticRegression(penalty='l2', C=1.0, solver='liblinear', random_state=42) # Changed penalty to 'l2'\n", "model.fit(X_train, y_train)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7TazBj_1AlgB", "outputId": "bedb4784-c6fb-4dab-9d72-a041073daa2c" }, "execution_count": 17, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "LogisticRegression(random_state=42, solver='liblinear')" ] }, "metadata": {}, "execution_count": 17 } ] }, { "cell_type": "code", "source": [ "y_pred = model.predict(X_test)\n", "\n", "accuracy = accuracy_score(y_test, y_pred)\n", "print(f\"Model Accuracy: {accuracy:.2f}\")\n", "\n", "print(\"Classificatoin Report:\")\n", "print(classification_report(y_test, y_pred, target_names=le.classes_))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fA57l0S0A4hB", "outputId": "1814a73e-5f7d-44ef-e5d6-5d0763f5f2a9" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Model Accuracy: 0.76\n", "Classificatoin Report:\n", " precision recall f1-score support\n", "\n", " b'DOWN' 0.75 0.87 0.81 5215\n", " b'UP' 0.78 0.61 0.68 3848\n", "\n", " accuracy 0.76 9063\n", " macro avg 0.76 0.74 0.75 9063\n", "weighted avg 0.76 0.76 0.75 9063\n", "\n" ] } ] } ] }