mirror of
https://github.com/fenago/data-science.git
synced 2026-05-10 11:30:53 +00:00
228 lines
4.6 KiB
Plaintext
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": "1da557b1-1792-4108-b652-b699514a2f02"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model = RandomForestClassifier(random_state=42, n_estimators=1)\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": "10ad2494-aa58-47b4-9307-6c7656cdb2bb"
|
|
},
|
|
"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": "83cf8edf-4739-4f04-b245-7a68dd8befd9"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rf_model2 = RandomForestClassifier(random_state=42, n_estimators=30)\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": "12c83adf-d0a4-41a5-adf6-f7959594298f"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(train_acc2)\n",
|
|
"print(test_acc2)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "Exercise4_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
|
|
}
|