{"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\nimport pandas as pd     # 판다스 임포트\n# 데이터 경로\ndata_path = '/kaggle/input/bike-sharing-demand/'\n\ntrain = pd.read_csv(data_path + 'train.csv')    # 훈련 데이터\ntest = pd.read_csv(data_path + 'test.csv')     # 테스트 데이터\nsubmission = pd.read_csv(data_path + 'sampleSubmission.csv')   # 제출 샘플 데이터","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.705211Z","iopub.execute_input":"2022-07-29T04:35:10.705919Z","iopub.status.idle":"2022-07-29T04:35:10.761635Z","shell.execute_reply.started":"2022-07-29T04:35:10.705871Z","shell.execute_reply":"2022-07-29T04:35:10.760041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.764134Z","iopub.execute_input":"2022-07-29T04:35:10.764699Z","iopub.status.idle":"2022-07-29T04:35:10.775049Z","shell.execute_reply.started":"2022-07-29T04:35:10.764662Z","shell.execute_reply":"2022-07-29T04:35:10.773375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.808518Z","iopub.execute_input":"2022-07-29T04:35:10.809841Z","iopub.status.idle":"2022-07-29T04:35:10.826277Z","shell.execute_reply.started":"2022-07-29T04:35:10.809725Z","shell.execute_reply":"2022-07-29T04:35:10.824956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.856887Z","iopub.execute_input":"2022-07-29T04:35:10.857336Z","iopub.status.idle":"2022-07-29T04:35:10.879894Z","shell.execute_reply.started":"2022-07-29T04:35:10.857301Z","shell.execute_reply":"2022-07-29T04:35:10.877948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.906494Z","iopub.execute_input":"2022-07-29T04:35:10.906988Z","iopub.status.idle":"2022-07-29T04:35:10.919363Z","shell.execute_reply.started":"2022-07-29T04:35:10.906950Z","shell.execute_reply":"2022-07-29T04:35:10.917882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:10.957361Z","iopub.execute_input":"2022-07-29T04:35:10.958325Z","iopub.status.idle":"2022-07-29T04:35:10.975483Z","shell.execute_reply.started":"2022-07-29T04:35:10.958273Z","shell.execute_reply":"2022-07-29T04:35:10.974620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.006503Z","iopub.execute_input":"2022-07-29T04:35:11.007631Z","iopub.status.idle":"2022-07-29T04:35:11.021810Z","shell.execute_reply.started":"2022-07-29T04:35:11.007567Z","shell.execute_reply":"2022-07-29T04:35:11.020624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.051943Z","iopub.execute_input":"2022-07-29T04:35:11.052896Z","iopub.status.idle":"2022-07-29T04:35:11.067617Z","shell.execute_reply.started":"2022-07-29T04:35:11.052855Z","shell.execute_reply":"2022-07-29T04:35:11.066642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100])  # 날짜\nprint(train['datetime'][100].split())  # 공백 기준으로 문자 나누기\nprint(train['datetime'][100].split()[0])  # 날짜\nprint(train['datetime'][100].split()[1])  # 시간","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.131029Z","iopub.execute_input":"2022-07-29T04:35:11.131527Z","iopub.status.idle":"2022-07-29T04:35:11.138552Z","shell.execute_reply.started":"2022-07-29T04:35:11.131490Z","shell.execute_reply":"2022-07-29T04:35:11.137640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100].split()[0])  # 날짜\nprint(train['datetime'][100].split()[0].split(\"-\"))  # \"-\" 기준으로 문자열 나누기\nprint(train['datetime'][100].split()[0].split(\"-\")[0])  # 연도\nprint(train['datetime'][100].split()[0].split(\"-\")[1])  # 월\nprint(train['datetime'][100].split()[0].split(\"-\")[2])  # 일\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.228239Z","iopub.execute_input":"2022-07-29T04:35:11.229490Z","iopub.status.idle":"2022-07-29T04:35:11.239331Z","shell.execute_reply.started":"2022-07-29T04:35:11.229433Z","shell.execute_reply":"2022-07-29T04:35:11.237937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100].split()[1])  # 시간\nprint(train['datetime'][100].split()[1].split(\":\"))  # \":\" 기준으로 문자열 나누기\nprint(train['datetime'][100].split()[1].split(\":\")[0])  # 시간\nprint(train['datetime'][100].split()[1].split(\":\")[1])  # 분\nprint(train['datetime'][100].split()[1].split(\":\")[2])  # 초","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.291001Z","iopub.execute_input":"2022-07-29T04:35:11.291681Z","iopub.status.idle":"2022-07-29T04:35:11.301986Z","shell.execute_reply.started":"2022-07-29T04:35:11.291630Z","shell.execute_reply":"2022-07-29T04:35:11.300358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['date'] = train['datetime'].apply(lambda x: x.split()[0])  # 날짜 피처 생성\n\n# 연도, 월, 일, 시, 분, 초 피처를 차례로 생성\ntrain['year'] = train['datetime'].apply(lambda x: x.split()[0].split('-')[0])\ntrain['month'] = train['datetime'].apply(lambda x: x.split()[0].split('-')[1])\ntrain['day'] = train['datetime'].apply(lambda x: x.split()[0].split('-')[2])\ntrain['hour'] = train['datetime'].apply(lambda x: x.split()[1].split(':')[0])\ntrain['minute'] = train['datetime'].apply(lambda x: x.split()[1].split(':')[1])\ntrain['second'] = train['datetime'].apply(lambda x: x.split()[1].split(':')[2])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.331755Z","iopub.execute_input":"2022-07-29T04:35:11.332625Z","iopub.status.idle":"2022-07-29T04:35:11.407697Z","shell.execute_reply.started":"2022-07-29T04:35:11.332566Z","shell.execute_reply":"2022-07-29T04:35:11.406633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime  # datetime 라이브러리 임포트\nimport calendar\n\nprint(train['date'][100])  # 날짜\nprint(datetime.strptime(train['date'][100], '%Y-%m-%d'))  # datetime 타입으로 변경\n# 정수로 요일 변환\nprint(datetime.strptime(train['date'][100], '%Y-%m-%d').weekday())\n# 문자열로 요일 변환\nprint(calendar.day_name[datetime.strptime(train['date'][100], '%Y-%m-%d').weekday()])\n                    \n                        ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.411768Z","iopub.execute_input":"2022-07-29T04:35:11.412102Z","iopub.status.idle":"2022-07-29T04:35:11.421214Z","shell.execute_reply.started":"2022-07-29T04:35:11.412072Z","shell.execute_reply":"2022-07-29T04:35:11.419964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weekday'] = train['date'].apply(\n    lambda dateString:\n    calendar.day_name[datetime.strptime(dateString, \"%Y-%m-%d\").weekday()])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.425757Z","iopub.execute_input":"2022-07-29T04:35:11.426112Z","iopub.status.idle":"2022-07-29T04:35:11.632276Z","shell.execute_reply.started":"2022-07-29T04:35:11.426080Z","shell.execute_reply":"2022-07-29T04:35:11.630869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['season'] = train['season'].map({1: 'Spring',\n                                       2: 'Summer',\n                                       3: 'Fall',\n                                       4: 'Winter'})\ntrain['weathe'] = train['weather'].map({1: 'Clear',\n                                        2: 'Mist, Few clouds',\n                                        3: 'Light Snow, Rain, Thunderstorm',\n                                        4: 'Heavy Rain, Thunderstorm, Snow, Fog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.635095Z","iopub.execute_input":"2022-07-29T04:35:11.635560Z","iopub.status.idle":"2022-07-29T04:35:11.645947Z","shell.execute_reply.started":"2022-07-29T04:35:11.635513Z","shell.execute_reply":"2022-07-29T04:35:11.644702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.647587Z","iopub.execute_input":"2022-07-29T04:35:11.647949Z","iopub.status.idle":"2022-07-29T04:35:11.679526Z","shell.execute_reply.started":"2022-07-29T04:35:11.647917Z","shell.execute_reply":"2022-07-29T04:35:11.678430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.683376Z","iopub.execute_input":"2022-07-29T04:35:11.683875Z","iopub.status.idle":"2022-07-29T04:35:11.691031Z","shell.execute_reply.started":"2022-07-29T04:35:11.683842Z","shell.execute_reply":"2022-07-29T04:35:11.689876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=15)  # 폰트 크기를 15로 설정\nsns.displot(train['count']); # 분포도 출력","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:11.692525Z","iopub.execute_input":"2022-07-29T04:35:11.693737Z","iopub.status.idle":"2022-07-29T04:35:12.081781Z","shell.execute_reply.started":"2022-07-29T04:35:11.693686Z","shell.execute_reply":"2022-07-29T04:35:12.080399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(np.log(train['count']));","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:12.083202Z","iopub.execute_input":"2022-07-29T04:35:12.083523Z","iopub.status.idle":"2022-07-29T04:35:12.435530Z","shell.execute_reply.started":"2022-07-29T04:35:12.083493Z","shell.execute_reply":"2022-07-29T04:35:12.434267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 스텝 1: m행 n열 Figure 준비\nmpl.rc('font', size=14)   # 폰트 크기 설정\nmpl.rc('axes', titlesize=15)  # 각 축의 제목 크기 설정\nfigure, axes = plt.subplots(nrows=3, ncols=2)  # 3행 2열 Figure 생성\nplt.tight_layout()     # 그래프 사이에 여백 확보\nfigure.set_size_inches(10, 9)   # 전체 figure 크기를 10*9인치로 설정\n\n# 스텝 2: 각 축에 서브플롯 할당\n# 각 축에 연도, 월, 일, 시간, 분, 초별 평균 대여 수량 막대 그래프 할당\nsns.barplot(x='year', y='count', data=train, ax=axes[0, 0])\nsns.barplot(x='month', y='count', data=train, ax=axes[0, 1])\nsns.barplot(x='day', y='count', data=train, ax=axes[1, 0])\nsns.barplot(x='hour', y='count', data=train, ax=axes[1, 1])\nsns.barplot(x='minute', y='count', data=train, ax=axes[2, 0])\nsns.barplot(x='second', y='count', data=train, ax=axes[2, 1])\n\n#스텝 3: 세부 설정\n# 3-1: 서브플롯에 제목 달기\naxes[0, 0].set(title='Rental amounts by year')\naxes[0, 1].set(title='Rental amounts by month')\naxes[1, 0].set(title='Rental amounts by day')\naxes[1, 1].set(title='Rental amounts by hour')\naxes[2, 0].set(title='Rental amounts by minute')\naxes[2, 1].set(title='Rental amounts by second')\n\n# 3-2: 1행에 위치한 서브플롯들의 x푹 라벨 90도 회전\naxes[1, 0].tick_params(axis='x', labelrotation=90)\naxes[1, 1].tick_params(axis='x', labelrotation=90)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:35:12.437901Z","iopub.execute_input":"2022-07-29T04:35:12.438349Z","iopub.status.idle":"2022-07-29T04:35:16.166928Z","shell.execute_reply.started":"2022-07-29T04:35:12.438305Z","shell.execute_reply":"2022-07-29T04:35:16.165488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 스텝 1: m행 n열 Figure 준비\nfigure, axes =plt.subplots(nrows=2, ncols=2) # 2행 2열","metadata":{},"execution_count":null,"outputs":[]}]}