Skip to content

Instantly share code, notes, and snippets.

@Neeratyoy
Created October 25, 2019 15:45
Show Gist options
  • Save Neeratyoy/b92d380ac4631ced4a09542da3105fe2 to your computer and use it in GitHub Desktop.
Save Neeratyoy/b92d380ac4631ced4a09542da3105fe2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn import datasets\n",
"from sklearn.svm import SVC\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import cross_val_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### DATASET Component"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# To load IRIS dataset as a dataset module/component\n",
"def dataset():\n",
" X, y = datasets.load_iris(return_X_y=True)\n",
" return X, y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### TASK Component"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Tasks here define the number of cross-validation folds\n",
"# and the scoring metric to be used for evaluation\n",
"def task_1(f):\n",
" X, y = dataset() # loads IRIS\n",
" return cross_val_score(f, X, y, cv=5, \n",
" scoring='accuracy')\n",
"\n",
"def task_2(f):\n",
" X, y = dataset() # loads IRIS\n",
" return cross_val_score(f, X, y, cv=15, \n",
" scoring='balanced_accuracy')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### FLOW Component"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Flows determine the modelling technique to be applied\n",
"# Helps define a model irrespective of dataset or tasks\n",
"def flow_1():\n",
" clf = RandomForestClassifier(n_estimators=10, max_depth=2)\n",
" return clf\n",
"\n",
"def flow_2():\n",
" clf = SVC(gamma='auto', kernel='linear')\n",
" return clf "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### RUN Component"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Runs essentially evaluates a task-flow pairing \n",
"# and therefore in effect executs the modelling \n",
"# of a dataset as per the task task definition\n",
"def run(task, flow):\n",
" return task(flow)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RF using task 1: 0.94667; task 2: 0.94444\n",
"SVM using task 1: 0.98; task 2: 0.97222\n"
]
}
],
"source": [
"# Results for Random Forest\n",
"rf_task_1 = run(task_1, flow_1())\n",
"rf_task_2 = run(task_2, flow_1())\n",
"print(\"RF using task 1: {:<.5}; task 2: {:<.5}\".format(rf_task_1.mean(), rf_task_2.mean()))\n",
"\n",
"# Results for SVM\n",
"svm_task_1 = run(task_1, flow_2())\n",
"svm_task_2 = run(task_2, flow_2())\n",
"print(\"SVM using task 1: {:<.5}; task 2: {:<.5}\".format(svm_task_1.mean(), svm_task_2.mean()))"
]
}
],
"metadata": {
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment