diff --git a/House_Price_Prediction/California.ipynb b/House_Price_Prediction/California.ipynb new file mode 100644 index 0000000..a648a25 --- /dev/null +++ b/House_Price_Prediction/California.ipynb @@ -0,0 +1,414 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "594c4a2d", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "from sklearn import linear_model\n", + "from sklearn.model_selection import train_test_split" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fca5993b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'data': array([[ 8.3252 , 41. , 6.98412698, ..., 2.55555556,\n", + " 37.88 , -122.23 ],\n", + " [ 8.3014 , 21. , 6.23813708, ..., 2.10984183,\n", + " 37.86 , -122.22 ],\n", + " [ 7.2574 , 52. , 8.28813559, ..., 2.80225989,\n", + " 37.85 , -122.24 ],\n", + " ...,\n", + " [ 1.7 , 17. , 5.20554273, ..., 2.3256351 ,\n", + " 39.43 , -121.22 ],\n", + " [ 1.8672 , 18. , 5.32951289, ..., 2.12320917,\n", + " 39.43 , -121.32 ],\n", + " [ 2.3886 , 16. , 5.25471698, ..., 2.61698113,\n", + " 39.37 , -121.24 ]]), 'target': array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894]), 'frame': None, 'target_names': ['MedHouseVal'], 'feature_names': ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude'], 'DESCR': '.. _california_housing_dataset:\\n\\nCalifornia Housing dataset\\n--------------------------\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 20640\\n\\n :Number of Attributes: 8 numeric, predictive attributes and the target\\n\\n :Attribute Information:\\n - MedInc median income in block group\\n - HouseAge median house age in block group\\n - AveRooms average number of rooms per household\\n - AveBedrms average number of bedrooms per household\\n - Population block group population\\n - AveOccup average number of household members\\n - Latitude block group latitude\\n - Longitude block group longitude\\n\\n :Missing Attribute Values: None\\n\\nThis dataset was obtained from the StatLib repository.\\nhttps://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html\\n\\nThe target variable is the median house value for California districts,\\nexpressed in hundreds of thousands of dollars ($100,000).\\n\\nThis dataset was derived from the 1990 U.S. census, using one row per census\\nblock group. A block group is the smallest geographical unit for which the U.S.\\nCensus Bureau publishes sample data (a block group typically has a population\\nof 600 to 3,000 people).\\n\\nA household is a group of people residing within a home. Since the average\\nnumber of rooms and bedrooms in this dataset are provided per household, these\\ncolumns may take surprisingly large values for block groups with few households\\nand many empty houses, such as vacation resorts.\\n\\nIt can be downloaded/loaded using the\\n:func:`sklearn.datasets.fetch_california_housing` function.\\n\\n.. topic:: References\\n\\n - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\\n Statistics and Probability Letters, 33 (1997) 291-297\\n'}\n" + ] + } + ], + "source": [ + "from sklearn.datasets import fetch_california_housing\n", + "housing = fetch_california_housing()\n", + "print(housing)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7b4a4ea3", + "metadata": {}, + "outputs": [], + "source": [ + "#transforming dataset to data frame\n", + "#feature_names --> column names\n", + "#data --> required data or independent variable x\n", + "#target --> price of house or dependent variable y\n", + "\n", + "df_x = pd.DataFrame(housing.data, columns = housing.feature_names)\n", + "df_y = pd.DataFrame(housing.target)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "406c5014", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
MedIncHouseAgeAveRoomsAveBedrmsPopulationAveOccupLatitudeLongitude
count20640.00000020640.00000020640.00000020640.00000020640.00000020640.00000020640.00000020640.000000
mean3.87067128.6394865.4290001.0966751425.4767443.07065535.631861-119.569704
std1.89982212.5855582.4741730.4739111132.46212210.3860502.1359522.003532
min0.4999001.0000000.8461540.3333333.0000000.69230832.540000-124.350000
25%2.56340018.0000004.4407161.006079787.0000002.42974133.930000-121.800000
50%3.53480029.0000005.2291291.0487801166.0000002.81811634.260000-118.490000
75%4.74325037.0000006.0523811.0995261725.0000003.28226137.710000-118.010000
max15.00010052.000000141.90909134.06666735682.0000001243.33333341.950000-114.310000
\n", + "
" + ], + "text/plain": [ + " MedInc HouseAge AveRooms AveBedrms Population \\\n", + "count 20640.000000 20640.000000 20640.000000 20640.000000 20640.000000 \n", + "mean 3.870671 28.639486 5.429000 1.096675 1425.476744 \n", + "std 1.899822 12.585558 2.474173 0.473911 1132.462122 \n", + "min 0.499900 1.000000 0.846154 0.333333 3.000000 \n", + "25% 2.563400 18.000000 4.440716 1.006079 787.000000 \n", + "50% 3.534800 29.000000 5.229129 1.048780 1166.000000 \n", + "75% 4.743250 37.000000 6.052381 1.099526 1725.000000 \n", + "max 15.000100 52.000000 141.909091 34.066667 35682.000000 \n", + "\n", + " AveOccup Latitude Longitude \n", + "count 20640.000000 20640.000000 20640.000000 \n", + "mean 3.070655 35.631861 -119.569704 \n", + "std 10.386050 2.135952 2.003532 \n", + "min 0.692308 32.540000 -124.350000 \n", + "25% 2.429741 33.930000 -121.800000 \n", + "50% 2.818116 34.260000 -118.490000 \n", + "75% 3.282261 37.710000 -118.010000 \n", + "max 1243.333333 41.950000 -114.310000 " + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Some statistics from dataset\n", + "df_x.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "9942a228", + "metadata": {}, + "outputs": [], + "source": [ + "#Initializing Linear Regression\n", + "reg = linear_model.LinearRegression()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "462e484f", + "metadata": {}, + "outputs": [], + "source": [ + "#Splitting for test and train dataset\n", + "x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size = 0.33, random_state = 42)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "15304633", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#training the model\n", + "reg.fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "44addf12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 4.44870466e-01 9.55004561e-03 -1.21991503e-01 7.79144696e-01\n", + " -7.68990808e-08 -3.29948505e-03 -4.19131153e-01 -4.34103468e-01]]\n" + ] + } + ], + "source": [ + "#print coeffecients/ weight for each column\n", + "print(reg.coef_)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "8f9a5744", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.72563462]\n", + " [1.76650223]\n", + " [2.70545812]\n", + " ...\n", + " [1.25803135]\n", + " [1.66673014]\n", + " [2.25826279]]\n" + ] + } + ], + "source": [ + "#print the predictions\n", + "y_pred = reg.predict(x_test)\n", + "print(y_pred)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "7fd0c4b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 0\n", + "20046 0.47700\n", + "3024 0.45800\n", + "15663 5.00001\n", + "20484 2.18600\n", + "9814 2.78000\n", + "... ...\n", + "15316 1.66100\n", + "14772 0.93600\n", + "12870 1.07000\n", + "13476 1.30700\n", + "16123 3.07100\n", + "\n", + "[6812 rows x 1 columns]\n" + ] + } + ], + "source": [ + "#print actual test values\n", + "print(y_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e9a7b3c0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0.536969\n", + "dtype: float64\n" + ] + } + ], + "source": [ + "#check for accuracy (Mean Square Error (MSE))\n", + "\n", + "print(np.mean((y_pred - y_test)**2))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1033235f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.5369686543372465\n" + ] + } + ], + "source": [ + "#check for accuracy using MSE method with help of sklearn\n", + "from sklearn.metrics import mean_squared_error\n", + "print(mean_squared_error(y_pred, y_test))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}