{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\ntitanic = seaborn.load_dataset('titanic')\n\ntitanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:39:16.080829Z","iopub.execute_input":"2022-07-14T11:39:16.081211Z","iopub.status.idle":"2022-07-14T11:39:16.114301Z","shell.execute_reply.started":"2022-07-14T11:39:16.081181Z","shell.execute_reply":"2022-07-14T11:39:16.113005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Numerical data - Histogram [discrete]\n\nsns.histplot(data = titanic, x = 'age'); # x축을 'age'로 하여 데이터의 구간별 빈도수 나타내기","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:45:07.936354Z","iopub.execute_input":"2022-07-14T11:45:07.936754Z","iopub.status.idle":"2022-07-14T11:45:08.175215Z","shell.execute_reply.started":"2022-07-14T11:45:07.936720Z","shell.execute_reply":"2022-07-14T11:45:08.174352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = titanic, x = 'age', bins = 10); # 구간을 10개로 만들기","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:44:49.752380Z","iopub.execute_input":"2022-07-14T11:44:49.752831Z","iopub.status.idle":"2022-07-14T11:44:49.976708Z","shell.execute_reply.started":"2022-07-14T11:44:49.752791Z","shell.execute_reply":"2022-07-14T11:44:49.975582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = titanic, x = 'age', hue = 'alive'); # 특정 범주형 데이터(생사여부)로 구분한 그래프","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:47:02.265268Z","iopub.execute_input":"2022-07-14T11:47:02.265644Z","iopub.status.idle":"2022-07-14T11:47:02.586597Z","shell.execute_reply.started":"2022-07-14T11:47:02.265615Z","shell.execute_reply":"2022-07-14T11:47:02.585470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = titanic, x = 'age', hue = 'alive', multiple = 'stack'); # 특정 범주형 데이터(생사여부)를 구분한 누적그래프","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:48:16.058569Z","iopub.execute_input":"2022-07-14T11:48:16.059483Z","iopub.status.idle":"2022-07-14T11:48:16.386598Z","shell.execute_reply.started":"2022-07-14T11:48:16.059436Z","shell.execute_reply":"2022-07-14T11:48:16.385398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Numerical data - Kernel density estimation [continuous version of histogram]\n\nsns.kdeplot(data = titanic, x = 'age');","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:49:46.638346Z","iopub.execute_input":"2022-07-14T11:49:46.638724Z","iopub.status.idle":"2022-07-14T11:49:46.848352Z","shell.execute_reply.started":"2022-07-14T11:49:46.638696Z","shell.execute_reply":"2022-07-14T11:49:46.847093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(data = titanic, x = 'age', hue = 'alive', multiple = 'stack');","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:50:26.199424Z","iopub.execute_input":"2022-07-14T11:50:26.199860Z","iopub.status.idle":"2022-07-14T11:50:26.436453Z","shell.execute_reply.started":"2022-07-14T11:50:26.199825Z","shell.execute_reply":"2022-07-14T11:50:26.435141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Numerical data - Displot\n\nsns.displot(data = titanic, x = 'age'); # histogram with different size","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:52:48.819217Z","iopub.execute_input":"2022-07-14T11:52:48.819645Z","iopub.status.idle":"2022-07-14T11:52:49.132776Z","shell.execute_reply.started":"2022-07-14T11:52:48.819610Z","shell.execute_reply":"2022-07-14T11:52:49.131632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data = titanic, x = 'age', kind = 'kde'); # kde graph with different size","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:53:35.267796Z","iopub.execute_input":"2022-07-14T11:53:35.268728Z","iopub.status.idle":"2022-07-14T11:53:35.533781Z","shell.execute_reply.started":"2022-07-14T11:53:35.268688Z","shell.execute_reply":"2022-07-14T11:53:35.532438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data = titanic, x = 'age', kde = True); # both histogram and kde","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:54:25.388381Z","iopub.execute_input":"2022-07-14T11:54:25.388792Z","iopub.status.idle":"2022-07-14T11:54:25.714387Z","shell.execute_reply.started":"2022-07-14T11:54:25.388746Z","shell.execute_reply":"2022-07-14T11:54:25.713091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Numerical data - Rugplot(Marginal distribution)\n\nsns.kdeplot(data = titanic, x = 'age')\nsns.rugplot(data = titanic, x = 'age');","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:57:57.456579Z","iopub.execute_input":"2022-07-14T11:57:57.457141Z","iopub.status.idle":"2022-07-14T11:57:57.690749Z","shell.execute_reply.started":"2022-07-14T11:57:57.457093Z","shell.execute_reply":"2022-07-14T11:57:57.689648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - Barplot : x parameter) categorical data / y parameter) numerical data\n\nsns.barplot(x = 'class', y = 'fare', data = titanic); \n# 막대높이 : 등급별 평균 운임, 검은색 세로줄 : 오차 막대(신뢰구간) -> 등급이 높아질수록 평균운임이 비싸고 신뢰구간이 넓어짐","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:02:48.666973Z","iopub.execute_input":"2022-07-14T12:02:48.667389Z","iopub.status.idle":"2022-07-14T12:02:48.930428Z","shell.execute_reply.started":"2022-07-14T12:02:48.667354Z","shell.execute_reply":"2022-07-14T12:02:48.928990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - pointplot\n\nsns.pointplot(x = 'class', y = 'fare', data = titanic); # same with barplot but in dots and lines","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:03:44.357910Z","iopub.execute_input":"2022-07-14T12:03:44.358313Z","iopub.status.idle":"2022-07-14T12:03:44.659939Z","shell.execute_reply.started":"2022-07-14T12:03:44.358283Z","shell.execute_reply":"2022-07-14T12:03:44.658022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - boxplot\n\nsns.boxplot(x = 'class', y = 'age', data = titanic); # 탑승자 등급별 나이 : x-categorical, y-numerical","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:13:38.206571Z","iopub.execute_input":"2022-07-14T12:13:38.207027Z","iopub.status.idle":"2022-07-14T12:13:38.412330Z","shell.execute_reply.started":"2022-07-14T12:13:38.206992Z","shell.execute_reply":"2022-07-14T12:13:38.411115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - violin plot : boxplot + kde\n\nsns.violinplot(x = 'class', y = 'age', data = titanic); # 탑승자 등급별 나이 : x-categorical, y-numerical","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:17:11.508535Z","iopub.execute_input":"2022-07-14T12:17:11.509157Z","iopub.status.idle":"2022-07-14T12:17:11.842729Z","shell.execute_reply.started":"2022-07-14T12:17:11.509112Z","shell.execute_reply":"2022-07-14T12:17:11.841568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.violinplot(x = 'class', y = 'age', hue = 'sex', data = titanic, split = True); \n# 성별에 따른 등급별 나이 분포","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:17:31.934928Z","iopub.execute_input":"2022-07-14T12:17:31.936003Z","iopub.status.idle":"2022-07-14T12:17:32.201059Z","shell.execute_reply.started":"2022-07-14T12:17:31.935951Z","shell.execute_reply":"2022-07-14T12:17:32.199834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - count plot : 범주형 데이터의 개수 확인\n\nsns.countplot(x = 'class', data = titanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:19:12.320675Z","iopub.execute_input":"2022-07-14T12:19:12.321629Z","iopub.status.idle":"2022-07-14T12:19:12.482258Z","shell.execute_reply.started":"2022-07-14T12:19:12.321581Z","shell.execute_reply":"2022-07-14T12:19:12.480811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(y = 'class', data = titanic); # x축 데이터를 y에 넣으면 그래프 방향 바뀜","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:19:36.322896Z","iopub.execute_input":"2022-07-14T12:19:36.323433Z","iopub.status.idle":"2022-07-14T12:19:36.494087Z","shell.execute_reply.started":"2022-07-14T12:19:36.323385Z","shell.execute_reply":"2022-07-14T12:19:36.492936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Categorical data - pie : 범주형 데이터의 비율 파악 only in matplotlib\n\nimport matplotlib.pyplot as plt\n\nx = [10, 60, 30] # 범주형 데이터별 파이 그래프의 부채꼴 크기(비율)\nlabels = ['A', 'B', 'C'] # 범주형 데이터 레이블\n\nplt.pie(x = x, labels = labels, autopct = '%.1f%%'); \n# autopct 파라미터를 통해 비율을 숫자로 나타낼 수 있음, %.1f는 소수점 첫째자리, %%는 퍼센트까지 나타낸다는 뜻 ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:25:50.212421Z","iopub.execute_input":"2022-07-14T12:25:50.213179Z","iopub.status.idle":"2022-07-14T12:25:50.316540Z","shell.execute_reply.started":"2022-07-14T12:25:50.213127Z","shell.execute_reply":"2022-07-14T12:25:50.314659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 관계도 - heatmap\n\nimport seaborn as sns\nflights = sns.load_dataset('flights')\n\nflights.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:29:24.953595Z","iopub.execute_input":"2022-07-14T12:29:24.954036Z","iopub.status.idle":"2022-07-14T12:29:24.975872Z","shell.execute_reply.started":"2022-07-14T12:29:24.953991Z","shell.execute_reply":"2022-07-14T12:29:24.974922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flights_pivot = flights.pivot(index = 'month',\n                              columns = 'year',\n                              values = 'passengers')\n\nflights_pivot","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:32:10.816259Z","iopub.execute_input":"2022-07-14T12:32:10.816658Z","iopub.status.idle":"2022-07-14T12:32:10.840336Z","shell.execute_reply.started":"2022-07-14T12:32:10.816627Z","shell.execute_reply":"2022-07-14T12:32:10.839256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(data = flights_pivot);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:33:06.044859Z","iopub.execute_input":"2022-07-14T12:33:06.045243Z","iopub.status.idle":"2022-07-14T12:33:06.381403Z","shell.execute_reply.started":"2022-07-14T12:33:06.045212Z","shell.execute_reply":"2022-07-14T12:33:06.380254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 관계도 - lineplot : relationship between two numerical data\n# x 파라미터에 전달한 값에 따라 y 파라미터에 전달한 값의 평균과 95% 신뢰구간을 나타냄\n# x축은 연도, y축은 평균 승객수 -> 해가 갈수록 평균 승객수는 늘어남, 음영은 95% 신뢰구간\n\nsns.lineplot(x = 'year', y = 'passengers', data = flights);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:36:14.268969Z","iopub.execute_input":"2022-07-14T12:36:14.269336Z","iopub.status.idle":"2022-07-14T12:36:14.830490Z","shell.execute_reply.started":"2022-07-14T12:36:14.269307Z","shell.execute_reply":"2022-07-14T12:36:14.829178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 관계도 - scatterplot\n\ntips = sns.load_dataset('tips')\ntips.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:37:55.820985Z","iopub.execute_input":"2022-07-14T12:37:55.821363Z","iopub.status.idle":"2022-07-14T12:37:55.844500Z","shell.execute_reply.started":"2022-07-14T12:37:55.821334Z","shell.execute_reply":"2022-07-14T12:37:55.843247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(x = 'total_bill', y = 'tip', data = tips);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:38:34.581498Z","iopub.execute_input":"2022-07-14T12:38:34.582189Z","iopub.status.idle":"2022-07-14T12:38:34.782041Z","shell.execute_reply.started":"2022-07-14T12:38:34.582135Z","shell.execute_reply":"2022-07-14T12:38:34.780817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'time', data = tips);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:39:15.212783Z","iopub.execute_input":"2022-07-14T12:39:15.213215Z","iopub.status.idle":"2022-07-14T12:39:15.466586Z","shell.execute_reply.started":"2022-07-14T12:39:15.213179Z","shell.execute_reply":"2022-07-14T12:39:15.465400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 관계도 - regplot\n\nsns.regplot(x = 'total_bill', y = 'tip', data = tips); # 회귀선 주변 음영은 95% 신뢰구간","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:40:14.119128Z","iopub.execute_input":"2022-07-14T12:40:14.119671Z","iopub.status.idle":"2022-07-14T12:40:14.440657Z","shell.execute_reply.started":"2022-07-14T12:40:14.119623Z","shell.execute_reply":"2022-07-14T12:40:14.437982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.regplot(x = 'total_bill', y = 'tip', ci = 99, data = tips); # 99% 신뢰구간","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:40:55.708264Z","iopub.execute_input":"2022-07-14T12:40:55.709148Z","iopub.status.idle":"2022-07-14T12:40:55.965122Z","shell.execute_reply.started":"2022-07-14T12:40:55.709087Z","shell.execute_reply":"2022-07-14T12:40:55.963677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}