{ "cells": [ { "cell_type": "markdown", "id": "650e8268", "metadata": {}, "source": [ "# Random Forest" ] }, { "cell_type": "code", "execution_count": null, "id": "4c638f04", "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_iris\n", "iris=load_iris()" ] }, { "cell_type": "code", "execution_count": 2, "id": "e711262a", "metadata": {}, "outputs": [], "source": [ "x,y=iris.data,iris.target" ] }, { "cell_type": "code", "execution_count": 17, "id": "638bbf11", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=150)" ] }, { "cell_type": "code", "execution_count": 5, "id": "1518fbc2", "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "array([[5.5, 2.4, 3.7, 1. ],\n", " [5.7, 2.8, 4.1, 1.3],\n", " [6. , 2.2, 5. , 1.5],\n", " [4.8, 3. , 1.4, 0.1],\n", " [5.4, 3.9, 1.3, 0.4],\n", " [6.4, 3.2, 4.5, 1.5],\n", " [5.1, 3.8, 1.6, 0.2],\n", " [5.5, 2.5, 4. , 1.3],\n", " [6.3, 3.4, 5.6, 2.4],\n", " [5.8, 2.8, 5.1, 2.4],\n", " [4.5, 2.3, 1.3, 0.3],\n", " [5.5, 2.6, 4.4, 1.2],\n", " [7.1, 3. , 5.9, 2.1],\n", " [7.2, 3.6, 6.1, 2.5],\n", " [4.9, 3.6, 1.4, 0.1],\n", " [4.6, 3.4, 1.4, 0.3],\n", " [5. , 3. , 1.6, 0.2],\n", " [5.1, 3.7, 1.5, 0.4],\n", " [5.8, 2.6, 4. , 1.2],\n", " [4.9, 3.1, 1.5, 0.1],\n", " [5.1, 3.3, 1.7, 0.5],\n", " [5. , 3.2, 1.2, 0.2],\n", " [6.5, 2.8, 4.6, 1.5],\n", " [7.9, 3.8, 6.4, 2. ],\n", " [6.1, 3. , 4.9, 1.8],\n", " [5.4, 3. , 4.5, 1.5],\n", " [6.4, 2.7, 5.3, 1.9],\n", " [5.7, 2.9, 4.2, 1.3],\n", " [7.7, 3.8, 6.7, 2.2],\n", " [6.5, 3.2, 5.1, 2. ],\n", " [5.8, 2.7, 3.9, 1.2],\n", " [4.6, 3.6, 1. , 0.2],\n", " [6.9, 3.1, 5.4, 2.1],\n", " [6.7, 3.3, 5.7, 2.1],\n", " [6.3, 2.8, 5.1, 1.5],\n", " [5.5, 4.2, 1.4, 0.2],\n", " [4.4, 3.2, 1.3, 0.2],\n", " [5.8, 2.7, 5.1, 1.9],\n", " [5.4, 3.9, 1.7, 0.4],\n", " [5.5, 3.5, 1.3, 0.2],\n", " [5. , 3.5, 1.6, 0.6],\n", " [6.9, 3.1, 4.9, 1.5],\n", " [6.5, 3. , 5.8, 2.2],\n", " [6.7, 3.3, 5.7, 2.5],\n", " [6.1, 2.6, 5.6, 1.4],\n", " [5.4, 3.7, 1.5, 0.2],\n", " [6. , 3.4, 4.5, 1.6],\n", " [5.9, 3.2, 4.8, 1.8],\n", " [4.6, 3.1, 1.5, 0.2],\n", " [6.8, 2.8, 4.8, 1.4],\n", " [4.9, 2.4, 3.3, 1. ],\n", " [6.2, 2.8, 4.8, 1.8],\n", " [5.1, 3.5, 1.4, 0.2],\n", " [6. , 2.9, 4.5, 1.5],\n", " [5.6, 3. , 4.1, 1.3],\n", " [6. , 2.7, 5.1, 1.6],\n", " [7. , 3.2, 4.7, 1.4],\n", " [6.2, 2.2, 4.5, 1.5],\n", " [5.7, 3. , 4.2, 1.2],\n", " [6.4, 2.8, 5.6, 2.2],\n", " [5.7, 2.5, 5. , 2. ],\n", " [4.3, 3. , 1.1, 0.1],\n", " [6.3, 2.5, 4.9, 1.5],\n", " [5.1, 3.5, 1.4, 0.3],\n", " [6.4, 2.9, 4.3, 1.3],\n", " [7.2, 3. , 5.8, 1.6],\n", " [6.4, 3.1, 5.5, 1.8],\n", " [4.9, 2.5, 4.5, 1.7],\n", " [5.6, 2.9, 3.6, 1.3],\n", " [5.7, 3.8, 1.7, 0.3],\n", " [5.1, 3.8, 1.9, 0.4],\n", " [4.4, 3. , 1.3, 0.2],\n", " [5.1, 3.4, 1.5, 0.2],\n", " [5.6, 2.8, 4.9, 2. ],\n", " [5.3, 3.7, 1.5, 0.2],\n", " [4.8, 3.1, 1.6, 0.2],\n", " [6.3, 3.3, 4.7, 1.6],\n", " [5.2, 3.5, 1.5, 0.2],\n", " [6.7, 3.1, 5.6, 2.4],\n", " [6.1, 2.9, 4.7, 1.4],\n", " [6.9, 3.1, 5.1, 2.3],\n", " [5.1, 3.8, 1.5, 0.3],\n", " [5.8, 2.7, 5.1, 1.9],\n", " [7.6, 3. , 6.6, 2.1],\n", " [4.7, 3.2, 1.3, 0.2],\n", " [5.5, 2.4, 3.8, 1.1],\n", " [6.1, 2.8, 4. , 1.3],\n", " [5.7, 2.8, 4.5, 1.3],\n", " [6.8, 3.2, 5.9, 2.3],\n", " [5.9, 3. , 4.2, 1.5],\n", " [6.7, 3.1, 4.4, 1.4],\n", " [4.6, 3.2, 1.4, 0.2],\n", " [5. , 3.3, 1.4, 0.2],\n", " [5. , 3.4, 1.5, 0.2],\n", " [6.5, 3. , 5.2, 2. ],\n", " [5.2, 2.7, 3.9, 1.4],\n", " [6.1, 3. , 4.6, 1.4],\n", " [5. , 3.6, 1.4, 0.2],\n", " [6.3, 3.3, 6. , 2.5],\n", " [6.7, 2.5, 5.8, 1.8],\n", " [7.4, 2.8, 6.1, 1.9],\n", " [6.7, 3.1, 4.7, 1.5],\n", " [5. , 2.3, 3.3, 1. ],\n", " [6.6, 2.9, 4.6, 1.3],\n", " [5. , 2. , 3.5, 1. ],\n", " [7.3, 2.9, 6.3, 1.8],\n", " [6.2, 3.4, 5.4, 2.3],\n", " [4.9, 3.1, 1.5, 0.2],\n", " [5.8, 4. , 1.2, 0.2],\n", " [5.6, 3. , 4.5, 1.5],\n", " [5.5, 2.3, 4. , 1.3],\n", " [5.1, 2.5, 3. , 1.1],\n", " [5.6, 2.7, 4.2, 1.3],\n", " [6. , 3. , 4.8, 1.8],\n", " [5.7, 2.6, 3.5, 1. ],\n", " [6.3, 2.3, 4.4, 1.3],\n", " [6.3, 2.9, 5.6, 1.8],\n", " [5.8, 2.7, 4.1, 1. ],\n", " [4.8, 3.4, 1.9, 0.2],\n", " [4.4, 2.9, 1.4, 0.2]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train" ] }, { "cell_type": "code", "execution_count": 18, "id": "ce8f4d0c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 1, 0, 2, 1, 0, 0, 0, 0, 2, 2,\n", " 0, 2, 1, 2, 0, 1, 1, 1, 0, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 0, 1, 1,\n", " 1, 1, 1, 2, 1, 0, 2, 1, 0, 0, 0, 0, 0, 2, 2, 0, 2, 1, 0, 0, 1, 1,\n", " 1, 2, 2, 0, 0, 0, 2, 1, 2, 2, 2, 2, 1, 2, 0, 2, 2, 1, 0, 1, 1, 0,\n", " 0, 2, 1, 2, 1, 2, 0, 1, 1, 2, 2, 0, 0, 2, 0, 0, 2, 1, 1, 2, 2, 0,\n", " 0, 0, 2, 2, 1, 2, 2, 2, 1, 0])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train" ] }, { "cell_type": "code", "execution_count": 7, "id": "289a4d3c", "metadata": {}, "outputs": [], "source": [ "#Random Forest classification\n", "from sklearn.ensemble import RandomForestClassifier" ] }, { "cell_type": "code", "execution_count": 19, "id": "bc2b28f8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RandomForestClassifier(n_estimators=30)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestClassifier\n", "classifier = RandomForestClassifier(n_estimators=30)\n", "classifier.fit(x_train,y_train)" ] }, { "cell_type": "code", "execution_count": 20, "id": "40ba2646", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 96.66666666666667\n", "Confusion Matrix: [[ 9 0 0]\n", " [ 0 11 1]\n", " [ 0 0 9]]\n", "Report : precision recall f1-score support\n", "\n", " 0 1.00 1.00 1.00 9\n", " 1 1.00 0.92 0.96 12\n", " 2 0.90 1.00 0.95 9\n", "\n", " accuracy 0.97 30\n", " macro avg 0.97 0.97 0.97 30\n", "weighted avg 0.97 0.97 0.97 30\n", "\n" ] } ], "source": [ "from sklearn.metrics import accuracy_score\n", "from sklearn.metrics import confusion_matrix\n", "from sklearn.metrics import classification_report\n", "y_pred=classifier.predict(x_test)\n", "accuracy=accuracy_score(y_test,y_pred)\n", "print(\"Accuracy:\",(accuracy)*100)\n", "print(\"Confusion Matrix: \",confusion_matrix(y_test,y_pred))\n", "print(\"Report :\",classification_report(y_test,y_pred))" ] }, { "cell_type": "code", "execution_count": 21, "id": "57dad48d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RandomForestClassifier(n_estimators=20)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier = RandomForestClassifier(n_estimators=20)\n", "classifier.fit(x_train,y_train)" ] }, { "cell_type": "code", "execution_count": 22, "id": "0c13e041", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 96.66666666666667\n", "Confusion Matrix: [[ 9 0 0]\n", " [ 0 11 1]\n", " [ 0 0 9]]\n", "Report : precision recall f1-score support\n", "\n", " 0 1.00 1.00 1.00 9\n", " 1 1.00 0.92 0.96 12\n", " 2 0.90 1.00 0.95 9\n", "\n", " accuracy 0.97 30\n", " macro avg 0.97 0.97 0.97 30\n", "weighted avg 0.97 0.97 0.97 30\n", "\n" ] } ], "source": [ "y_pred=classifier.predict(x_test)\n", "accuracy=accuracy_score(y_test,y_pred)\n", "print(\"Accuracy:\",(accuracy)*100)\n", "print(\"Confusion Matrix: \",confusion_matrix(y_test,y_pred))\n", "print(\"Report :\",classification_report(y_test,y_pred))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }