{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Analiza danych w Pythonie\n", "\n", "### Tomasz Dwojak\n", "\n", "### 3 czerwca 2018" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Analiza danych:\n", "\n", " * R\n", " * Python" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Python Ekosystem\n", "\n", " * pandas: ramka danych\n", " * sklearn: modele ML\n", " * numpy: obliczenia\n", " * matplotlib: wykresy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Typy danych\n", "\n", " * Szereg (`pd.Series`)\n", " * Ramka danych (`pd.DataFrame`)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Wczytanie danych" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "data = pd.read_csv(\"./data/iowa.csv.gz\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "slide" } }, "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", " \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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
1220RL80.09600PaveNaNRegLvlAllPub...0NaNNaNNaN052007WDNormal181500
2360RL68.011250PaveNaNIR1LvlAllPub...0NaNNaNNaN092008WDNormal223500
3470RL60.09550PaveNaNIR1LvlAllPub...0NaNNaNNaN022006WDAbnorml140000
4560RL84.014260PaveNaNIR1LvlAllPub...0NaNNaNNaN0122008WDNormal250000
\n", "

5 rows × 81 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "0 1 60 RL 65.0 8450 Pave NaN Reg \n", "1 2 20 RL 80.0 9600 Pave NaN Reg \n", "2 3 60 RL 68.0 11250 Pave NaN IR1 \n", "3 4 70 RL 60.0 9550 Pave NaN IR1 \n", "4 5 60 RL 84.0 14260 Pave NaN IR1 \n", "\n", " LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \\\n", "0 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "1 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "2 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "3 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "4 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice \n", "0 2 2008 WD Normal 208500 \n", "1 5 2007 WD Normal 181500 \n", "2 9 2008 WD Normal 223500 \n", "3 2 2006 WD Abnorml 140000 \n", "4 12 2008 WD Normal 250000 \n", "\n", "[5 rows x 81 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1460, 81)\n" ] } ], "source": [ "shape = data.shape\n", "rows = shape[0]\n", "cols = shape[1]\n", "\n", "print(rows, cols)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 1460 entries, 0 to 1459\n", "Data columns (total 81 columns):\n", "Id 1460 non-null int64\n", "MSSubClass 1460 non-null int64\n", "MSZoning 1460 non-null object\n", "LotFrontage 1201 non-null float64\n", "LotArea 1460 non-null int64\n", "Street 1460 non-null object\n", "Alley 91 non-null object\n", "LotShape 1460 non-null object\n", "LandContour 1460 non-null object\n", "Utilities 1460 non-null object\n", "LotConfig 1460 non-null object\n", "LandSlope 1460 non-null object\n", "Neighborhood 1460 non-null object\n", "Condition1 1460 non-null object\n", "Condition2 1460 non-null object\n", "BldgType 1460 non-null object\n", "HouseStyle 1460 non-null object\n", "OverallQual 1460 non-null int64\n", "OverallCond 1460 non-null int64\n", "YearBuilt 1460 non-null int64\n", "YearRemodAdd 1460 non-null int64\n", "RoofStyle 1460 non-null object\n", "RoofMatl 1460 non-null object\n", "Exterior1st 1460 non-null object\n", "Exterior2nd 1460 non-null object\n", "MasVnrType 1452 non-null object\n", "MasVnrArea 1452 non-null float64\n", "ExterQual 1460 non-null object\n", "ExterCond 1460 non-null object\n", "Foundation 1460 non-null object\n", "BsmtQual 1423 non-null object\n", "BsmtCond 1423 non-null object\n", "BsmtExposure 1422 non-null object\n", "BsmtFinType1 1423 non-null object\n", "BsmtFinSF1 1460 non-null int64\n", "BsmtFinType2 1422 non-null object\n", "BsmtFinSF2 1460 non-null int64\n", "BsmtUnfSF 1460 non-null int64\n", "TotalBsmtSF 1460 non-null int64\n", "Heating 1460 non-null object\n", "HeatingQC 1460 non-null object\n", "CentralAir 1460 non-null object\n", "Electrical 1459 non-null object\n", "1stFlrSF 1460 non-null int64\n", "2ndFlrSF 1460 non-null int64\n", "LowQualFinSF 1460 non-null int64\n", "GrLivArea 1460 non-null int64\n", "BsmtFullBath 1460 non-null int64\n", "BsmtHalfBath 1460 non-null int64\n", "FullBath 1460 non-null int64\n", "HalfBath 1460 non-null int64\n", "BedroomAbvGr 1460 non-null int64\n", "KitchenAbvGr 1460 non-null int64\n", "KitchenQual 1460 non-null object\n", "TotRmsAbvGrd 1460 non-null int64\n", "Functional 1460 non-null object\n", "Fireplaces 1460 non-null int64\n", "FireplaceQu 770 non-null object\n", "GarageType 1379 non-null object\n", "GarageYrBlt 1379 non-null float64\n", "GarageFinish 1379 non-null object\n", "GarageCars 1460 non-null int64\n", "GarageArea 1460 non-null int64\n", "GarageQual 1379 non-null object\n", "GarageCond 1379 non-null object\n", "PavedDrive 1460 non-null object\n", "WoodDeckSF 1460 non-null int64\n", "OpenPorchSF 1460 non-null int64\n", "EnclosedPorch 1460 non-null int64\n", "3SsnPorch 1460 non-null int64\n", "ScreenPorch 1460 non-null int64\n", "PoolArea 1460 non-null int64\n", "PoolQC 7 non-null object\n", "Fence 281 non-null object\n", "MiscFeature 54 non-null object\n", "MiscVal 1460 non-null int64\n", "MoSold 1460 non-null int64\n", "YrSold 1460 non-null int64\n", "SaleType 1460 non-null object\n", "SaleCondition 1460 non-null object\n", "SalePrice 1460 non-null int64\n", "dtypes: float64(3), int64(35), object(43)\n", "memory usage: 924.0+ KB\n" ] } ], "source": [ "data.info()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "slide" } }, "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IdMSSubClassLotFrontageLotAreaOverallQualOverallCondYearBuiltYearRemodAddMasVnrAreaBsmtFinSF1...WoodDeckSFOpenPorchSFEnclosedPorch3SsnPorchScreenPorchPoolAreaMiscValMoSoldYrSoldSalePrice
count1460.0000001460.0000001201.0000001460.0000001460.0000001460.0000001460.0000001460.0000001452.0000001460.000000...1460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.000000
mean730.50000056.89726070.04995810516.8280826.0993155.5753421971.2678081984.865753103.685262443.639726...94.24452146.66027421.9541103.40958915.0609592.75890443.4890416.3219182007.815753180921.195890
std421.61000942.30057124.2847529981.2649321.3829971.11279930.20290420.645407181.066207456.098091...125.33879466.25602861.11914929.31733155.75741540.177307496.1230242.7036261.32809579442.502883
min1.00000020.00000021.0000001300.0000001.0000001.0000001872.0000001950.0000000.0000000.000000...0.0000000.0000000.0000000.0000000.0000000.0000000.0000001.0000002006.00000034900.000000
25%365.75000020.00000059.0000007553.5000005.0000005.0000001954.0000001967.0000000.0000000.000000...0.0000000.0000000.0000000.0000000.0000000.0000000.0000005.0000002007.000000129975.000000
50%730.50000050.00000069.0000009478.5000006.0000005.0000001973.0000001994.0000000.000000383.500000...0.00000025.0000000.0000000.0000000.0000000.0000000.0000006.0000002008.000000163000.000000
75%1095.25000070.00000080.00000011601.5000007.0000006.0000002000.0000002004.000000166.000000712.250000...168.00000068.0000000.0000000.0000000.0000000.0000000.0000008.0000002009.000000214000.000000
max1460.000000190.000000313.000000215245.00000010.0000009.0000002010.0000002010.0000001600.0000005644.000000...857.000000547.000000552.000000508.000000480.000000738.00000015500.00000012.0000002010.000000755000.000000
\n", "

8 rows × 38 columns

\n", "
" ], "text/plain": [ " Id MSSubClass LotFrontage LotArea OverallQual \\\n", "count 1460.000000 1460.000000 1201.000000 1460.000000 1460.000000 \n", "mean 730.500000 56.897260 70.049958 10516.828082 6.099315 \n", "std 421.610009 42.300571 24.284752 9981.264932 1.382997 \n", "min 1.000000 20.000000 21.000000 1300.000000 1.000000 \n", "25% 365.750000 20.000000 59.000000 7553.500000 5.000000 \n", "50% 730.500000 50.000000 69.000000 9478.500000 6.000000 \n", "75% 1095.250000 70.000000 80.000000 11601.500000 7.000000 \n", "max 1460.000000 190.000000 313.000000 215245.000000 10.000000 \n", "\n", " OverallCond YearBuilt YearRemodAdd MasVnrArea BsmtFinSF1 \\\n", "count 1460.000000 1460.000000 1460.000000 1452.000000 1460.000000 \n", "mean 5.575342 1971.267808 1984.865753 103.685262 443.639726 \n", "std 1.112799 30.202904 20.645407 181.066207 456.098091 \n", "min 1.000000 1872.000000 1950.000000 0.000000 0.000000 \n", "25% 5.000000 1954.000000 1967.000000 0.000000 0.000000 \n", "50% 5.000000 1973.000000 1994.000000 0.000000 383.500000 \n", "75% 6.000000 2000.000000 2004.000000 166.000000 712.250000 \n", "max 9.000000 2010.000000 2010.000000 1600.000000 5644.000000 \n", "\n", " ... WoodDeckSF OpenPorchSF EnclosedPorch 3SsnPorch \\\n", "count ... 1460.000000 1460.000000 1460.000000 1460.000000 \n", "mean ... 94.244521 46.660274 21.954110 3.409589 \n", "std ... 125.338794 66.256028 61.119149 29.317331 \n", "min ... 0.000000 0.000000 0.000000 0.000000 \n", "25% ... 0.000000 0.000000 0.000000 0.000000 \n", "50% ... 0.000000 25.000000 0.000000 0.000000 \n", "75% ... 168.000000 68.000000 0.000000 0.000000 \n", "max ... 857.000000 547.000000 552.000000 508.000000 \n", "\n", " ScreenPorch PoolArea MiscVal MoSold YrSold \\\n", "count 1460.000000 1460.000000 1460.000000 1460.000000 1460.000000 \n", "mean 15.060959 2.758904 43.489041 6.321918 2007.815753 \n", "std 55.757415 40.177307 496.123024 2.703626 1.328095 \n", "min 0.000000 0.000000 0.000000 1.000000 2006.000000 \n", "25% 0.000000 0.000000 0.000000 5.000000 2007.000000 \n", "50% 0.000000 0.000000 0.000000 6.000000 2008.000000 \n", "75% 0.000000 0.000000 0.000000 8.000000 2009.000000 \n", "max 480.000000 738.000000 15500.000000 12.000000 2010.000000 \n", "\n", " SalePrice \n", "count 1460.000000 \n", "mean 180921.195890 \n", "std 79442.502883 \n", "min 34900.000000 \n", "25% 129975.000000 \n", "50% 163000.000000 \n", "75% 214000.000000 \n", "max 755000.000000 \n", "\n", "[8 rows x 38 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.describe()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dostęp do danych" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index([u'Id', u'MSSubClass', u'MSZoning', u'LotFrontage', u'LotArea',\n", " u'Street', u'Alley', u'LotShape', u'LandContour', u'Utilities',\n", " u'LotConfig', u'LandSlope', u'Neighborhood', u'Condition1',\n", " u'Condition2', u'BldgType', u'HouseStyle', u'OverallQual',\n", " u'OverallCond', u'YearBuilt', u'YearRemodAdd', u'RoofStyle',\n", " u'RoofMatl', u'Exterior1st', u'Exterior2nd', u'MasVnrType',\n", " u'MasVnrArea', u'ExterQual', u'ExterCond', u'Foundation', u'BsmtQual',\n", " u'BsmtCond', u'BsmtExposure', u'BsmtFinType1', u'BsmtFinSF1',\n", " u'BsmtFinType2', u'BsmtFinSF2', u'BsmtUnfSF', u'TotalBsmtSF',\n", " u'Heating', u'HeatingQC', u'CentralAir', u'Electrical', u'1stFlrSF',\n", " u'2ndFlrSF', u'LowQualFinSF', u'GrLivArea', u'BsmtFullBath',\n", " u'BsmtHalfBath', u'FullBath', u'HalfBath', u'BedroomAbvGr',\n", " u'KitchenAbvGr', u'KitchenQual', u'TotRmsAbvGrd', u'Functional',\n", " u'Fireplaces', u'FireplaceQu', u'GarageType', u'GarageYrBlt',\n", " u'GarageFinish', u'GarageCars', u'GarageArea', u'GarageQual',\n", " u'GarageCond', u'PavedDrive', u'WoodDeckSF', u'OpenPorchSF',\n", " u'EnclosedPorch', u'3SsnPorch', u'ScreenPorch', u'PoolArea', u'PoolQC',\n", " u'Fence', u'MiscFeature', u'MiscVal', u'MoSold', u'YrSold', u'SaleType',\n", " u'SaleCondition', u'SalePrice'],\n", " dtype='object')\n" ] } ], "source": [ "print(data.columns)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 60\n", "1 20\n", "2 60\n", "3 70\n", "4 60\n", "Name: MSSubClass, dtype: int64\n" ] } ], "source": [ "print(data['MSSubClass'].head())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " MSSubClass SalePrice\n", "0 60 208500\n", "1 20 181500\n", "2 60 223500\n", "3 70 140000\n", "4 60 250000\n" ] } ], "source": [ "print(data[['MSSubClass', 'SalePrice']].head())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "slide" } }, "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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
3470RL60.09550PaveNaNIR1LvlAllPub...0NaNNaNNaN022006WDAbnorml140000
\n", "

