Skip to content

Instantly share code, notes, and snippets.

@bcyoungV
Created April 18, 2021 10:28
Show Gist options
  • Save bcyoungV/2514e0883670bb4ee54f66465d4c1826 to your computer and use it in GitHub Desktop.
Save bcyoungV/2514e0883670bb4ee54f66465d4c1826 to your computer and use it in GitHub Desktop.
Tree de Decision Simple en Python par IBM Cloud --Bruno Casaca
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"source": "#install graphviz for tree visualization\n!pip install graphviz",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/secretstorage/dhcrypto.py:16: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead\n from cryptography.utils import int_from_bytes\n/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/secretstorage/util.py:25: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead\n from cryptography.utils import int_from_bytes\nCollecting graphviz\n Downloading graphviz-0.16-py2.py3-none-any.whl (19 kB)\nInstalling collected packages: graphviz\nSuccessfully installed graphviz-0.16\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "#import required libraries\nfrom sklearn.tree import DecisionTreeClassifier \nfrom sklearn import tree \nfrom sklearn.preprocessing import LabelEncoder \nfrom sklearn.tree import export_graphviz\nfrom graphviz import Source\nfrom IPython.display import SVG\nimport pandas as pd \nimport numpy as np \nimport io\nimport requests",
"execution_count": 2,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "#Read training Dataset \nurl=\"https://raw.githubusercontent.com/dinasayed/mavenFirst/master/mavenFirst/PlayerFittness.csv\"\ndf=pd.read_csv(url)\nprint(df)",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": " Practice Abid_Dietary IsMotivated IsFit\n0 Moderate Yes Yes Yes\n1 good No Yes Yes\n2 No No Yes No\n3 Moderate No No No\n4 Moderate No Yes Yes\n5 Moderate No Yes Yes\n6 No Yes No No\n7 good Yes No No\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "# Prepare your dataset \nlb = LabelEncoder() \ndf['Practice_'] = lb.fit_transform(df['Practice']) \ndf['Abid_Dietary_'] = lb.fit_transform(df['Abid_Dietary'] ) \ndf['IsMotivated_'] = lb.fit_transform(df['IsMotivated'] ) \ndf['IsFit_'] = lb.fit_transform(df['IsFit'] ) \nX = df.iloc[:,4:7] \nY = df.iloc[:,7]\nprint(\"Features in numeric form\")\nprint(X)\nprint(\"*********************************************\")\nprint(\"Label in numeric form\")\nprint(Y)\n# Prepare a testcase\nX_test=[[0 for x in range(3)] for y in range(1)]\nX_test[0][0]=2\nX_test[0][1]=1\nX_test[0][2]=1\n#Train a Decision tree\nd_tree = DecisionTreeClassifier(criterion='entropy')\nd_tree.fit(X.astype(int), Y.astype(int))\n#Test using sample test X_test\ny_pred_en = d_tree.predict(X_test)\n#The prediction for X-test\nprint(\"The prediction result for test case, given features are: practice is moderate, Abid_Dietary is yes and IsMotivated is yes\")\nprint(y_pred_en)\n\n",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "#Visualize the decision tree\ngraph = Source(export_graphviz(d_tree, out_file=None, feature_names=X.columns))\nSVG(graph.pipe(format='svg'))",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment