{"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-07-24T11:59:32.937547Z","iopub.execute_input":"2022-07-24T11:59:32.937941Z","iopub.status.idle":"2022-07-24T11:59:32.970205Z","shell.execute_reply.started":"2022-07-24T11:59:32.937869Z","shell.execute_reply":"2022-07-24T11:59:32.969557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ndata_path = '../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-24T11:59:32.971942Z","iopub.execute_input":"2022-07-24T11:59:32.973161Z","iopub.status.idle":"2022-07-24T11:59:33.035353Z","shell.execute_reply.started":"2022-07-24T11:59:32.973128Z","shell.execute_reply":"2022-07-24T11:59:33.034303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.037365Z","iopub.execute_input":"2022-07-24T11:59:33.037763Z","iopub.status.idle":"2022-07-24T11:59:33.048275Z","shell.execute_reply.started":"2022-07-24T11:59:33.037725Z","shell.execute_reply":"2022-07-24T11:59:33.046807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.051517Z","iopub.execute_input":"2022-07-24T11:59:33.051889Z","iopub.status.idle":"2022-07-24T11:59:33.079839Z","shell.execute_reply.started":"2022-07-24T11:59:33.051853Z","shell.execute_reply":"2022-07-24T11:59:33.078801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.081820Z","iopub.execute_input":"2022-07-24T11:59:33.082089Z","iopub.status.idle":"2022-07-24T11:59:33.099375Z","shell.execute_reply.started":"2022-07-24T11:59:33.082062Z","shell.execute_reply":"2022-07-24T11:59:33.098287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.100526Z","iopub.execute_input":"2022-07-24T11:59:33.100935Z","iopub.status.idle":"2022-07-24T11:59:33.114444Z","shell.execute_reply.started":"2022-07-24T11:59:33.100898Z","shell.execute_reply":"2022-07-24T11:59:33.113572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.115676Z","iopub.execute_input":"2022-07-24T11:59:33.116185Z","iopub.status.idle":"2022-07-24T11:59:33.147024Z","shell.execute_reply.started":"2022-07-24T11:59:33.116162Z","shell.execute_reply":"2022-07-24T11:59:33.146187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.148202Z","iopub.execute_input":"2022-07-24T11:59:33.148630Z","iopub.status.idle":"2022-07-24T11:59:33.158225Z","shell.execute_reply.started":"2022-07-24T11:59:33.148606Z","shell.execute_reply":"2022-07-24T11:59:33.157443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.161717Z","iopub.execute_input":"2022-07-24T11:59:33.162738Z","iopub.status.idle":"2022-07-24T11:59:33.168469Z","shell.execute_reply.started":"2022-07-24T11:59:33.162706Z","shell.execute_reply":"2022-07-24T11:59:33.167936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.169422Z","iopub.execute_input":"2022-07-24T11:59:33.169797Z","iopub.status.idle":"2022-07-24T11:59:33.180858Z","shell.execute_reply.started":"2022-07-24T11:59:33.169774Z","shell.execute_reply":"2022-07-24T11:59:33.179768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.182365Z","iopub.execute_input":"2022-07-24T11:59:33.183498Z","iopub.status.idle":"2022-07-24T11:59:33.192707Z","shell.execute_reply.started":"2022-07-24T11:59:33.183458Z","shell.execute_reply":"2022-07-24T11:59:33.191874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.193982Z","iopub.execute_input":"2022-07-24T11:59:33.194224Z","iopub.status.idle":"2022-07-24T11:59:33.203956Z","shell.execute_reply.started":"2022-07-24T11:59:33.194202Z","shell.execute_reply":"2022-07-24T11:59:33.203404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.to_datetime(train['datetime'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.205063Z","iopub.execute_input":"2022-07-24T11:59:33.205421Z","iopub.status.idle":"2022-07-24T11:59:33.232743Z","shell.execute_reply.started":"2022-07-24T11:59:33.205367Z","shell.execute_reply":"2022-07-24T11:59:33.231090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.233857Z","iopub.execute_input":"2022-07-24T11:59:33.234252Z","iopub.status.idle":"2022-07-24T11:59:33.243880Z","shell.execute_reply.started":"2022-07-24T11:59:33.234229Z","shell.execute_reply":"2022-07-24T11:59:33.243053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.244652Z","iopub.execute_input":"2022-07-24T11:59:33.245595Z","iopub.status.idle":"2022-07-24T11:59:33.252921Z","shell.execute_reply.started":"2022-07-24T11:59:33.245559Z","shell.execute_reply":"2022-07-24T11:59:33.251849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0].split('-')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.254076Z","iopub.execute_input":"2022-07-24T11:59:33.254409Z","iopub.status.idle":"2022-07-24T11:59:33.265254Z","shell.execute_reply.started":"2022-07-24T11:59:33.254358Z","shell.execute_reply":"2022-07-24T11:59:33.263891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0].split('-')[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.268220Z","iopub.execute_input":"2022-07-24T11:59:33.268620Z","iopub.status.idle":"2022-07-24T11:59:33.274055Z","shell.execute_reply.started":"2022-07-24T11:59:33.268590Z","shell.execute_reply":"2022-07-24T11:59:33.273539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0].split('-')[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.274868Z","iopub.execute_input":"2022-07-24T11:59:33.275540Z","iopub.status.idle":"2022-07-24T11:59:33.286493Z","shell.execute_reply.started":"2022-07-24T11:59:33.275516Z","shell.execute_reply":"2022-07-24T11:59:33.285614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[0].split('-')[2]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.288496Z","iopub.execute_input":"2022-07-24T11:59:33.288933Z","iopub.status.idle":"2022-07-24T11:59:33.298962Z","shell.execute_reply.started":"2022-07-24T11:59:33.288899Z","shell.execute_reply":"2022-07-24T11:59:33.297763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.300878Z","iopub.execute_input":"2022-07-24T11:59:33.301441Z","iopub.status.idle":"2022-07-24T11:59:33.311239Z","shell.execute_reply.started":"2022-07-24T11:59:33.301378Z","shell.execute_reply":"2022-07-24T11:59:33.310521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1].split(':')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.312512Z","iopub.execute_input":"2022-07-24T11:59:33.313186Z","iopub.status.idle":"2022-07-24T11:59:33.325124Z","shell.execute_reply.started":"2022-07-24T11:59:33.313156Z","shell.execute_reply":"2022-07-24T11:59:33.323947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1].split(':')[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.331245Z","iopub.execute_input":"2022-07-24T11:59:33.331974Z","iopub.status.idle":"2022-07-24T11:59:33.339000Z","shell.execute_reply.started":"2022-07-24T11:59:33.331942Z","shell.execute_reply":"2022-07-24T11:59:33.337943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1].split(':')[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.340163Z","iopub.execute_input":"2022-07-24T11:59:33.341166Z","iopub.status.idle":"2022-07-24T11:59:33.354184Z","shell.execute_reply.started":"2022-07-24T11:59:33.341128Z","shell.execute_reply":"2022-07-24T11:59:33.353198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'][100].split()[1].split(':')[2]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.355318Z","iopub.execute_input":"2022-07-24T11:59:33.355607Z","iopub.status.idle":"2022-07-24T11:59:33.366715Z","shell.execute_reply.started":"2022-07-24T11:59:33.355585Z","shell.execute_reply":"2022-07-24T11:59:33.365461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['date'] = train['datetime'].apply(lambda x:x.split()[0])\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.367965Z","iopub.execute_input":"2022-07-24T11:59:33.368224Z","iopub.status.idle":"2022-07-24T11:59:33.393218Z","shell.execute_reply.started":"2022-07-24T11:59:33.368179Z","shell.execute_reply":"2022-07-24T11:59:33.391978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['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])\n\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-24T11:59:33.394545Z","iopub.execute_input":"2022-07-24T11:59:33.395596Z","iopub.status.idle":"2022-07-24T11:59:33.483559Z","shell.execute_reply.started":"2022-07-24T11:59:33.395554Z","shell.execute_reply":"2022-07-24T11:59:33.481963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.484928Z","iopub.execute_input":"2022-07-24T11:59:33.485575Z","iopub.status.idle":"2022-07-24T11:59:33.508635Z","shell.execute_reply.started":"2022-07-24T11:59:33.485545Z","shell.execute_reply":"2022-07-24T11:59:33.507875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.509771Z","iopub.execute_input":"2022-07-24T11:59:33.510174Z","iopub.status.idle":"2022-07-24T11:59:33.533476Z","shell.execute_reply.started":"2022-07-24T11:59:33.510147Z","shell.execute_reply":"2022-07-24T11:59:33.532463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['datetime'] = pd.to_datetime(train['datetime'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.534947Z","iopub.execute_input":"2022-07-24T11:59:33.535780Z","iopub.status.idle":"2022-07-24T11:59:33.546227Z","shell.execute_reply.started":"2022-07-24T11:59:33.535743Z","shell.execute_reply":"2022-07-24T11:59:33.545040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.547465Z","iopub.execute_input":"2022-07-24T11:59:33.547949Z","iopub.status.idle":"2022-07-24T11:59:33.574028Z","shell.execute_reply.started":"2022-07-24T11:59:33.547920Z","shell.execute_reply":"2022-07-24T11:59:33.571032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['year'] = train['datetime'].dt.year\ntrain['month'] = train['datetime'].dt.month\ntrain['day'] = train['datetime'].dt.day\n\ntrain['hour'] = train['datetime'].dt.hour\ntrain['minute'] = train['datetime'].dt.minute\ntrain['second'] = train['datetime'].dt.second","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.576000Z","iopub.execute_input":"2022-07-24T11:59:33.576429Z","iopub.status.idle":"2022-07-24T11:59:33.604876Z","shell.execute_reply.started":"2022-07-24T11:59:33.576373Z","shell.execute_reply":"2022-07-24T11:59:33.603148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.606321Z","iopub.execute_input":"2022-07-24T11:59:33.606981Z","iopub.status.idle":"2022-07-24T11:59:33.626109Z","shell.execute_reply.started":"2022-07-24T11:59:33.606943Z","shell.execute_reply":"2022-07-24T11:59:33.625461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nimport calendar\n\nprint(train['date'][100])\nprint(datetime.strptime(train['date'][100], '%Y-%m-%d'))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.627057Z","iopub.execute_input":"2022-07-24T11:59:33.627449Z","iopub.status.idle":"2022-07-24T11:59:33.635233Z","shell.execute_reply.started":"2022-07-24T11:59:33.627421Z","shell.execute_reply":"2022-07-24T11:59:33.634075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.strptime(train['date'][100], '%Y-%m-%d').weekday())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.636200Z","iopub.execute_input":"2022-07-24T11:59:33.636463Z","iopub.status.idle":"2022-07-24T11:59:33.647067Z","shell.execute_reply.started":"2022-07-24T11:59:33.636438Z","shell.execute_reply":"2022-07-24T11:59:33.646055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(calendar.day_name[datetime.strptime(train['date'][100], '%Y-%m-%d').weekday()])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.648319Z","iopub.execute_input":"2022-07-24T11:59:33.648933Z","iopub.status.idle":"2022-07-24T11:59:33.659669Z","shell.execute_reply.started":"2022-07-24T11:59:33.648897Z","shell.execute_reply":"2022-07-24T11:59:33.658347Z"},"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-24T11:59:33.660631Z","iopub.execute_input":"2022-07-24T11:59:33.661110Z","iopub.status.idle":"2022-07-24T11:59:33.800028Z","shell.execute_reply.started":"2022-07-24T11:59:33.661086Z","shell.execute_reply":"2022-07-24T11:59:33.798901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weekday']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.801815Z","iopub.execute_input":"2022-07-24T11:59:33.802211Z","iopub.status.idle":"2022-07-24T11:59:33.809212Z","shell.execute_reply.started":"2022-07-24T11:59:33.802173Z","shell.execute_reply":"2022-07-24T11:59:33.808538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:33.810353Z","iopub.execute_input":"2022-07-24T11:59:33.810810Z","iopub.status.idle":"2022-07-24T11:59:33.838074Z","shell.execute_reply.started":"2022-07-24T11:59:33.810782Z","shell.execute_reply":"2022-07-24T11:59:33.837355Z"},"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'})\n\ntrain['weather'] = 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-24T11:59:33.839282Z","iopub.execute_input":"2022-07-24T11:59:33.839590Z","iopub.status.idle":"2022-07-24T11:59:33.848053Z","shell.execute_reply.started":"2022-07-24T11:59:33.839560Z","shell.execute_reply":"2022-07-24T11:59:33.846873Z"},"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-24T11:59:33.849340Z","iopub.execute_input":"2022-07-24T11:59:33.850155Z","iopub.status.idle":"2022-07-24T11:59:35.059815Z","shell.execute_reply.started":"2022-07-24T11:59:33.850106Z","shell.execute_reply":"2022-07-24T11:59:35.058905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=15)\nsns.displot(train['count'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:35.060858Z","iopub.execute_input":"2022-07-24T11:59:35.061113Z","iopub.status.idle":"2022-07-24T11:59:35.363425Z","shell.execute_reply.started":"2022-07-24T11:59:35.061086Z","shell.execute_reply":"2022-07-24T11:59:35.362374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(np.log(train['count']))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:35.366840Z","iopub.execute_input":"2022-07-24T11:59:35.367145Z","iopub.status.idle":"2022-07-24T11:59:35.685315Z","shell.execute_reply.started":"2022-07-24T11:59:35.367117Z","shell.execute_reply":"2022-07-24T11:59:35.683891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Step1 : m행 n열 Figure 준비하기","metadata":{}},{"cell_type":"code","source":"mpl.rc('font', size=14)\nmpl.rc('axes', titlesize=15)\nfigure, axes = plt.subplots(nrows=3, ncols=2)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:35.687460Z","iopub.execute_input":"2022-07-24T11:59:35.687905Z","iopub.status.idle":"2022-07-24T11:59:36.028030Z","shell.execute_reply.started":"2022-07-24T11:59:35.687870Z","shell.execute_reply":"2022-07-24T11:59:36.027429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,9)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:36.033856Z","iopub.execute_input":"2022-07-24T11:59:36.034815Z","iopub.status.idle":"2022-07-24T11:59:36.495301Z","shell.execute_reply.started":"2022-07-24T11:59:36.034791Z","shell.execute_reply":"2022-07-24T11:59:36.493698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axes","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:36.496650Z","iopub.execute_input":"2022-07-24T11:59:36.496896Z","iopub.status.idle":"2022-07-24T11:59:36.504449Z","shell.execute_reply.started":"2022-07-24T11:59:36.496874Z","shell.execute_reply":"2022-07-24T11:59:36.503628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:36.505830Z","iopub.execute_input":"2022-07-24T11:59:36.506377Z","iopub.status.idle":"2022-07-24T11:59:36.904063Z","shell.execute_reply.started":"2022-07-24T11:59:36.506345Z","shell.execute_reply":"2022-07-24T11:59:36.902933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,9)\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])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:36.905770Z","iopub.execute_input":"2022-07-24T11:59:36.906096Z","iopub.status.idle":"2022-07-24T11:59:39.688711Z","shell.execute_reply.started":"2022-07-24T11:59:36.906062Z","shell.execute_reply":"2022-07-24T11:59:39.687641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,9)\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\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')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:39.690056Z","iopub.execute_input":"2022-07-24T11:59:39.690268Z","iopub.status.idle":"2022-07-24T11:59:42.285929Z","shell.execute_reply.started":"2022-07-24T11:59:39.690247Z","shell.execute_reply":"2022-07-24T11:59:42.285042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,9)\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\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\naxes[1,0].tick_params(axis='x', labelrotation=90)\naxes[1,1].tick_params(axis='x', labelrotation=90)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:42.286976Z","iopub.execute_input":"2022-07-24T11:59:42.287204Z","iopub.status.idle":"2022-07-24T11:59:46.651243Z","shell.execute_reply.started":"2022-07-24T11:59:42.287181Z","shell.execute_reply":"2022-07-24T11:59:46.650164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 박스플롯\n- Box Plot은 범주형 데이터에 따른 수치형 데이터 정보를 나타내는 그래프","metadata":{}},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,10)\n\nsns.boxplot(x='season', y='count', data=train, ax=axes[0,0])\nsns.boxplot(x='weather', y='count', data=train, ax=axes[0,1])\nsns.boxplot(x='holiday', y='count', data=train, ax=axes[1,0])\nsns.boxplot(x='workingday', y='count', data=train, ax=axes[1,1])\n\naxes[0,0].set(title='Box Plot On Count Across Season')\naxes[0,1].set(title='Box Plot On Count Across Weather')\naxes[1,0].set(title='Box Plot On Count Across Holiday')\naxes[1,1].set(title='Box Plot On Count Across Working Day')\n\naxes[0,1].tick_params(axis='x', labelrotation=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:46.652794Z","iopub.execute_input":"2022-07-24T11:59:46.653172Z","iopub.status.idle":"2022-07-24T11:59:47.276949Z","shell.execute_reply.started":"2022-07-24T11:59:46.653135Z","shell.execute_reply":"2022-07-24T11:59:47.275912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 포인트플롯(pointplot)","metadata":{}},{"cell_type":"code","source":"mpl.rc('font', size=11)\nfigure, axes = plt.subplots(nrows=5)\nfigure.set_size_inches(12,18)\n\nsns.pointplot(x='hour', y='count', data=train, hue='workingday', ax=axes[0])\nsns.pointplot(x='hour', y='count', data=train, hue='holiday', ax=axes[1])\nsns.pointplot(x='hour', y='count', data=train, hue='weekday', ax=axes[2])\nsns.pointplot(x='hour', y='count', data=train, hue='season', ax=axes[3])\nsns.pointplot(x='hour', y='count', data=train, hue='weather', ax=axes[4])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T11:59:47.278533Z","iopub.execute_input":"2022-07-24T11:59:47.278843Z","iopub.status.idle":"2022-07-24T12:00:01.792135Z","shell.execute_reply.started":"2022-07-24T11:59:47.278811Z","shell.execute_reply":"2022-07-24T12:00:01.791238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 회귀선을 포함한 산점도 그래프(regplot)","metadata":{}},{"cell_type":"code","source":"mpl.rc('font', size=15)\nfigure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(7,6)\n\nsns.regplot(x='temp', y='count', data=train, ax=axes[0,0],\n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='atemp', y='count', data=train, ax=axes[0,1],\n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='windspeed', y='count', data=train, ax=axes[1,0],\n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='humidity', y='count', data=train, ax=axes[1,1],\n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:01.793785Z","iopub.execute_input":"2022-07-24T12:00:01.794417Z","iopub.status.idle":"2022-07-24T12:00:07.567615Z","shell.execute_reply.started":"2022-07-24T12:00:01.794339Z","shell.execute_reply":"2022-07-24T12:00:07.566364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 히트맵","metadata":{}},{"cell_type":"code","source":"train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.569344Z","iopub.execute_input":"2022-07-24T12:00:07.569699Z","iopub.status.idle":"2022-07-24T12:00:07.588663Z","shell.execute_reply.started":"2022-07-24T12:00:07.569670Z","shell.execute_reply":"2022-07-24T12:00:07.587448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrMat = train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()\nfig, ax = plt.subplots()\nfig.set_size_inches(10,10)\nsns.heatmap(corrMat, annot=True)\nax.set(title='Heatmap of Numerical Data')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.590294Z","iopub.execute_input":"2022-07-24T12:00:07.590590Z","iopub.status.idle":"2022-07-24T12:00:07.845050Z","shell.execute_reply.started":"2022-07-24T12:00:07.590566Z","shell.execute_reply":"2022-07-24T12:00:07.844337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6.4 베이스라인 모델","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ndata_path = '/kaggle/input/bike-sharing-demand/'\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-24T12:00:07.846575Z","iopub.execute_input":"2022-07-24T12:00:07.847177Z","iopub.status.idle":"2022-07-24T12:00:07.885942Z","shell.execute_reply.started":"2022-07-24T12:00:07.847143Z","shell.execute_reply":"2022-07-24T12:00:07.884922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4.1 피처 엔지니어링\n- 데이터를 변환하는 작업\n- 훈련 데이터와 테스트 데이터에 공통으로 반영하기 때문에, 두 데이터를 합쳤다가 끝나면 다시 나눠준다","metadata":{}},{"cell_type":"markdown","source":"이상치 제거","metadata":{}},{"cell_type":"code","source":"train = train[train['weather'] != 4]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.887313Z","iopub.execute_input":"2022-07-24T12:00:07.889348Z","iopub.status.idle":"2022-07-24T12:00:07.896784Z","shell.execute_reply.started":"2022-07-24T12:00:07.889311Z","shell.execute_reply":"2022-07-24T12:00:07.895659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"데이터 합치기","metadata":{}},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.898355Z","iopub.execute_input":"2022-07-24T12:00:07.899304Z","iopub.status.idle":"2022-07-24T12:00:07.909230Z","shell.execute_reply.started":"2022-07-24T12:00:07.899265Z","shell.execute_reply":"2022-07-24T12:00:07.907573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data_temp = pd.concat([train, test])\nall_data_temp","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.910996Z","iopub.execute_input":"2022-07-24T12:00:07.911980Z","iopub.status.idle":"2022-07-24T12:00:07.937495Z","shell.execute_reply.started":"2022-07-24T12:00:07.911932Z","shell.execute_reply":"2022-07-24T12:00:07.936314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = pd.concat([train, test], ignore_index=True)\nall_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.939517Z","iopub.execute_input":"2022-07-24T12:00:07.939862Z","iopub.status.idle":"2022-07-24T12:00:07.970129Z","shell.execute_reply.started":"2022-07-24T12:00:07.939834Z","shell.execute_reply":"2022-07-24T12:00:07.968548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"파생 피처(변수) 추가","metadata":{}},{"cell_type":"markdown","source":"- 방법1","metadata":{}},{"cell_type":"code","source":"from datetime import datetime\n\nall_data['date'] = all_data['datetime'].apply(lambda x:x.split()[0])\nall_data['year'] = all_data['datetime'].apply(lambda x:x.split()[0].split('-')[0])\nall_data['month'] = all_data['datetime'].apply(lambda x:x.split()[0].split('-')[1])\nall_data['hour'] = all_data['datetime'].apply(lambda x:x.split()[1].split(':')[0])\nall_data['weekday'] = all_data['date'].apply(lambda dateString : datetime.strptime(dateString, '%Y-%m-%d').weekday())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:07.971471Z","iopub.execute_input":"2022-07-24T12:00:07.971848Z","iopub.status.idle":"2022-07-24T12:00:08.155367Z","shell.execute_reply.started":"2022-07-24T12:00:07.971821Z","shell.execute_reply":"2022-07-24T12:00:08.154286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.156617Z","iopub.execute_input":"2022-07-24T12:00:08.156984Z","iopub.status.idle":"2022-07-24T12:00:08.182322Z","shell.execute_reply.started":"2022-07-24T12:00:08.156937Z","shell.execute_reply":"2022-07-24T12:00:08.181355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- 방법2","metadata":{}},{"cell_type":"code","source":"all_data['date'] = pd.to_datetime(all_data['datetime']) ","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.183760Z","iopub.execute_input":"2022-07-24T12:00:08.184294Z","iopub.status.idle":"2022-07-24T12:00:08.200447Z","shell.execute_reply.started":"2022-07-24T12:00:08.184259Z","shell.execute_reply":"2022-07-24T12:00:08.199421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.201787Z","iopub.execute_input":"2022-07-24T12:00:08.202824Z","iopub.status.idle":"2022-07-24T12:00:08.224667Z","shell.execute_reply.started":"2022-07-24T12:00:08.202763Z","shell.execute_reply":"2022-07-24T12:00:08.223609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['year'] = all_data['date'].dt.year\nall_data['month'] = all_data['date'].dt.month\nall_data['hour'] = all_data['date'].dt.hour\nall_data['weekday'] = all_data['date'].dt.weekday","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.225561Z","iopub.execute_input":"2022-07-24T12:00:08.226689Z","iopub.status.idle":"2022-07-24T12:00:08.236963Z","shell.execute_reply.started":"2022-07-24T12:00:08.226663Z","shell.execute_reply":"2022-07-24T12:00:08.235920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.238047Z","iopub.execute_input":"2022-07-24T12:00:08.238302Z","iopub.status.idle":"2022-07-24T12:00:08.268010Z","shell.execute_reply.started":"2022-07-24T12:00:08.238279Z","shell.execute_reply":"2022-07-24T12:00:08.267000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"필요 없는 피처 제거","metadata":{}},{"cell_type":"code","source":"drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']\nall_data = all_data.drop(drop_features, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.269006Z","iopub.execute_input":"2022-07-24T12:00:08.269784Z","iopub.status.idle":"2022-07-24T12:00:08.277238Z","shell.execute_reply.started":"2022-07-24T12:00:08.269758Z","shell.execute_reply":"2022-07-24T12:00:08.275759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.278864Z","iopub.execute_input":"2022-07-24T12:00:08.279825Z","iopub.status.idle":"2022-07-24T12:00:08.303973Z","shell.execute_reply.started":"2022-07-24T12:00:08.279786Z","shell.execute_reply":"2022-07-24T12:00:08.302902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"데이터 나누기","metadata":{}},{"cell_type":"code","source":"X_train = all_data[~pd.isnull(all_data['count'])]\nX_test = all_data[pd.isnull(all_data['count'])]\n\nX_train = X_train.drop(['count'], axis=1)\nX_test = X_test.drop(['count'], axis=1)\n\ny = train['count']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.305209Z","iopub.execute_input":"2022-07-24T12:00:08.305483Z","iopub.status.idle":"2022-07-24T12:00:08.315907Z","shell.execute_reply.started":"2022-07-24T12:00:08.305456Z","shell.execute_reply":"2022-07-24T12:00:08.314922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.317142Z","iopub.execute_input":"2022-07-24T12:00:08.317443Z","iopub.status.idle":"2022-07-24T12:00:08.334463Z","shell.execute_reply.started":"2022-07-24T12:00:08.317416Z","shell.execute_reply":"2022-07-24T12:00:08.333354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4.2 평가지표 계산 함수 작성","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ndef rmsle(y_true, y_pred, convertExp = True):\n    # 지수 변환\n    if convertExp:\n        y_true = np.exp(y_true)\n        y_pred = np.exp(y_pred)\n        \n    # 로그변환 후 결측값을 0으로 변환\n    log_true = np.nan_to_num(np.log(y_true+1))\n    log_pred = np.nan_to_num(np.log(y_pred+1))\n    \n    # RMSLE\n    output = np.sqrt(np.mean((log_true - log_pred) ** 2))\n    return output","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.335638Z","iopub.execute_input":"2022-07-24T12:00:08.335909Z","iopub.status.idle":"2022-07-24T12:00:08.342993Z","shell.execute_reply.started":"2022-07-24T12:00:08.335883Z","shell.execute_reply":"2022-07-24T12:00:08.341915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4.3 모델 훈련","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\nlinear_reg_model = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.344304Z","iopub.execute_input":"2022-07-24T12:00:08.344622Z","iopub.status.idle":"2022-07-24T12:00:08.563410Z","shell.execute_reply.started":"2022-07-24T12:00:08.344599Z","shell.execute_reply":"2022-07-24T12:00:08.562225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_y = np.log(y)\nlinear_reg_model.fit(X_train, log_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.564839Z","iopub.execute_input":"2022-07-24T12:00:08.565218Z","iopub.status.idle":"2022-07-24T12:00:08.863730Z","shell.execute_reply.started":"2022-07-24T12:00:08.565181Z","shell.execute_reply":"2022-07-24T12:00:08.862505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4.4 모델 성능 검증","metadata":{}},{"cell_type":"code","source":"preds = linear_reg_model.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.864939Z","iopub.execute_input":"2022-07-24T12:00:08.865668Z","iopub.status.idle":"2022-07-24T12:00:08.877906Z","shell.execute_reply.started":"2022-07-24T12:00:08.865640Z","shell.execute_reply":"2022-07-24T12:00:08.876887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'선형 회귀의 RMSLE 값 : {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.879086Z","iopub.execute_input":"2022-07-24T12:00:08.883675Z","iopub.status.idle":"2022-07-24T12:00:08.890635Z","shell.execute_reply.started":"2022-07-24T12:00:08.883635Z","shell.execute_reply":"2022-07-24T12:00:08.889805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4.5 예측 및 결과 제출","metadata":{}},{"cell_type":"code","source":"linearreg_preds = linear_reg_model.predict(X_test) # 테스트 데이터로 예측\n\nsubmission['count'] = np.exp(linearreg_preds) # 지수변환\nsubmission.to_csv('submission.csv', index=False) # 파일로 저장","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.891813Z","iopub.execute_input":"2022-07-24T12:00:08.892534Z","iopub.status.idle":"2022-07-24T12:00:08.933263Z","shell.execute_reply.started":"2022-07-24T12:00:08.892500Z","shell.execute_reply":"2022-07-24T12:00:08.932352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6.5 성능 개선 1 : 릿지 회귀 모델","metadata":{}},{"cell_type":"markdown","source":"모델 생성","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import Ridge\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import metrics\n\nridge_model = Ridge()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.934770Z","iopub.execute_input":"2022-07-24T12:00:08.935347Z","iopub.status.idle":"2022-07-24T12:00:08.940134Z","shell.execute_reply.started":"2022-07-24T12:00:08.935312Z","shell.execute_reply":"2022-07-24T12:00:08.939354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"그리드 서치 객체 생성","metadata":{}},{"cell_type":"code","source":"# 하이퍼 팍라미터 값 목록\nridge_params = {'max_iter':[3000], 'alpha':[0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000]}\n\n# 교차 검증용 평가 함수\nrmsle_scorer = metrics.make_scorer(rmsle, greater_is_better = False)\n\n# 그리드서치(with 릿지) 객체 생성\ngridsearch_ridge_model = GridSearchCV(estimator=ridge_model,\n                                      param_grid=ridge_params,\n                                      scoring=rmsle_scorer,\n                                      cv=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.941589Z","iopub.execute_input":"2022-07-24T12:00:08.942538Z","iopub.status.idle":"2022-07-24T12:00:08.950948Z","shell.execute_reply.started":"2022-07-24T12:00:08.942504Z","shell.execute_reply":"2022-07-24T12:00:08.950164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"그리드서치 수행","metadata":{}},{"cell_type":"code","source":"log_y = np.log(y)\ngridsearch_ridge_model.fit(X_train, log_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:08.954172Z","iopub.execute_input":"2022-07-24T12:00:08.956753Z","iopub.status.idle":"2022-07-24T12:00:09.502033Z","shell.execute_reply.started":"2022-07-24T12:00:08.956718Z","shell.execute_reply":"2022-07-24T12:00:09.501232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(gridsearch_ridge_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:09.503520Z","iopub.execute_input":"2022-07-24T12:00:09.504062Z","iopub.status.idle":"2022-07-24T12:00:09.510958Z","shell.execute_reply.started":"2022-07-24T12:00:09.504028Z","shell.execute_reply":"2022-07-24T12:00:09.510171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = gridsearch_ridge_model.best_estimator_.predict(X_train)\nprint(f'릿지 회귀 RMSLE 값 : {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:09.515371Z","iopub.execute_input":"2022-07-24T12:00:09.517617Z","iopub.status.idle":"2022-07-24T12:00:09.531271Z","shell.execute_reply.started":"2022-07-24T12:00:09.517582Z","shell.execute_reply":"2022-07-24T12:00:09.530442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6.6 성능 개선2 : 라쏘 회귀 모델","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\n\nlasso_model = Lasso()\n\nlasso_alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000])\n\nlasso_params = {'max_iter':[3000], 'alpha':lasso_alpha}\n\ngridsearch_lasso_model = GridSearchCV(estimator=lasso_model,\n                                      param_grid=lasso_params,\n                                      scoring=rmsle_scorer,\n                                      cv=5)\n\n# 그리드서치 수행\nlog_y = np.log(y)\ngridsearch_lasso_model.fit(X_train, log_y)\n\nprint(gridsearch_lasso_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:09.535514Z","iopub.execute_input":"2022-07-24T12:00:09.536750Z","iopub.status.idle":"2022-07-24T12:00:11.087945Z","shell.execute_reply.started":"2022-07-24T12:00:09.536683Z","shell.execute_reply":"2022-07-24T12:00:11.087050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.6.2 성능 검증","metadata":{}},{"cell_type":"code","source":"# 예측\npreds = gridsearch_lasso_model.best_estimator_.predict(X_train)\n\n# 평가\nprint(f'{rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:00:11.091958Z","iopub.execute_input":"2022-07-24T12:00:11.094091Z","iopub.status.idle":"2022-07-24T12:00:11.110941Z","shell.execute_reply.started":"2022-07-24T12:00:11.094053Z","shell.execute_reply":"2022-07-24T12:00:11.109658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6.3 성능 개선3 : 랜덤 포레스트 회귀 모델","metadata":{}},{"cell_type":"markdown","source":"### 6.7.1 하이퍼파라미터 최적화(모델 훈련)","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBRegressor\nxgb_model = XGBRegressor(n_estimators=100, max_depth=5)\nxgb_model.fit(X_train, log_y)\nxgb_preds = xgb_model.predict(X_train)\nprint(f'{rmsle(log_y, xgb_preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:05:32.968537Z","iopub.execute_input":"2022-07-24T12:05:32.968901Z","iopub.status.idle":"2022-07-24T12:05:33.519194Z","shell.execute_reply.started":"2022-07-24T12:05:32.968872Z","shell.execute_reply":"2022-07-24T12:05:33.518437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_preds = xgb_model.predict(X_test)\nsubmission['count'] = np.exp(xgb_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:06:21.579942Z","iopub.execute_input":"2022-07-24T12:06:21.580306Z","iopub.status.idle":"2022-07-24T12:06:21.610951Z","shell.execute_reply.started":"2022-07-24T12:06:21.580278Z","shell.execute_reply":"2022-07-24T12:06:21.610110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrandomforest_model = RandomForestRegressor()\n\nrf_params = {'random_state':[42], 'n_estimators':[100, 120, 140, 500, 1000], 'max_depth':[3,5,10]}\n\ngridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,\n                                              param_grid=rf_params,\n                                              scoring=rmsle_scorer,\n                                              cv=5)\n\nlog_y = np.log(y)\ngridsearch_random_forest_model.fit(X_train, log_y)\nprint(gridsearch_random_forest_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:12:13.867194Z","iopub.execute_input":"2022-07-24T12:12:13.867611Z","iopub.status.idle":"2022-07-24T12:16:53.091724Z","shell.execute_reply.started":"2022-07-24T12:12:13.867579Z","shell.execute_reply":"2022-07-24T12:16:53.090462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.7.2 모델 성능 검증","metadata":{}},{"cell_type":"code","source":"preds = gridsearch_random_forest_model.best_estimator_.predict(X_train)\n\nprint(f'{rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:17:04.990885Z","iopub.execute_input":"2022-07-24T12:17:04.991275Z","iopub.status.idle":"2022-07-24T12:17:05.671704Z","shell.execute_reply.started":"2022-07-24T12:17:04.991245Z","shell.execute_reply":"2022-07-24T12:17:05.669555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.7.3 예측 및 결과 제출","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nrandomforest_preds = gridsearch_random_forest_model.best_estimator_.predict(X_test)\n\nfigure, axes = plt.subplots(ncols=2)\nfigure.set_size_inches(10,4)\n\nsns.histplot(y, bins=50, ax=axes[0])\naxes[0].set_title('Train Data Distribution')\nsns.histplot(np.exp(randomforest_preds), bins=50, ax=axes[1])\naxes[1].set_title('Predicted Test Data Distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:01:00.661048Z","iopub.execute_input":"2022-07-24T12:01:00.662182Z","iopub.status.idle":"2022-07-24T12:01:01.276352Z","shell.execute_reply.started":"2022-07-24T12:01:00.662145Z","shell.execute_reply":"2022-07-24T12:01:01.275621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['count'] = np.exp(randomforest_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:17:30.528024Z","iopub.execute_input":"2022-07-24T12:17:30.528318Z","iopub.status.idle":"2022-07-24T12:17:30.554890Z","shell.execute_reply.started":"2022-07-24T12:17:30.528295Z","shell.execute_reply":"2022-07-24T12:17:30.553739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:24:36.391513Z","iopub.execute_input":"2022-07-24T12:24:36.391830Z","iopub.status.idle":"2022-07-24T12:24:36.405441Z","shell.execute_reply.started":"2022-07-24T12:24:36.391805Z","shell.execute_reply":"2022-07-24T12:24:36.404539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:24:41.740468Z","iopub.execute_input":"2022-07-24T12:24:41.740896Z","iopub.status.idle":"2022-07-24T12:24:41.756376Z","shell.execute_reply.started":"2022-07-24T12:24:41.740867Z","shell.execute_reply":"2022-07-24T12:24:41.755191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['year'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:25:00.950379Z","iopub.execute_input":"2022-07-24T12:25:00.951110Z","iopub.status.idle":"2022-07-24T12:25:00.961979Z","shell.execute_reply.started":"2022-07-24T12:25:00.951069Z","shell.execute_reply":"2022-07-24T12:25:00.960483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test['year'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:25:10.251141Z","iopub.execute_input":"2022-07-24T12:25:10.251406Z","iopub.status.idle":"2022-07-24T12:25:10.258240Z","shell.execute_reply.started":"2022-07-24T12:25:10.251366Z","shell.execute_reply":"2022-07-24T12:25:10.257235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:26:28.362404Z","iopub.execute_input":"2022-07-24T12:26:28.362762Z","iopub.status.idle":"2022-07-24T12:26:28.382463Z","shell.execute_reply.started":"2022-07-24T12:26:28.362734Z","shell.execute_reply":"2022-07-24T12:26:28.381218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(X_train.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:26:40.306171Z","iopub.execute_input":"2022-07-24T12:26:40.307401Z","iopub.status.idle":"2022-07-24T12:26:40.602292Z","shell.execute_reply.started":"2022-07-24T12:26:40.307322Z","shell.execute_reply":"2022-07-24T12:26:40.601238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train2 = X_train.drop(columns = ['weekday', 'atemp', 'humidity'])\nX_test2 = X_test.drop(columns = ['weekday', 'atemp', 'humidity'])\nX_train2","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:30:40.675797Z","iopub.execute_input":"2022-07-24T12:30:40.676149Z","iopub.status.idle":"2022-07-24T12:30:40.692783Z","shell.execute_reply.started":"2022-07-24T12:30:40.676125Z","shell.execute_reply":"2022-07-24T12:30:40.692141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrandomforest_model = RandomForestRegressor()\n\nrf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}\n\ngridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,\n                                              param_grid=rf_params,\n                                              scoring=rmsle_scorer,\n                                              cv=5)\n\nlog_y = np.log(y)\ngridsearch_random_forest_model.fit(X_train2, log_y)\nprint(gridsearch_random_forest_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:29:23.741589Z","iopub.execute_input":"2022-07-24T12:29:23.741916Z","iopub.status.idle":"2022-07-24T12:29:47.031076Z","shell.execute_reply.started":"2022-07-24T12:29:23.741893Z","shell.execute_reply":"2022-07-24T12:29:47.030010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"randomforest_preds = gridsearch_random_forest_model.best_estimator_.predict(X_test2)\nsubmission['count'] = np.exp(randomforest_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:30:48.074765Z","iopub.execute_input":"2022-07-24T12:30:48.075095Z","iopub.status.idle":"2022-07-24T12:30:48.238881Z","shell.execute_reply.started":"2022-07-24T12:30:48.075070Z","shell.execute_reply":"2022-07-24T12:30:48.237795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}