{"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-31T14:15:47.958164Z","iopub.execute_input":"2022-07-31T14:15:47.959121Z","iopub.status.idle":"2022-07-31T14:15:47.975130Z","shell.execute_reply.started":"2022-07-31T14:15:47.959021Z","shell.execute_reply":"2022-07-31T14:15:47.974349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpi = pd.read_csv('../input/housing-affordability-in-canada/CPI-inflation-by-region-1914-202.csv')\nhpi = pd.read_csv('../input/housing-affordability-in-canada/HPI 1981-2022 by regions.csv')\nrates = pd.read_csv('../input/housing-affordability-in-canada/Interest and mortgage rates 1951-2022.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:15:52.654597Z","iopub.execute_input":"2022-07-31T14:15:52.655308Z","iopub.status.idle":"2022-07-31T14:15:52.802737Z","shell.execute_reply.started":"2022-07-31T14:15:52.655268Z","shell.execute_reply":"2022-07-31T14:15:52.801523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hpi = hpi.iloc[1: , :]\n#hpi = hpi.iloc[:1455 , :]\n#hpi[\"Month-Year\"].iloc[0:1198] = hpi[\"Month-Year\"].iloc[0:1198].astype(str).str.slice(4, 6)\n#hpi[\"Month-Year\"].iloc[1198:1455] = hpi[\"Month-Year\"].iloc[1198:1455].astype(str).str.slice(0, 2)\n#hpi['Month-Year'] = hpi['Month-Year'].astype(str)\n#hpi['Month-Year'] = hpi['Month-Year'].str.extract('(d+)', expand=False)\n#numeric_filter = filter(str.isdigit, hpi['Month-Year'])\n#hpi['Month-Year'] = \"\".join(numeric_filter)\n#dataTypeSeries = hpi.dtypes\n#dataTypeSeries\nhpi[\"Month-Year\"] = hpi[\"Month-Year\"].astype(str)\nhpi[\"Month-Year\"] = hpi[\"Month-Year\"].map(lambda x: x.lstrip(' -ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz').rstrip(' -ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'))\n#h = hpi[\"Month-Year\"].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:00.094881Z","iopub.execute_input":"2022-07-31T14:16:00.095945Z","iopub.status.idle":"2022-07-31T14:16:00.106479Z","shell.execute_reply.started":"2022-07-31T14:16:00.095892Z","shell.execute_reply":"2022-07-31T14:16:00.105339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#hpi[['Canada', 'Newfoundland and Labrador', 'Prince Edward Island', 'Nova Scotia', 'New Brunswick', 'Quebec', 'Ontario', 'Manitoba', 'Saskatchewan', 'Alberta', 'British Columbia']] = hpi[['Canada', 'Newfoundland and Labrador', 'Prince Edward Island', 'Nova Scotia', 'New Brunswick', 'Quebec', 'Ontario', 'Manitoba', 'Saskatchewan', 'Alberta', 'British Columbia']].astype(str)\n#hpi['Canada'] = hpi['Canada'].map(lambda x: x.rstrip('E'))\n#hpi['Newfoundland and Labrador'] = hpi['Newfoundland and Labrador'].map(lambda x: x.rstrip('E'))\n#hpi['Prince Edward Island'] = hpi['Prince Edward Island'].map(lambda x: x.rstrip('E'))\n#hpi['Nova Scotia'] = hpi['Nova Scotia'].map(lambda x: x.rstrip('E'))\n#hpi['New Brunswick'] = hpi['New Brunswick'].map(lambda x: x.rstrip('E'))\n#hpi['Quebec'] = hpi['Quebec'].map(lambda x: x.rstrip('E'))\n#hpi['Ontario'] = hpi['Ontario'].map(lambda x: x.rstrip('E'))\n#hpi['Manitoba'] = hpi['Manitoba'].map(lambda x: x.rstrip('E'))\n#hpi['Saskatchewan'] = hpi['Saskatchewan'].map(lambda x: x.rstrip('E'))\n#hpi['Alberta'] = hpi['Alberta'].map(lambda x: x.rstrip('E'))\n#hpi['British Columbia'] = hpi['British Columbia'].map(lambda x: x.rstrip('E'))\n#hpi[['Canada', 'Newfoundland and Labrador', 'Prince Edward Island', 'Nova Scotia', 'New Brunswick', 'Quebec', 'Ontario', 'Manitoba', 'Saskatchewan', 'Alberta', 'British Columbia']] = hpi[['Canada', 'Newfoundland and Labrador', 'Prince Edward Island', 'Nova Scotia', 'New Brunswick', 'Quebec', 'Ontario', 'Manitoba', 'Saskatchewan', 'Alberta', 'British Columbia']].astype(float)\n#hpi = hpi.drop(columns=['Atlantic Region', 'Québec, Quebec', 'Sherbrooke, Quebec', 'COORDINATE'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:05.728965Z","iopub.execute_input":"2022-07-31T14:16:05.729802Z","iopub.status.idle":"2022-07-31T14:16:05.814361Z","shell.execute_reply.started":"2022-07-31T14:16:05.729756Z","shell.execute_reply":"2022-07-31T14:16:05.812481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cpi[\"REF_DATE\"] = cpi[\"REF_DATE\"].astype(str).str.slice(2, 4)\n#cpi = cpi.iloc[27: , :]\n#cpi = cpi.drop(columns=['DGUID', 'UOM_ID', 'VECTOR', 'COORDINATE'])\ncpiAll = cpi.loc[cpi['Products and product groups'] == 'All-items']\ncpiAllOne = cpi.loc[cpi['Products and product groups'] == 'All-items (1992=100)']\ncpiGS = cpi.loc[cpi['Products and product groups'] == 'Goods and services']\ncpiMIC = cpi.loc[cpi['Products and product groups'] == 'Mortgage interest cost']\ncpiHousing = cpi.loc[cpi['Products and product groups'] == 'Housing (1986 definition)']\ncpi = pd.concat([cpiAll, cpiAllOne, cpiGS, cpiMIC, cpiHousing])\ncpiCAN = cpi.loc[cpi['GEO'] == 'Canada']\ncpiON = cpi.loc[cpi['GEO'] == 'Ontario']\ncpiNFL = cpi.loc[cpi['GEO'] == 'Newfoundland and Labrador']\ncpiPEI = cpi.loc[cpi['GEO'] == 'Prince Edward Island']\ncpiNS = cpi.loc[cpi['GEO'] == 'Nova Scotia']\ncpiNB = cpi.loc[cpi['GEO'] == 'Nova Brunswick']\ncpiQB = cpi.loc[cpi['GEO'] == 'Quebec']\ncpiMB = cpiNB = cpi.loc[cpi['GEO'] == 'Manitoba']\ncpiSK = cpiNB = cpi.loc[cpi['GEO'] == 'Saskatchewan']\ncpiAB = cpiNB = cpi.loc[cpi['GEO'] == 'Alberta']\ncpiBC = cpi.loc[cpi['GEO'] == 'British Columbia']\ncpiYK = cpi.loc[cpi['GEO'] =='Whitehorse, Yukon']\ncpiNWT = cpi.loc[cpi['GEO'] =='Yellowknife, Northwest Territories']\ncpiNU = cpi.loc[cpi['GEO'] =='Iqaluit, Nunavut']\n#h = cpi['GEO'].unique()\ncpi = pd.concat([cpiCAN, cpiON, cpiGS, cpiNFL, cpiPEI, cpiNS, cpiNB, cpiQB, cpiMB, cpiSK, cpiAB, cpiBC, cpiYK, cpiNWT, cpiNU])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:12.285644Z","iopub.execute_input":"2022-07-31T14:16:12.286095Z","iopub.status.idle":"2022-07-31T14:16:12.355421Z","shell.execute_reply.started":"2022-07-31T14:16:12.286062Z","shell.execute_reply":"2022-07-31T14:16:12.354240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rates['Date'] = rates['Date'].str[:4]\nrates = rates.groupby('Date').mean()\nrates.insert(0, 'Year', range(1951, 1951 + len(rates)))\nrates\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hpi['Month-Year'] = pd.to_numeric(hpi['Month-Year'], errors='coerce')\n#h = hpi['Month-Year'].tolist()\nhpi['Month-Year'].loc[hpi['Month-Year'] > 80] = hpi['Month-Year'].loc[hpi['Month-Year'] > 80].astype(float) + 1900\nhpi['Month-Year'].loc[hpi['Month-Year'] < 80] = hpi['Month-Year'].loc[hpi['Month-Year'] < 80].astype(float) + 2000\n#hpi.loc[hpi['Month-Year'] < 80] + 2000\n#hpi['Month-Year'] = hpi['Month-Year'] + 1900\nhpi['Month-Year'] = hpi['Month-Year'].fillna(0)\nhpi['Month-Year'] = hpi['Month-Year'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:18.774467Z","iopub.execute_input":"2022-07-31T14:16:18.774958Z","iopub.status.idle":"2022-07-31T14:16:18.829392Z","shell.execute_reply.started":"2022-07-31T14:16:18.774919Z","shell.execute_reply":"2022-07-31T14:16:18.828006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hpi","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#aggFunc = {'Canada': 'sum'}\nhpi = hpi.loc[(hpi['Type'] == 'House and Land')]\nhpi = hpi.groupby('year', as_index=False).mean()\n#cpi = cpi.groupby(\"REF_DATE\").mean()\n#data = pd.concat([hpi, cpi])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:29.409780Z","iopub.execute_input":"2022-07-31T14:16:29.410242Z","iopub.status.idle":"2022-07-31T14:16:29.424385Z","shell.execute_reply.started":"2022-07-31T14:16:29.410202Z","shell.execute_reply":"2022-07-31T14:16:29.423388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\nrates.replace(0, np.nan, inplace=True)\nMI = plt.scatter(rates['Mortgage Rate'], rates['Interest Rate'])\n\n\nMI","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ncpican = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1980) & (cpi['GEO'] == 'Canada')]\nhpican = hpi['Canada'].loc[hpi['year'] < 2022]\ngraph1 = plt.scatter(hpican, cpican)\nplt.title(\"Canada HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph1\ncorr = np.corrcoef(hpican, cpican)\nprint(corr)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:16:37.478643Z","iopub.execute_input":"2022-07-31T14:16:37.479058Z","iopub.status.idle":"2022-07-31T14:16:37.710955Z","shell.execute_reply.started":"2022-07-31T14:16:37.479026Z","shell.execute_reply":"2022-07-31T14:16:37.709799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpican = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1980) & (cpi['GEO'] == 'Canada')]\nhpican = hpi['Canada'].loc[hpi['year'] < 2022]\ngraph1 = plt.scatter(hpican, cpican)\nplt.title(\"Canada HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph1\ncorr = np.corrcoef(hpican, cpican)\nprint(corr)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 95 pei, 86 everyone else\ncpinfl = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Newfoundland and Labrador')]\nhpinfl = hpi['Newfoundland and Labrador'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph2 = plt.scatter(hpinfl, cpinfl)\nplt.title(\"Newfoundland and Labrador HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph2\ncorr = np.corrcoef(hpinfl, cpinfl)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpipei = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1994) & (cpi['GEO'] == 'Prince Edward Island')]\nhpipei = hpi['Newfoundland and Labrador'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1994)]\ngraph3 = plt.scatter(hpipei, cpipei)\nplt.title(\"Prince Edward Island HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph3\ncorr = np.corrcoef(hpipei, cpipei)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpins = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Nova Scotia')]\nhpins = hpi['Nova Scotia'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph4 = plt.scatter(hpins, cpins)\nplt.title(\"Nova Scotia HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph4\ncorr = np.corrcoef(hpins, cpins)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'New Brunswick')]\nhpiregion = hpi['New Brunswick'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"New Brunswick HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Quebec')]\nhpiregion = hpi['Quebec'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"Quebec HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Ontario')]\nhpiregion = hpi['Ontario '].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"Ontario HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:17:19.123481Z","iopub.execute_input":"2022-07-31T14:17:19.123975Z","iopub.status.idle":"2022-07-31T14:17:19.347118Z","shell.execute_reply.started":"2022-07-31T14:17:19.123937Z","shell.execute_reply":"2022-07-31T14:17:19.345812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Manitoba')]\nhpiregion = hpi['Manitoba'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"Manitoba HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Saskatchewan')]\nhpiregion = hpi['Saskatchewan'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"Saskatchewan HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'Alberta')]\nhpiregion = hpi['Alberta'].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"Alberta HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cpiregion = cpi['CPI'].loc[(cpi['Products and product groups'] == 'All-items') & (cpi['REF_DATE'] > 1985) & (cpi['GEO'] == 'British Columbia')]\nhpiregion = hpi['British Columbia '].loc[(hpi['year'] < 2022) & (hpi['year'] > 1985)]\ngraph = plt.scatter(hpiregion, cpiregion)\nplt.title(\"British Columbia HPI vs. CPI\")\nplt.xlabel(\"HPI\")\nplt.ylabel(\"CPI\")\ngraph\ncorr = np.corrcoef(hpiregion, cpiregion)\nprint(corr)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:17:10.239451Z","iopub.execute_input":"2022-07-31T14:17:10.240730Z","iopub.status.idle":"2022-07-31T14:17:10.445958Z","shell.execute_reply.started":"2022-07-31T14:17:10.240671Z","shell.execute_reply":"2022-07-31T14:17:10.444696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hpi['British Columbia']\nfor col in hpi.columns:\n    print(col)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}