2 rows × 81 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "0 1 60 RL 65.0 8450 Pave NaN Reg \n", "3 4 70 RL 60.0 9550 Pave NaN IR1 \n", "\n", " LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \\\n", "0 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "3 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice \n", "0 2 2008 WD Normal 208500 \n", "3 2 2006 WD Abnorml 140000 \n", "\n", "[2 rows x 81 columns]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[[0,3]]" ] }, { "cell_type": "code", "execution_count": 14, "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", " \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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
1220RL80.09600PaveNaNRegLvlAllPub...0NaNNaNNaN052007WDNormal181500
2360RL68.011250PaveNaNIR1LvlAllPub...0NaNNaNNaN092008WDNormal223500
3470RL60.09550PaveNaNIR1LvlAllPub...0NaNNaNNaN022006WDAbnorml140000
4560RL84.014260PaveNaNIR1LvlAllPub...0NaNNaNNaN0122008WDNormal250000
5650RL85.014115PaveNaNIR1LvlAllPub...0NaNMnPrvShed700102009WDNormal143000
\n", "

6 rows × 81 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "0 1 60 RL 65.0 8450 Pave NaN Reg \n", "1 2 20 RL 80.0 9600 Pave NaN Reg \n", "2 3 60 RL 68.0 11250 Pave NaN IR1 \n", "3 4 70 RL 60.0 9550 Pave NaN IR1 \n", "4 5 60 RL 84.0 14260 Pave NaN IR1 \n", "5 6 50 RL 85.0 14115 Pave NaN IR1 \n", "\n", " LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \\\n", "0 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "1 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "2 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "3 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "4 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "5 Lvl AllPub ... 0 NaN MnPrv Shed 700 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice \n", "0 2 2008 WD Normal 208500 \n", "1 5 2007 WD Normal 181500 \n", "2 9 2008 WD Normal 223500 \n", "3 2 2006 WD Abnorml 140000 \n", "4 12 2008 WD Normal 250000 \n", "5 10 2009 WD Normal 143000 \n", "\n", "[6 rows x 81 columns]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[0:5]" ] }, { "cell_type": "code", "execution_count": 16, "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", " \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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
1220RL80.09600PaveNaNRegLvlAllPub...0NaNNaNNaN052007WDNormal181500
2360RL68.011250PaveNaNIR1LvlAllPub...0NaNNaNNaN092008WDNormal223500
3470RL60.09550PaveNaNIR1LvlAllPub...0NaNNaNNaN022006WDAbnorml140000
4560RL84.014260PaveNaNIR1LvlAllPub...0NaNNaNNaN0122008WDNormal250000
\n", "

