{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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","execution":{"iopub.status.busy":"2022-05-14T12:12:51.335896Z","iopub.execute_input":"2022-05-14T12:12:51.336293Z","iopub.status.idle":"2022-05-14T12:12:51.346729Z","shell.execute_reply.started":"2022-05-14T12:12:51.336247Z","shell.execute_reply":"2022-05-14T12:12:51.345600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/energy-anomaly-detection/train.csv')\ndata.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:51.349063Z","iopub.execute_input":"2022-05-14T12:12:51.349307Z","iopub.status.idle":"2022-05-14T12:12:52.917089Z","shell.execute_reply.started":"2022-05-14T12:12:51.349277Z","shell.execute_reply":"2022-05-14T12:12:52.916369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:52.918252Z","iopub.execute_input":"2022-05-14T12:12:52.918971Z","iopub.status.idle":"2022-05-14T12:12:52.944694Z","shell.execute_reply.started":"2022-05-14T12:12:52.918930Z","shell.execute_reply":"2022-05-14T12:12:52.943329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking for null values \nnull = pd.DataFrame({'Null Values' : data.isna().sum(), 'Percentage Null Values' : (data.isna().sum()) / (data.shape[0]) * (100)})\nnull.sort_values(by=['Null Values'],ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:52.947689Z","iopub.execute_input":"2022-05-14T12:12:52.948046Z","iopub.status.idle":"2022-05-14T12:12:53.371346Z","shell.execute_reply.started":"2022-05-14T12:12:52.947999Z","shell.execute_reply":"2022-05-14T12:12:53.370364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.dropna()\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:53.372863Z","iopub.execute_input":"2022-05-14T12:12:53.373703Z","iopub.status.idle":"2022-05-14T12:12:53.698328Z","shell.execute_reply.started":"2022-05-14T12:12:53.373658Z","shell.execute_reply":"2022-05-14T12:12:53.697206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['timestamp'] = pd.to_datetime(data['timestamp'])\ndata.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:53.700251Z","iopub.execute_input":"2022-05-14T12:12:53.700499Z","iopub.status.idle":"2022-05-14T12:12:54.008384Z","shell.execute_reply.started":"2022-05-14T12:12:53.700470Z","shell.execute_reply":"2022-05-14T12:12:54.007707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['year'] = data['timestamp'].dt.year\ndata['month'] = data['timestamp'].dt.month\ndata['day'] = data['timestamp'].dt.day\ndata['hour'] = data['timestamp'].dt.hour\ndata['minute'] = data['timestamp'].dt.minute\ndata['second'] = data['timestamp'].dt.second\ndata = data.drop(['timestamp'], axis=1)\ndata.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:54.009468Z","iopub.execute_input":"2022-05-14T12:12:54.010369Z","iopub.status.idle":"2022-05-14T12:12:55.117969Z","shell.execute_reply.started":"2022-05-14T12:12:54.010321Z","shell.execute_reply":"2022-05-14T12:12:55.117079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['meter_reading'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:55.119334Z","iopub.execute_input":"2022-05-14T12:12:55.119948Z","iopub.status.idle":"2022-05-14T12:12:55.745720Z","shell.execute_reply.started":"2022-05-14T12:12:55.119905Z","shell.execute_reply":"2022-05-14T12:12:55.744881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['anomaly'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:55.747251Z","iopub.execute_input":"2022-05-14T12:12:55.747734Z","iopub.status.idle":"2022-05-14T12:12:56.454892Z","shell.execute_reply.started":"2022-05-14T12:12:55.747688Z","shell.execute_reply":"2022-05-14T12:12:56.454001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(['anomaly'], axis=1)\ny = data['anomaly']","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:56.457443Z","iopub.execute_input":"2022-05-14T12:12:56.457694Z","iopub.status.idle":"2022-05-14T12:12:56.546941Z","shell.execute_reply.started":"2022-05-14T12:12:56.457663Z","shell.execute_reply":"2022-05-14T12:12:56.546194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:56.548096Z","iopub.execute_input":"2022-05-14T12:12:56.548721Z","iopub.status.idle":"2022-05-14T12:12:58.242041Z","shell.execute_reply.started":"2022-05-14T12:12:56.548685Z","shell.execute_reply":"2022-05-14T12:12:58.241125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import classification_report\nlr = LogisticRegression(solver='newton-cg',)\nlr.fit(X_train, y_train)\nprint(lr.score(X_train,y_train))\ny_pred_lr = lr.predict(X_test)\nclf_report = classification_report(y_test, y_pred_lr)\nprint(f\"Classification Report : \\n{clf_report}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:12:58.243367Z","iopub.execute_input":"2022-05-14T12:12:58.243632Z","iopub.status.idle":"2022-05-14T12:13:15.693621Z","shell.execute_reply.started":"2022-05-14T12:12:58.243600Z","shell.execute_reply":"2022-05-14T12:13:15.692742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier()\nknn.fit(X_train, y_train)\n\ny_pred_knn = knn.predict(X_test)\n\nclf_report = classification_report(y_test, y_pred_knn)\nprint(f\"Classification Report : \\n{clf_report}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:13:27.679620Z","iopub.execute_input":"2022-05-14T12:13:27.679930Z","iopub.status.idle":"2022-05-14T12:14:03.270787Z","shell.execute_reply.started":"2022-05-14T12:13:27.679897Z","shell.execute_reply":"2022-05-14T12:14:03.269760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndtc = DecisionTreeClassifier()\ndtc.fit(X_train, y_train)\n\ny_pred_dtc = dtc.predict(X_test)\nclf_report = classification_report(y_test, y_pred_dtc)\n\nprint(f\"Classification Report : \\n{clf_report}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:14:18.046740Z","iopub.execute_input":"2022-05-14T12:14:18.047280Z","iopub.status.idle":"2022-05-14T12:14:25.690667Z","shell.execute_reply.started":"2022-05-14T12:14:18.047238Z","shell.execute_reply":"2022-05-14T12:14:25.689743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrd_clf = RandomForestClassifier()\nrd_clf.fit(X_train, y_train)\n\ny_pred_rd_clf = rd_clf.predict(X_test)\nclf_report = classification_report(y_test, y_pred_rd_clf)\n\nprint(f\"Classification Report : \\n{clf_report}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:14:25.692325Z","iopub.execute_input":"2022-05-14T12:14:25.692581Z","iopub.status.idle":"2022-05-14T12:17:37.756267Z","shell.execute_reply.started":"2022-05-14T12:14:25.692549Z","shell.execute_reply":"2022-05-14T12:17:37.755065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/energy-anomaly-detection/test.csv')\ntest_data.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:17:55.278749Z","iopub.execute_input":"2022-05-14T12:17:55.279061Z","iopub.status.idle":"2022-05-14T12:17:56.337416Z","shell.execute_reply.started":"2022-05-14T12:17:55.279029Z","shell.execute_reply":"2022-05-14T12:17:56.336544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['timestamp'] = pd.to_datetime(test_data['timestamp'])\ntest_data['year'] = test_data['timestamp'].dt.year\ntest_data['month'] = test_data['timestamp'].dt.month\ntest_data['day'] = test_data['timestamp'].dt.day\ntest_data['hour'] = test_data['timestamp'].dt.hour\ntest_data['minute'] = test_data['timestamp'].dt.minute\ntest_data['second'] = test_data['timestamp'].dt.second\ntest_data = test_data.drop(['timestamp'], axis=1)\ntest_data.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:17:58.540394Z","iopub.execute_input":"2022-05-14T12:17:58.540890Z","iopub.status.idle":"2022-05-14T12:17:59.991276Z","shell.execute_reply.started":"2022-05-14T12:17:58.540841Z","shell.execute_reply":"2022-05-14T12:17:59.990002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:19:37.544821Z","iopub.execute_input":"2022-05-14T12:19:37.545345Z","iopub.status.idle":"2022-05-14T12:19:37.670706Z","shell.execute_reply.started":"2022-05-14T12:19:37.545287Z","shell.execute_reply":"2022-05-14T12:19:37.669917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_t = test_data.drop(['row_id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:20:56.937906Z","iopub.execute_input":"2022-05-14T12:20:56.938407Z","iopub.status.idle":"2022-05-14T12:20:57.029849Z","shell.execute_reply.started":"2022-05-14T12:20:56.938373Z","shell.execute_reply":"2022-05-14T12:20:57.028881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['anomaly'] = knn.predict(X_test_t)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:20:58.760165Z","iopub.execute_input":"2022-05-14T12:20:58.760497Z","iopub.status.idle":"2022-05-14T12:23:18.779087Z","shell.execute_reply.started":"2022-05-14T12:20:58.760463Z","shell.execute_reply":"2022-05-14T12:23:18.778091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_csv = test_data[['row_id','anomaly']]\nsub_csv","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:31:34.620878Z","iopub.execute_input":"2022-05-14T12:31:34.621301Z","iopub.status.idle":"2022-05-14T12:31:34.653986Z","shell.execute_reply.started":"2022-05-14T12:31:34.621261Z","shell.execute_reply":"2022-05-14T12:31:34.652865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_csv.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T12:31:39.739581Z","iopub.execute_input":"2022-05-14T12:31:39.740209Z","iopub.status.idle":"2022-05-14T12:31:42.719615Z","shell.execute_reply.started":"2022-05-14T12:31:39.740169Z","shell.execute_reply":"2022-05-14T12:31:42.718871Z"},"trusted":true},"execution_count":null,"outputs":[]}]}