{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.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":35297,"databundleVersionId":3589144,"sourceType":"competition"}],"dockerImageVersionId":30191,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-01-01T05:51:51.018056Z","iopub.execute_input":"2024-01-01T05:51:51.018988Z","iopub.status.idle":"2024-01-01T05:51:51.054492Z","shell.execute_reply.started":"2024-01-01T05:51:51.018856Z","shell.execute_reply":"2024-01-01T05:51:51.053259Z"},"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":"2024-01-01T05:51:51.056597Z","iopub.execute_input":"2024-01-01T05:51:51.057078Z","iopub.status.idle":"2024-01-01T05:51:52.951082Z","shell.execute_reply.started":"2024-01-01T05:51:51.057027Z","shell.execute_reply":"2024-01-01T05:51:52.949899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:52.953342Z","iopub.execute_input":"2024-01-01T05:51:52.953725Z","iopub.status.idle":"2024-01-01T05:51:52.981407Z","shell.execute_reply.started":"2024-01-01T05:51:52.953681Z","shell.execute_reply":"2024-01-01T05:51:52.980629Z"},"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":"2024-01-01T05:51:52.983588Z","iopub.execute_input":"2024-01-01T05:51:52.984435Z","iopub.status.idle":"2024-01-01T05:51:53.490034Z","shell.execute_reply.started":"2024-01-01T05:51:52.984387Z","shell.execute_reply":"2024-01-01T05:51:53.489059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.dropna()\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:53.492553Z","iopub.execute_input":"2024-01-01T05:51:53.493303Z","iopub.status.idle":"2024-01-01T05:51:53.910533Z","shell.execute_reply.started":"2024-01-01T05:51:53.493238Z","shell.execute_reply":"2024-01-01T05:51:53.909455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['timestamp'] = pd.to_datetime(data['timestamp'])\ndata.tail()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:53.912662Z","iopub.execute_input":"2024-01-01T05:51:53.913015Z","iopub.status.idle":"2024-01-01T05:51:54.311979Z","shell.execute_reply.started":"2024-01-01T05:51:53.912975Z","shell.execute_reply":"2024-01-01T05:51:54.311282Z"},"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":"2024-01-01T05:51:54.313195Z","iopub.execute_input":"2024-01-01T05:51:54.314019Z","iopub.status.idle":"2024-01-01T05:51:55.513380Z","shell.execute_reply.started":"2024-01-01T05:51:54.313981Z","shell.execute_reply":"2024-01-01T05:51:55.512381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['meter_reading'].plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:55.514794Z","iopub.execute_input":"2024-01-01T05:51:55.515078Z","iopub.status.idle":"2024-01-01T05:51:56.200554Z","shell.execute_reply.started":"2024-01-01T05:51:55.515043Z","shell.execute_reply":"2024-01-01T05:51:56.199535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['anomaly'].plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:56.202240Z","iopub.execute_input":"2024-01-01T05:51:56.203275Z","iopub.status.idle":"2024-01-01T05:51:56.976182Z","shell.execute_reply.started":"2024-01-01T05:51:56.203220Z","shell.execute_reply":"2024-01-01T05:51:56.975168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(['anomaly'], axis=1)\ny = data['anomaly']","metadata":{"execution":{"iopub.status.busy":"2024-01-01T05:51:56.979422Z","iopub.execute_input":"2024-01-01T05:51:56.979730Z","iopub.status.idle":"2024-01-01T05:51:57.062927Z","shell.execute_reply.started":"2024-01-01T05:51:56.979694Z","shell.execute_reply":"2024-01-01T05:51:57.062083Z"},"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":"2024-01-01T05:51:57.064471Z","iopub.execute_input":"2024-01-01T05:51:57.064958Z","iopub.status.idle":"2024-01-01T05:51:58.228516Z","shell.execute_reply.started":"2024-01-01T05:51:57.064893Z","shell.execute_reply":"2024-01-01T05:51:58.227319Z"},"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":"2024-01-01T05:51:58.230118Z","iopub.execute_input":"2024-01-01T05:51:58.230531Z","iopub.status.idle":"2024-01-01T05:52:19.458912Z","shell.execute_reply.started":"2024-01-01T05:51:58.230479Z","shell.execute_reply":"2024-01-01T05:52:19.457942Z"},"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":"2024-01-01T05:52:19.460242Z","iopub.execute_input":"2024-01-01T05:52:19.460523Z","iopub.status.idle":"2024-01-01T05:53:02.401663Z","shell.execute_reply.started":"2024-01-01T05:52:19.460488Z","shell.execute_reply":"2024-01-01T05:53:02.400674Z"},"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":"2024-01-01T05:53:02.402986Z","iopub.execute_input":"2024-01-01T05:53:02.403281Z","iopub.status.idle":"2024-01-01T05:53:22.222630Z","shell.execute_reply.started":"2024-01-01T05:53:02.403246Z","shell.execute_reply":"2024-01-01T05:53:22.221500Z"},"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":"2024-01-01T05:53:22.224173Z","iopub.execute_input":"2024-01-01T05:53:22.224499Z","iopub.status.idle":"2024-01-01T06:00:44.348933Z","shell.execute_reply.started":"2024-01-01T05:53:22.224461Z","shell.execute_reply":"2024-01-01T06:00:44.347802Z"},"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":"2024-01-01T06:00:44.350799Z","iopub.execute_input":"2024-01-01T06:00:44.351089Z","iopub.status.idle":"2024-01-01T06:00:46.060139Z","shell.execute_reply.started":"2024-01-01T06:00:44.351055Z","shell.execute_reply":"2024-01-01T06:00:46.059295Z"},"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":"2024-01-01T06:00:46.061430Z","iopub.execute_input":"2024-01-01T06:00:46.061684Z","iopub.status.idle":"2024-01-01T06:00:47.860508Z","shell.execute_reply.started":"2024-01-01T06:00:46.061652Z","shell.execute_reply":"2024-01-01T06:00:47.859461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:00:47.862008Z","iopub.execute_input":"2024-01-01T06:00:47.862269Z","iopub.status.idle":"2024-01-01T06:00:47.932800Z","shell.execute_reply.started":"2024-01-01T06:00:47.862236Z","shell.execute_reply":"2024-01-01T06:00:47.931672Z"},"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":"2024-01-01T06:00:47.934600Z","iopub.execute_input":"2024-01-01T06:00:47.935327Z","iopub.status.idle":"2024-01-01T06:00:48.032729Z","shell.execute_reply.started":"2024-01-01T06:00:47.935261Z","shell.execute_reply":"2024-01-01T06:00:48.031273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['anomaly'] = knn.predict(X_test_t)","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:00:48.034832Z","iopub.execute_input":"2024-01-01T06:00:48.035713Z","iopub.status.idle":"2024-01-01T06:03:31.697967Z","shell.execute_reply.started":"2024-01-01T06:00:48.035640Z","shell.execute_reply":"2024-01-01T06:03:31.696974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_csv = test_data[['row_id','anomaly']]\nsub_csv","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:03:31.699760Z","iopub.execute_input":"2024-01-01T06:03:31.700044Z","iopub.status.idle":"2024-01-01T06:03:31.853779Z","shell.execute_reply.started":"2024-01-01T06:03:31.700011Z","shell.execute_reply":"2024-01-01T06:03:31.852868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_csv.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:03:31.855227Z","iopub.execute_input":"2024-01-01T06:03:31.855491Z","iopub.status.idle":"2024-01-01T06:03:35.172298Z","shell.execute_reply.started":"2024-01-01T06:03:31.855459Z","shell.execute_reply":"2024-01-01T06:03:35.171453Z"},"trusted":true},"execution_count":null,"outputs":[]}]}