{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "RCQKxdZOjw_2" }, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.ensemble import RandomForestRegressor\n", "from mlxtend.evaluate import feature_importance_permutation\n", "import altair as alt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "tm5mPWzJkRLO" }, "outputs": [], "source": [ "file_url = '../Dataset/phpYYZ4Qc.csv'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "FEC78ZbAj3Vb" }, "outputs": [], "source": [ "df = pd.read_csv(file_url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "IxiwVfiJq8KL" }, "outputs": [], "source": [ "y = df.pop('rej')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "ifR4fTCIrJBe" }, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.3, random_state=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "LF-qSSFXCaHa" }, "outputs": [], "source": [ "rf_model = RandomForestRegressor(random_state=1, n_estimators=50, max_depth=6, min_samples_leaf=60)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 141 }, "colab_type": "code", "id": "CB2JS9B2CaJ8", "outputId": "01208cec-d18f-44b7-cd4f-daf356615b09" }, "outputs": [], "source": [ "rf_model.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 159 }, "colab_type": "code", "id": "K3QQ305yvYWM", "outputId": "8e96b8dd-8d53-45ac-8d88-5394d777003d" }, "outputs": [], "source": [ "imp_vals, _ = feature_importance_permutation(predict_method=rf_model.predict, X=X_test.values, y=y_test.values, metric='r2', num_rounds=1, seed=2)\n", "imp_vals" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "dNumrsgTwcTH" }, "outputs": [], "source": [ "varimp_df = pd.DataFrame({'feature': df.columns, 'importance': imp_vals})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 702 }, "colab_type": "code", "id": "0INCKVEIwkFB", "outputId": "efad98ad-4a5b-4e15-d5ae-2d3e4bd4ed08" }, "outputs": [], "source": [ "alt.Chart(varimp_df).mark_bar().encode(\n", " x='importance',\n", " y=\"feature\"\n", ")" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Exercise9_03.ipynb", "provenance": [] }, "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.8.6" } }, "nbformat": 4, "nbformat_minor": 1 }