mirror of
https://github.com/fenago/data-science.git
synced 2026-05-06 01:22:41 +00:00
367 lines
9.4 KiB
Plaintext
367 lines
9.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "qrZ-Tc-wgsId"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"from sklearn.ensemble import RandomForestClassifier\n",
|
|
"from sklearn.metrics import accuracy_score"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "W0sK3Lq-gu9p"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"file_url = '../Dataset/phpB0xrNj.csv'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "mtve2TXSgz8j"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df = pd.read_csv(file_url)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 253
|
|
},
|
|
"colab_type": "code",
|
|
"id": "A6IylzvbhSzT",
|
|
"outputId": "76a5fcb1-8854-49d0-8227-cebd62595554"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "vo8hM8nwhVi4"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"y = df.pop('class')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "vTcPJdafhSYK"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.3, random_state=888)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "KgCFUWeclP8v"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def train_rf(X_train, y_train, random_state=888, n_estimators=10, max_depth=None, min_samples_leaf=1, max_features='sqrt'):\n",
|
|
" rf_model = RandomForestClassifier(random_state=random_state, n_estimators=n_estimators, max_depth=max_depth, min_samples_leaf=min_samples_leaf, max_features=max_features)\n",
|
|
" rf_model.fit(X_train, y_train)\n",
|
|
" return rf_model"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 306
|
|
},
|
|
"colab_type": "code",
|
|
"id": "oq3np8M_l3ko",
|
|
"outputId": "e4fc9461-6bbe-4b03-8718-42f375d894b1"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_1 = train_rf(X_train, y_train)\n",
|
|
"rf_1.get_params()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "BStd7tbbmTZb"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def get_preds(rf_model, X_train, X_test):\n",
|
|
" train_preds = rf_model.predict(X_train)\n",
|
|
" test_preds = rf_model.predict(X_test)\n",
|
|
" return train_preds, test_preds"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "nyyO-BsNmkTh"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"trn_preds, tst_preds = get_preds(rf_1, X_train, X_test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "ZCWagMxKo0vn"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def print_accuracy(y_train, y_test, train_preds, test_preds):\n",
|
|
" train_acc = accuracy_score(y_train, train_preds)\n",
|
|
" test_acc = accuracy_score(y_test, test_preds)\n",
|
|
" print(train_acc)\n",
|
|
" print(test_acc)\n",
|
|
" return train_acc, test_acc"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "Jtkqn1Odo05L",
|
|
"outputId": "97a676a6-8b5d-4522-8848-4859105056ec"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"trn_acc, tst_preds = print_accuracy(y_train, y_test, trn_preds, tst_preds)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "b-jKLvOBrDjo"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=10, max_depth=None, min_samples_leaf=1, max_features='sqrt'):\n",
|
|
" rf_model = train_rf(X_train, y_train, random_state=random_state, n_estimators=n_estimators, max_depth=max_depth, min_samples_leaf=min_samples_leaf, max_features=max_features)\n",
|
|
" train_preds, test_preds = get_preds(rf_model, X_train, X_test)\n",
|
|
" train_acc, test_acc = print_accuracy(y_train, y_test, train_preds, test_preds)\n",
|
|
" return rf_model, train_preds, test_preds, train_acc, test_acc"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "1LY4nc3NstBp",
|
|
"outputId": "952f4ada-ad9c-4962-c561-c30e8cd0189a"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_1, trn_preds_1, tst_preds_1, trn_acc_1, tst_acc_1 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=20, max_depth=None, min_samples_leaf=1, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "xicoU1yUstHY",
|
|
"outputId": "320cd548-4340-4f08-c67a-5b1adeec2568"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_2, trn_preds_2, tst_preds_2, trn_acc_2, tst_acc_2 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=None, min_samples_leaf=1, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "bS36lUWUb_t9",
|
|
"outputId": "6b6f3129-02e0-49db-9664-971b4a49eb21"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_3, trn_preds_3, tst_preds_3, trn_acc_3, tst_acc_3 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=5, min_samples_leaf=1, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "iSn7GKK0cRKw",
|
|
"outputId": "0a39aee4-fc50-4437-e954-5d9932f75071"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_4, trn_preds_4, tst_preds_4, trn_acc_4, tst_acc_4 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=10, min_samples_leaf=1, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "7DIqN4IHcbj-",
|
|
"outputId": "7574f556-112b-445f-9e5e-2e2db2d4e56d"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_5, trn_preds_5, tst_preds_5, trn_acc_5, tst_acc_5 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=10, min_samples_leaf=10, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "pDXu7LTpdJOW",
|
|
"outputId": "6767af2a-becf-4896-c9a9-50044e5d79ec"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_6, trn_preds_6, tst_preds_6, trn_acc_6, tst_acc_6 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=10, min_samples_leaf=50, max_features='sqrt')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "HnZruTmidXnA",
|
|
"outputId": "871bde15-9a37-45e4-98c3-9d94a06127a4"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_7, trn_preds_7, tst_preds_7, trn_acc_7, tst_acc_7 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=10, min_samples_leaf=50, max_features=0.5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 51
|
|
},
|
|
"colab_type": "code",
|
|
"id": "-EPXRoZvghcX",
|
|
"outputId": "f2700168-e00e-4880-b7f0-c042a8388d6b"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model_8, trn_preds_8, tst_preds_8, trn_acc_8, tst_acc_8 = fit_predict_rf(X_train, X_test, y_train, y_test, random_state=888, n_estimators=50, max_depth=10, min_samples_leaf=50, max_features=0.3)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "Activity4_1.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
|
|
}
|