{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T08:13:47.997518Z","iopub.execute_input":"2022-07-05T08:13:47.997933Z","iopub.status.idle":"2022-07-05T08:13:48.002807Z","shell.execute_reply.started":"2022-07-05T08:13:47.997882Z","shell.execute_reply":"2022-07-05T08:13:48.001673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Introduction\n\nThe intention of this notebook is to be my first look at the data and a quick submission. This will serve as my baseline for further notebooks, hopefully there is something here that is enjoyable and useful for you.","metadata":{}},{"cell_type":"markdown","source":"# Load and Look at Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:15:57.800083Z","iopub.execute_input":"2022-07-05T08:15:57.800526Z","iopub.status.idle":"2022-07-05T08:15:58.795920Z","shell.execute_reply.started":"2022-07-05T08:15:57.800489Z","shell.execute_reply":"2022-07-05T08:15:58.794564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Describe Data","metadata":{}},{"cell_type":"code","source":"df.describe(include='all').T","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:15:59.534119Z","iopub.execute_input":"2022-07-05T08:15:59.534651Z","iopub.status.idle":"2022-07-05T08:15:59.773553Z","shell.execute_reply.started":"2022-07-05T08:15:59.534601Z","shell.execute_reply":"2022-07-05T08:15:59.772415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply Principle Component Analysis","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nimport plotly.express as px\n\nscaler = StandardScaler()\ndf_temp = df.copy()\ndf_temp = df.drop(columns=\"id\", axis=0)\ndf_temp = scaler.fit_transform(df_temp)\npca = PCA(n_components=0.95)\nX_p = pca.fit(df_temp).transform(df_temp)\nprint(\"95% variance explained by \" + str(len(X_p[0])) + \" components by principle component analysis\")\npca = PCA(n_components=2)\nTwoX_p = pca.fit(df_temp).transform(df_temp)\nprint(str(round(pca.explained_variance_ratio_.sum() * 100)) + \"% variance explained by 2 components by principle component analysis\")\n# 2D plot\nfig = px.scatter(TwoX_p, x=0, y=1, width=600, height=600, title=\"Two Component PCA\")\nfig.show()\npca = PCA(n_components=3)\nThreeX_p = pca.fit(df_temp).transform(df_temp)\nprint(str(round(pca.explained_variance_ratio_.sum() * 100)) + \"% variance explained by 3 components by principle component analysis\")\n# 3D plot \nfig = px.scatter_3d(ThreeX_p, x=0, y=1, z=2, width=600, height=600, title=\"Three Component PCA\")\nfig.show()\n\npca = PCA(n_components=4)\nfourX_p = pca.fit(df_temp).transform(df_temp)\nprint(str(round(pca.explained_variance_ratio_.sum() * 100)) + \"% variance explained by 4 components by principle component analysis\")\n\nlabels = {\n    str(i): f\"PC {i+1} ({var:.1f}%)\"\n    for i, var in enumerate(pca.explained_variance_ratio_ * 100)\n}\n\nfig = px.scatter_matrix(\n    fourX_p,\n    title=\"4 Component PCA\",\n    labels=labels,\n    dimensions=range(4),\n    width=800,\n    height=800\n)\nfig.update_traces(diagonal_visible=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:25:16.465877Z","iopub.execute_input":"2022-07-05T08:25:16.466357Z","iopub.status.idle":"2022-07-05T08:25:20.505502Z","shell.execute_reply.started":"2022-07-05T08:25:16.466316Z","shell.execute_reply":"2022-07-05T08:25:20.502387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Anomaly Detection \n\nIt can help to look at the presence of anomalies in the dataset. ","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import IsolationForest\n\niforest = IsolationForest(random_state=1, contamination=0.01)\niforest.fit(df_temp)\ny_pred_train = iforest.predict(df_temp)\nfig = px.scatter(TwoX_p, x=0, y=1, opacity=0.5, color=y_pred_train,  width=600, height=600, title=\"Two Component PCA With Colored Anomalies\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:38:04.839513Z","iopub.execute_input":"2022-07-05T08:38:04.839940Z","iopub.status.idle":"2022-07-05T08:38:13.826515Z","shell.execute_reply.started":"2022-07-05T08:38:04.839891Z","shell.execute_reply":"2022-07-05T08:38:13.825260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Clustering","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans\n\nkmeans = KMeans(random_state=12345).fit(df_temp)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:45:05.685135Z","iopub.execute_input":"2022-07-05T08:45:05.685571Z","iopub.status.idle":"2022-07-05T08:45:15.113278Z","shell.execute_reply.started":"2022-07-05T08:45:05.685524Z","shell.execute_reply":"2022-07-05T08:45:15.111980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"sample_df = pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")\nsample_df[\"Predicted\"] = kmeans.labels_\nsample_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T08:48:07.797373Z","iopub.execute_input":"2022-07-05T08:48:07.797815Z","iopub.status.idle":"2022-07-05T08:48:07.822663Z","shell.execute_reply.started":"2022-07-05T08:48:07.797778Z","shell.execute_reply":"2022-07-05T08:48:07.821344Z"},"trusted":true},"execution_count":null,"outputs":[]}]}