Files
fenago f3b24b4b7f added
2021-02-07 15:16:01 +05:00

228 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "6GidYkS3bVYK"
},
"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": "9eyKK38Rbanb"
},
"outputs": [],
"source": [
"file_url = '../Dataset/openml_phpZNNasq.csv'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_id92wzgbc4Y"
},
"outputs": [],
"source": [
"df = pd.read_csv(file_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "VeD8BgUcb3cD"
},
"outputs": [],
"source": [
"df.drop(columns='animal', inplace=True)\n",
"y = df.pop('type')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "wiSxDUrVd5ZE"
},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.4, random_state=188)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "9uqyaQ_Sd5ZB",
"outputId": "37f61ce7-2919-46e2-b02f-06104b194cad"
},
"outputs": [],
"source": [
"rf_model = RandomForestClassifier(random_state=42, n_estimators=30, max_depth=5)\n",
"rf_model.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "4LQz_9AId5ZA"
},
"outputs": [],
"source": [
"train_preds = rf_model.predict(X_train)\n",
"test_preds = rf_model.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "aMN0ckfjd5Y8"
},
"outputs": [],
"source": [
"train_acc = accuracy_score(y_train, train_preds)\n",
"test_acc = accuracy_score(y_test, test_preds)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"id": "fd8CL_NMd5Y0",
"outputId": "4aa6f1bf-e512-4414-b65f-5b37550af617"
},
"outputs": [],
"source": [
"print(train_acc)\n",
"print(test_acc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "83LhoI9_d5Yw",
"outputId": "1fdd9b2d-e41b-4719-c9fa-698996f32ccb"
},
"outputs": [],
"source": [
"rf_model2 = RandomForestClassifier(random_state=42, n_estimators=30, max_depth=2)\n",
"rf_model2.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "dQogC4wjiuAv"
},
"outputs": [],
"source": [
"train_preds2 = rf_model2.predict(X_train)\n",
"test_preds2 = rf_model2.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "aKMos2Ooi_qJ"
},
"outputs": [],
"source": [
"train_acc2 = accuracy_score(y_train, train_preds2)\n",
"test_acc2 = accuracy_score(y_test, test_preds2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"id": "39QZnnn1i4pT",
"outputId": "42047520-00c7-46f5-e48e-7ae62738b9b3"
},
"outputs": [],
"source": [
"print(train_acc2)\n",
"print(test_acc2)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Exercise4_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
}