{"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":"#mutual information feature selection for regression\n#Tutorial: https://bobrupakroy.medium.com/8eb19071664b?source=friends_link&sk=acda45060f35ec47f74c84945fcb6b66\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.feature_selection import mutual_info_regression\n\n# Set Matplotlib defaults\nplt.style.use(\"seaborn-whitegrid\")\nplt.rc(\"figure\", autolayout=True)\nplt.rc(\n    \"axes\",\n    labelweight=\"bold\",\n    labelsize=\"large\",\n    titleweight=\"bold\",\n    titlesize=14,\n    titlepad=10,\n)\n# Load data\ndf = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\ncol_list = ['MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street', 'Alley',\n       'LotShape', 'LandContour', 'Utilities', 'LotConfig', 'LandSlope',\n       'Neighborhood', 'Condition1', 'Condition2', 'BldgType', 'HouseStyle',\n       'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',\n       'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'MasVnrArea',\n       'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual', 'BsmtCond',\n       'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtFinType2',\n       'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'Heating', 'HeatingQC',\n       'CentralAir', 'Electrical', 'LowQualFinSF',\n       'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath',\n       'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual', 'TotRmsAbvGrd',\n       'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType', 'GarageFinish',\n       'GarageCars', 'GarageArea', 'GarageQual', 'GarageCond', 'PavedDrive',\n       'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', \n       'ScreenPorch', 'PoolArea', 'PoolQC', 'Fence', 'MiscFeature', 'MiscVal',\n       'MoSold', 'SaleType', 'SaleCondition', 'SalePrice']\n\n#'FirstFlrSF', 'SecondFlrSF', 'Threeseasonporch', 'YearSold'\ndf = df[col_list]\ndf.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:19.620093Z","iopub.execute_input":"2022-07-17T06:59:19.620509Z","iopub.status.idle":"2022-07-17T06:59:19.681247Z","shell.execute_reply.started":"2022-07-17T06:59:19.620478Z","shell.execute_reply":"2022-07-17T06:59:19.680030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Utility functions\ndef make_mi_scores(X, y):\n    X = X.copy()\n    for colname in X.select_dtypes([\"object\", \"category\"]):\n        X[colname], _ = X[colname].factorize()\n    # All discrete features should now have integer dtypes\n    discrete_features = [pd.api.types.is_integer_dtype(t) for t in X.dtypes]\n    mi_scores = mutual_info_regression(X, y, discrete_features=discrete_features, random_state=0)\n    mi_scores = pd.Series(mi_scores, name=\"MI Scores\", index=X.columns)\n    mi_scores = mi_scores.sort_values(ascending=False)\n    return mi_scores\n\ndef plot_mi_scores(scores):\n    scores = scores.sort_values(ascending=True)\n    width = np.arange(len(scores))\n    ticks = list(scores.index)\n    plt.barh(width, scores)\n    plt.yticks(width, ticks)\n    plt.title(\"Mutual Information Scores\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:23.797403Z","iopub.execute_input":"2022-07-17T06:59:23.798143Z","iopub.status.idle":"2022-07-17T06:59:23.806977Z","shell.execute_reply.started":"2022-07-17T06:59:23.798082Z","shell.execute_reply":"2022-07-17T06:59:23.806117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EDA\nfeatures = [\"YearBuilt\", \"MoSold\", \"ScreenPorch\"]\nsns.relplot(\n    x=\"value\", y=\"SalePrice\", col=\"variable\", data=df.melt(id_vars=\"SalePrice\", value_vars=features), facet_kws=dict(sharex=False),\n);","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:25.880381Z","iopub.execute_input":"2022-07-17T06:59:25.880777Z","iopub.status.idle":"2022-07-17T06:59:26.456458Z","shell.execute_reply.started":"2022-07-17T06:59:25.880743Z","shell.execute_reply":"2022-07-17T06:59:26.455615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mutual Information \n\nX = df.copy()\ny = X.pop('SalePrice')\n\ndf.fillna(0,inplace=True)\ndf.isna().sum()\n\nX = df.copy()\ny = X.pop('SalePrice')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:28.062153Z","iopub.execute_input":"2022-07-17T06:59:28.062530Z","iopub.status.idle":"2022-07-17T06:59:28.099780Z","shell.execute_reply.started":"2022-07-17T06:59:28.062501Z","shell.execute_reply":"2022-07-17T06:59:28.098854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mi_scores = make_mi_scores(X, y)\nmi_scores.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:29.806529Z","iopub.execute_input":"2022-07-17T06:59:29.806926Z","iopub.status.idle":"2022-07-17T06:59:31.696683Z","shell.execute_reply.started":"2022-07-17T06:59:29.806893Z","shell.execute_reply":"2022-07-17T06:59:31.695363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(dpi=100, figsize=(8, 5))\nplot_mi_scores(mi_scores.head(20))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-17T06:59:36.519203Z","iopub.execute_input":"2022-07-17T06:59:36.519573Z","iopub.status.idle":"2022-07-17T06:59:36.782065Z","shell.execute_reply.started":"2022-07-17T06:59:36.519543Z","shell.execute_reply":"2022-07-17T06:59:36.781135Z"},"trusted":true},"execution_count":null,"outputs":[]}]}