mirror of
https://github.com/fenago/data-science.git
synced 2026-05-04 08:31:59 +00:00
273 lines
5.3 KiB
Plaintext
273 lines
5.3 KiB
Plaintext
{
|
|
"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 sklearn.metrics import mean_squared_error\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": "19bc9dc1-8be5-4e86-e046-b3f0db93036e"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model.fit(X_train, y_train)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "8E5jQG-fCaMM"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"preds_train = rf_model.predict(X_train)\n",
|
|
"preds_test = rf_model.predict(X_test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "0L0vYPIOri1M",
|
|
"outputId": "7a89036f-2928-4de0-fccb-0af63f2e38e9"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"train_mse = mean_squared_error(y_train, preds_train)\n",
|
|
"train_mse"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "4qIcCT_7rwrQ",
|
|
"outputId": "cf6116e0-9bbf-482f-fa9a-201968438cb3"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"test_mse = mean_squared_error(y_test, preds_test)\n",
|
|
"test_mse"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 159
|
|
},
|
|
"colab_type": "code",
|
|
"id": "lUuLmYo8r0JK",
|
|
"outputId": "a54d81d3-1d5b-4491-9174-0640a1f80c41"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model.feature_importances_"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "qYJAfNV4s120"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"varimp_df = pd.DataFrame()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "_updjbMss5js"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"varimp_df['feature'] = df.columns"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "dN4nrheJtAAg"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"varimp_df['importance'] = rf_model.feature_importances_"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 194
|
|
},
|
|
"colab_type": "code",
|
|
"id": "F7YMpWyFtE5B",
|
|
"outputId": "1224b31e-30c1-4924-9ece-173490108b3f"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"varimp_df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 702
|
|
},
|
|
"colab_type": "code",
|
|
"id": "LxFxUajHs2Ge",
|
|
"outputId": "26b1ddd5-cada-460d-e10f-095e90db7d5c"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"alt.Chart(varimp_df).mark_bar().encode(\n",
|
|
" x='importance',\n",
|
|
" y=\"feature\"\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "Exercise9_02.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
|
|
}
|