5 rows × 81 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "0 1 60 RL 65.0 8450 Pave NaN Reg \n", "1 2 20 RL 80.0 9600 Pave NaN Reg \n", "2 3 60 RL 68.0 11250 Pave NaN IR1 \n", "3 4 70 RL 60.0 9550 Pave NaN IR1 \n", "4 5 60 RL 84.0 14260 Pave NaN IR1 \n", "\n", " LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \\\n", "0 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "1 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "2 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "3 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "4 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice \n", "0 2 2008 WD Normal 208500 \n", "1 5 2007 WD Normal 181500 \n", "2 9 2008 WD Normal 223500 \n", "3 2 2006 WD Abnorml 140000 \n", "4 12 2008 WD Normal 250000 \n", "\n", "[5 rows x 81 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[data['MSZoning'] == 'RL'].head()" ] }, { "cell_type": "code", "execution_count": 18, "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", " \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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
1220RL80.09600PaveNaNRegLvlAllPub...0NaNNaNNaN052007WDNormal181500
6720RL75.010084PaveNaNRegLvlAllPub...0NaNNaNNaN082007WDNormal307000
910190RL50.07420PaveNaNRegLvlAllPub...0NaNNaNNaN012008WDNormal118000
101120RL70.011200PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal129500
\n", "

5 rows × 81 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "0 1 60 RL 65.0 8450 Pave NaN Reg \n", "1 2 20 RL 80.0 9600 Pave NaN Reg \n", "6 7 20 RL 75.0 10084 Pave NaN Reg \n", "9 10 190 RL 50.0 7420 Pave NaN Reg \n", "10 11 20 RL 70.0 11200 Pave NaN Reg \n", "\n", " LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal \\\n", "0 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "1 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "6 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "9 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "10 Lvl AllPub ... 0 NaN NaN NaN 0 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice \n", "0 2 2008 WD Normal 208500 \n", "1 5 2007 WD Normal 181500 \n", "6 8 2007 WD Normal 307000 \n", "9 1 2008 WD Normal 118000 \n", "10 2 2008 WD Normal 129500 \n", "\n", "[5 rows x 81 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[(data['MSZoning'] == 'RL') & (data['LotShape'] == 'Reg')].head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "ceny = data['SalePrice']" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "180921.19589041095" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ceny.mean()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "755000" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ceny.max()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'SalePrice'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ceny.name" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Plus vat:', 0 256455.00\n", "1 223245.00\n", "2 274905.00\n", "3 172200.00\n", "4 307500.00\n", "5 175890.00\n", "6 377610.00\n", "7 246000.00\n", "8 159777.00\n", "9 145140.00\n", "10 159285.00\n", "11 424350.00\n", "12 177120.00\n", "13 343785.00\n", "14 193110.00\n", "15 162360.00\n", "16 183270.00\n", "17 110700.00\n", "18 195570.00\n", "19 170970.00\n", "20 400119.00\n", "21 171462.00\n", "22 282900.00\n", "23 159777.00\n", "24 189420.00\n", "25 315249.00\n", "26 165804.00\n", "27 376380.00\n", "28 255225.00\n", "29 84255.00\n", " ... \n", "1430 236332.20\n", "1431 176812.50\n", "1432 79335.00\n", "1433 229395.00\n", "1434 196800.00\n", "1435 214020.00\n", "1436 148215.00\n", "1437 485378.91\n", "1438 184131.00\n", "1439 242310.00\n", "1440 234930.00\n", "1441 183639.00\n", "1442 381300.00\n", "1443 148830.00\n", "1444 220908.00\n", "1445 158670.00\n", "1446 194217.00\n", "1447 295200.00\n", "1448 137760.00\n", "1449 113160.00\n", "1450 167280.00\n", "1451 353120.70\n", "1452 178350.00\n", "1453 103935.00\n", "1454 227550.00\n", "1455 215250.00\n", "1456 258300.00\n", "1457 327795.00\n", "1458 174813.75\n", "1459 181425.00\n", "Name: SalePrice, Length: 1460, dtype: float64)\n" ] } ], "source": [ "print(\"Plus vat:\", ceny * 1.23)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['RL', 'RM', 'C (all)', 'FV', 'RH'], dtype=object)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.MSZoning.unique()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RL 1151\n", "RM 218\n", "FV 65\n", "RH 16\n", "C (all) 10\n", "Name: MSZoning, dtype: int64" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.MSZoning.value_counts()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "data['nowa'] = ceny * 1.23" ] }, { "cell_type": "code", "execution_count": 42, "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", " \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", "
MSSubClassMSZoningLotFrontageStreetAlleyLotShapeLandContourUtilitiesLotConfigLandSlope...PoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePricenowa
060RL65.0PaveNaNRegLvlAllPubInsideGtl...NaNNaNNaN022008WDNormal208500256455.0
120RL80.0PaveNaNRegLvlAllPubFR2Gtl...NaNNaNNaN052007WDNormal181500223245.0
260RL68.0PaveNaNIR1LvlAllPubInsideGtl...NaNNaNNaN092008WDNormal223500274905.0
370RL60.0PaveNaNIR1LvlAllPubCornerGtl...NaNNaNNaN022006WDAbnorml140000172200.0
460RL84.0PaveNaNIR1LvlAllPubFR2Gtl...NaNNaNNaN0122008WDNormal250000307500.0
\n", "

5 rows × 80 columns

\n", "
" ], "text/plain": [ " MSSubClass MSZoning LotFrontage Street Alley LotShape LandContour \\\n", "0 60 RL 65.0 Pave NaN Reg Lvl \n", "1 20 RL 80.0 Pave NaN Reg Lvl \n", "2 60 RL 68.0 Pave NaN IR1 Lvl \n", "3 70 RL 60.0 Pave NaN IR1 Lvl \n", "4 60 RL 84.0 Pave NaN IR1 Lvl \n", "\n", " Utilities LotConfig LandSlope ... PoolQC Fence MiscFeature MiscVal \\\n", "0 AllPub Inside Gtl ... NaN NaN NaN 0 \n", "1 AllPub FR2 Gtl ... NaN NaN NaN 0 \n", "2 AllPub Inside Gtl ... NaN NaN NaN 0 \n", "3 AllPub Corner Gtl ... NaN NaN NaN 0 \n", "4 AllPub FR2 Gtl ... NaN NaN NaN 0 \n", "\n", " MoSold YrSold SaleType SaleCondition SalePrice nowa \n", "0 2 2008 WD Normal 208500 256455.0 \n", "1 5 2007 WD Normal 181500 223245.0 \n", "2 9 2008 WD Normal 223500 274905.0 \n", "3 2 2006 WD Abnorml 140000 172200.0 \n", "4 12 2008 WD Normal 250000 307500.0 \n", "\n", "[5 rows x 80 columns]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.drop('LotArea', axis=1)\n", "data.drop(['Id', 'LotArea'], axis=1).head()" ] }, { "cell_type": "code", "execution_count": 44, "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", " \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", "
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePricenowa
1220RL80.09600PaveNaNRegLvlAllPub...NaNNaNNaN052007WDNormal181500223245.0
2360RL68.011250PaveNaNIR1LvlAllPub...NaNNaNNaN092008WDNormal223500274905.0
3470RL60.09550PaveNaNIR1LvlAllPub...NaNNaNNaN022006WDAbnorml140000172200.0
4560RL84.014260PaveNaNIR1LvlAllPub...NaNNaNNaN0122008WDNormal250000307500.0
5650RL85.014115PaveNaNIR1LvlAllPub...NaNMnPrvShed700102009WDNormal143000175890.0
\n", "

5 rows × 82 columns

\n", "
" ], "text/plain": [ " Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \\\n", "1 2 20 RL 80.0 9600 Pave NaN Reg \n", "2 3 60 RL 68.0 11250 Pave NaN IR1 \n", "3 4 70 RL 60.0 9550 Pave NaN IR1 \n", "4 5 60 RL 84.0 14260 Pave NaN IR1 \n", "5 6 50 RL 85.0 14115 Pave NaN IR1 \n", "\n", " LandContour Utilities ... PoolQC Fence MiscFeature MiscVal MoSold \\\n", "1 Lvl AllPub ... NaN NaN NaN 0 5 \n", "2 Lvl AllPub ... NaN NaN NaN 0 9 \n", "3 Lvl AllPub ... NaN NaN NaN 0 2 \n", "4 Lvl AllPub ... NaN NaN NaN 0 12 \n", "5 Lvl AllPub ... NaN MnPrv Shed 700 10 \n", "\n", " YrSold SaleType SaleCondition SalePrice nowa \n", "1 2007 WD Normal 181500 223245.0 \n", "2 2008 WD Normal 223500 274905.0 \n", "3 2006 WD Abnorml 140000 172200.0 \n", "4 2008 WD Normal 250000 307500.0 \n", "5 2009 WD Normal 143000 175890.0 \n", "\n", "[5 rows x 82 columns]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.drop(0).head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }