{ "cells": [ { "cell_type": "markdown", "id": "407d4131-b3c5-4b66-a214-baf5ee98aae3", "metadata": {}, "source": [ "### Install" ] }, { "cell_type": "code", "execution_count": 21, "id": "cd0826ea-da42-4fd3-90a9-abc13ed05203", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.11/site-packages (1.4.0)\n", "Requirement already satisfied: numpy<2.0,>=1.19.5 in /opt/conda/lib/python3.11/site-packages (from scikit-learn) (1.26.3)\n", "Requirement already satisfied: scipy>=1.6.0 in /opt/conda/lib/python3.11/site-packages (from scikit-learn) (1.12.0)\n", "Requirement already satisfied: joblib>=1.2.0 in /opt/conda/lib/python3.11/site-packages (from scikit-learn) (1.3.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.11/site-packages (from scikit-learn) (3.2.0)\n" ] } ], "source": [ "!pip install scikit-learn" ] }, { "cell_type": "markdown", "id": "ad1b4816-64f8-427f-8af4-b862d7561bfb", "metadata": {}, "source": [ "### Import california housing dataset\n", "\n", "print out dataset --> in JSON format" ] }, { "cell_type": "code", "execution_count": 22, "id": "9a456fe9-2741-43e3-b3d1-f8f98595a466", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'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 ]]),\n", " 'target': array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894]),\n", " 'frame': None,\n", " 'target_names': ['MedHouseVal'],\n", " 'feature_names': ['MedInc',\n", " 'HouseAge',\n", " 'AveRooms',\n", " 'AveBedrms',\n", " 'Population',\n", " 'AveOccup',\n", " 'Latitude',\n", " 'Longitude'],\n", " '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'}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.datasets import fetch_california_housing\n", "housing = fetch_california_housing()\n", "housing" ] }, { "cell_type": "markdown", "id": "e52ae3d2-8d34-4569-aa24-00269f9f51a0", "metadata": {}, "source": [ "### show keys of dataset\n", "show all the keys of the dataset, to get an overview, what's inside the dataset\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "8911162c-2a9a-43aa-9cad-362dedb43b13", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['data', 'target', 'frame', 'target_names', 'feature_names', 'DESCR'])\n" ] } ], "source": [ "#show the keys of the JSON california housing\n", "print(housing.keys())" ] }, { "cell_type": "markdown", "id": "ac4cc0f9-cc2e-4c90-865a-6731be8964a5", "metadata": {}, "source": [ "### import pandas\n", "convert the dataset from a json format to a pandas dataframe \n", "!import housing.data into the rows and feature_names as column names" ] }, { "cell_type": "code", "execution_count": 24, "id": "19373cf8-4274-4528-a82b-809871ef7008", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | MedInc | \n", "HouseAge | \n", "AveRooms | \n", "AveBedrms | \n", "Population | \n", "AveOccup | \n", "Latitude | \n", "Longitude | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "8.3252 | \n", "41.0 | \n", "6.984127 | \n", "1.023810 | \n", "322.0 | \n", "2.555556 | \n", "37.88 | \n", "-122.23 | \n", "
1 | \n", "8.3014 | \n", "21.0 | \n", "6.238137 | \n", "0.971880 | \n", "2401.0 | \n", "2.109842 | \n", "37.86 | \n", "-122.22 | \n", "
2 | \n", "7.2574 | \n", "52.0 | \n", "8.288136 | \n", "1.073446 | \n", "496.0 | \n", "2.802260 | \n", "37.85 | \n", "-122.24 | \n", "
3 | \n", "5.6431 | \n", "52.0 | \n", "5.817352 | \n", "1.073059 | \n", "558.0 | \n", "2.547945 | \n", "37.85 | \n", "-122.25 | \n", "
4 | \n", "3.8462 | \n", "52.0 | \n", "6.281853 | \n", "1.081081 | \n", "565.0 | \n", "2.181467 | \n", "37.85 | \n", "-122.25 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
20635 | \n", "1.5603 | \n", "25.0 | \n", "5.045455 | \n", "1.133333 | \n", "845.0 | \n", "2.560606 | \n", "39.48 | \n", "-121.09 | \n", "
20636 | \n", "2.5568 | \n", "18.0 | \n", "6.114035 | \n", "1.315789 | \n", "356.0 | \n", "3.122807 | \n", "39.49 | \n", "-121.21 | \n", "
20637 | \n", "1.7000 | \n", "17.0 | \n", "5.205543 | \n", "1.120092 | \n", "1007.0 | \n", "2.325635 | \n", "39.43 | \n", "-121.22 | \n", "
20638 | \n", "1.8672 | \n", "18.0 | \n", "5.329513 | \n", "1.171920 | \n", "741.0 | \n", "2.123209 | \n", "39.43 | \n", "-121.32 | \n", "
20639 | \n", "2.3886 | \n", "16.0 | \n", "5.254717 | \n", "1.162264 | \n", "1387.0 | \n", "2.616981 | \n", "39.37 | \n", "-121.24 | \n", "
20640 rows × 8 columns
\n", "\n", " | MedInc | \n", "HouseAge | \n", "AveRooms | \n", "AveBedrms | \n", "Population | \n", "AveOccup | \n", "Latitude | \n", "Longitude | \n", "MedHouseVal | \n", "
---|---|---|---|---|---|---|---|---|---|
0 | \n", "8.3252 | \n", "41.0 | \n", "6.984127 | \n", "1.023810 | \n", "322.0 | \n", "2.555556 | \n", "37.88 | \n", "-122.23 | \n", "4.526 | \n", "
1 | \n", "8.3014 | \n", "21.0 | \n", "6.238137 | \n", "0.971880 | \n", "2401.0 | \n", "2.109842 | \n", "37.86 | \n", "-122.22 | \n", "3.585 | \n", "
2 | \n", "7.2574 | \n", "52.0 | \n", "8.288136 | \n", "1.073446 | \n", "496.0 | \n", "2.802260 | \n", "37.85 | \n", "-122.24 | \n", "3.521 | \n", "
3 | \n", "5.6431 | \n", "52.0 | \n", "5.817352 | \n", "1.073059 | \n", "558.0 | \n", "2.547945 | \n", "37.85 | \n", "-122.25 | \n", "3.413 | \n", "
4 | \n", "3.8462 | \n", "52.0 | \n", "6.281853 | \n", "1.081081 | \n", "565.0 | \n", "2.181467 | \n", "37.85 | \n", "-122.25 | \n", "3.422 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
20635 | \n", "1.5603 | \n", "25.0 | \n", "5.045455 | \n", "1.133333 | \n", "845.0 | \n", "2.560606 | \n", "39.48 | \n", "-121.09 | \n", "0.781 | \n", "
20636 | \n", "2.5568 | \n", "18.0 | \n", "6.114035 | \n", "1.315789 | \n", "356.0 | \n", "3.122807 | \n", "39.49 | \n", "-121.21 | \n", "0.771 | \n", "
20637 | \n", "1.7000 | \n", "17.0 | \n", "5.205543 | \n", "1.120092 | \n", "1007.0 | \n", "2.325635 | \n", "39.43 | \n", "-121.22 | \n", "0.923 | \n", "
20638 | \n", "1.8672 | \n", "18.0 | \n", "5.329513 | \n", "1.171920 | \n", "741.0 | \n", "2.123209 | \n", "39.43 | \n", "-121.32 | \n", "0.847 | \n", "
20639 | \n", "2.3886 | \n", "16.0 | \n", "5.254717 | \n", "1.162264 | \n", "1387.0 | \n", "2.616981 | \n", "39.37 | \n", "-121.24 | \n", "0.894 | \n", "
20640 rows × 9 columns
\n", "\n", " | AveRooms | \n", "AveBedrms | \n", "AveOccup | \n", "Population | \n", "
---|---|---|---|---|
count | \n", "20640.000000 | \n", "20640.000000 | \n", "20640.000000 | \n", "20640.000000 | \n", "
mean | \n", "5.429000 | \n", "1.096675 | \n", "3.070655 | \n", "1425.476744 | \n", "
std | \n", "2.474173 | \n", "0.473911 | \n", "10.386050 | \n", "1132.462122 | \n", "
min | \n", "0.846154 | \n", "0.333333 | \n", "0.692308 | \n", "3.000000 | \n", "
25% | \n", "4.440716 | \n", "1.006079 | \n", "2.429741 | \n", "787.000000 | \n", "
50% | \n", "5.229129 | \n", "1.048780 | \n", "2.818116 | \n", "1166.000000 | \n", "
75% | \n", "6.052381 | \n", "1.099526 | \n", "3.282261 | \n", "1725.000000 | \n", "
max | \n", "141.909091 | \n", "34.066667 | \n", "1243.333333 | \n", "35682.000000 | \n", "