{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%pylab inline\n# 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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"645adc744574fae073f1ac1de9ebc6ab5aa86264"},"cell_type":"code","source":"data_train = pd.read_csv(\"../input/train.csv\")\ndata_test = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0e6a9e80ae39908385e54a8a02b134e57fcd850"},"cell_type":"code","source":"from sklearn import pipeline, compose, impute, preprocessing\nall_features = ['Id', 'MSSubClass', 'MSZoning', \n                'LotFrontage', 'LotArea', 'Street',\n                'Alley', 'LotShape', 'LandContour', \n                'Utilities', 'LotConfig','LandSlope', \n                'Neighborhood', 'Condition1', 'Condition2', \n                'BldgType','HouseStyle', 'OverallQual', \n                'OverallCond', 'YearBuilt', 'YearRemodAdd',\n                'RoofStyle', 'RoofMatl', 'Exterior1st', \n                'Exterior2nd', 'MasVnrType','MasVnrArea', \n                'ExterQual', 'ExterCond', 'Foundation', \n                'BsmtQual','BsmtCond', 'BsmtExposure', \n                'BsmtFinType1', 'BsmtFinSF1','BsmtFinType2',\n                'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', \n                'Heating','HeatingQC', 'CentralAir', \n                'Electrical', '1stFlrSF', '2ndFlrSF',\n                'LowQualFinSF', 'GrLivArea', 'BsmtFullBath',\n                'BsmtHalfBath', 'FullBath','HalfBath', \n                'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',\n                'TotRmsAbvGrd', 'Functional', 'Fireplaces', \n                'FireplaceQu', 'GarageType','GarageYrBlt', \n                'GarageFinish', 'GarageCars', 'GarageArea', \n                'GarageQual','GarageCond', 'PavedDrive', \n                'WoodDeckSF', 'OpenPorchSF','EnclosedPorch',\n                '3SsnPorch', 'ScreenPorch', 'PoolArea', \n                'PoolQC','Fence', 'MiscFeature', 'MiscVal', \n                'MoSold', 'YrSold', 'SaleType',\n                'SaleCondition', 'SalePrice']\n\nnumeric_features = [\"LotFrontage\", \"LotArea\",\"OverallQual\",\n                   \"OverallCond\", \"YearBuilt\", \"YearRemodAdd\",\n                   \"BsmtFinSF1\",\"BsmtFinSF2\",\"BsmtUnfSF\",\n                   \"TotalBsmtSF\", \"1stFlrSF\", \"2ndFlrSF\",\n                   \"LowQualFinSF\", \"GrLivArea\", \"BsmtFullBath\",\n                   \"BsmtHalfBath\", \"FullBath\", \"HalfBath\",\n                   \"BedroomAbvGr\", \"KitchenAbvGr\", \"TotRmsAbvGrd\",\n                   \"Fireplaces\", \"GarageCars\", \"GarageArea\",\n                   \"WoodDeckSF\", \"OpenPorchSF\", \"EnclosedPorch\",\n                   \"3SsnPorch\", \"ScreenPorch\", \"PoolArea\",\n                   \"MiscVal\", \"MoSold\", \"YrSold\",\"GarageYrBlt\",\n                   \"MasVnrArea\"]\ncategorical_features = [\"MSZoning\", \"Street\", \"Alley\", \"LotShape\",\n                       \"LandContour\", \"Utilities\", \"LotConfig\",\n                       \"LandSlope\", \"Neighborhood\", \"Condition1\",\n                       \"Condition2\", \"BldgType\", \"HouseStyle\",\n                       \"RoofStyle\", \"RoofMatl\", \"Exterior1st\",\n                       \"Exterior2nd\", \"ExterQual\",\n                       \"ExterCond\", \"Foundation\", \"BsmtQual\",\n                       \"BsmtCond\", \"BsmtExposure\",\"BsmtFinType1\",\n                       \"BsmtFinType2\", \"Heating\", \"HeatingQC\",\n                       \"CentralAir\", \"Electrical\",\"KitchenQual\",\n                       \"Functional\", \"FireplaceQu\", \"GarageType\",\n                       \"GarageFinish\", \"GarageQual\",\n                       \"GarageCond\", \"PavedDrive\", \"PoolQC\",\n                       \"Fence\", \"MiscFeature\", \"SaleType\",\n                       \"SaleCondition\"]\n\nnumeric_cleanup = pipeline.make_pipeline(\n  impute.SimpleImputer(strategy=\"median\"),\n  preprocessing.StandardScaler())\ncategorical_cleanup = pipeline.make_pipeline(\n  impute.SimpleImputer(strategy=\"constant\", fill_value=\"NA\"),\n  preprocessing.OneHotEncoder(handle_unknown=\"ignore\"))\n\ncleanup = compose.make_column_transformer(\n  (numeric_cleanup, numeric_features),\n  (categorical_cleanup, categorical_features))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a8eac393f36d00893fb2a78875f1ede77a888d0"},"cell_type":"code","source":"from sklearn import model_selection\n\ncleanup.fit(data_train)\nclean_train = cleanup.transform(data_train)\nclean_test = cleanup.transform(data_test)\n\nX_train, X_val, y_train, y_val = \\\n  model_selection.train_test_split(clean_train, data_train[\"SalePrice\"].values)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"1db0af3b2812404ab9261d1da993542f001be63d"},"cell_type":"code","source":"X_train","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"import seaborn\nseaborn.pairplot(data_train[[\n    \"SalePrice\",#\"LotFrontage\", \"LotArea\",\"OverallQual\",\n    #\"OverallCond\", \"YearBuilt\", \"YearRemodAdd\",\n    \"BsmtFinSF1\",\"BsmtFinSF2\",\"BsmtUnfSF\",\n    \"TotalBsmtSF\", \"1stFlrSF\", \"2ndFlrSF\",\n    \"LowQualFinSF\"\n]])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab6dbaaba366edcb866ac9595ee6b09085478789"},"cell_type":"code","source":"from sklearn import linear_model\nmodel = linear_model.LinearRegression()\nmodel.fit(X_train, y_train)\nprint(model.score(X_val, y_val))\n\ny_pred = model.predict(clean_test)\nsubmission = pd.DataFrame({\n    \"Id\": data_test[\"Id\"],\n    \"SalePrice\": y_pred\n})\nsubmission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d0d1ee6656d9902f014a722353ee0b26484a5f4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}