-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
487 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,330 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Create a Simple SFrame Manually" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Import Turi Create\n", | ||
"Please follow the repository README instructions to install the Turi Create package.\n", | ||
"\n", | ||
"**Note**: Turi Create is currently only compatible with Python 2.7" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import turicreate as turi" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create an SFrame object\n", | ||
"An [`SFrame`](https://apple.github.io/turicreate/docs/api/generated/turicreate.SFrame.html) is an object that can handle and load large data. `SFrame` is essentially an ordered dictionary of multiple [`SArray`](https://apple.github.io/turicreate/docs/api/generated/turicreate.SArray.html#turicreate.SArray) objects. \n", | ||
"\n", | ||
"Each column in the `SFrame` is an `SArray` object.\n", | ||
"\n", | ||
"In the following `SFrame` object, we have created a data buffer (dataset) that specifies the size of each pet's eyes, nose, and head.\n", | ||
"\n", | ||
"Each number in the dataset is a ratio from 15cm.\n", | ||
"\n", | ||
"**Note**: The ratios in the dataset are NOT accurate or related to reality. Those are random ratios used for demonstration purposes." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"+------+------+------+-----------+\n", | ||
"| eyes | head | nose | pet_types |\n", | ||
"+------+------+------+-----------+\n", | ||
"| 0.23 | 0.34 | 0.11 | cat |\n", | ||
"| 0.64 | 0.47 | 0.68 | dog |\n", | ||
"| 0.89 | 0.66 | 0.78 | wolf |\n", | ||
"| 0.26 | 0.37 | 0.08 | cat |\n", | ||
"| 0.93 | 0.68 | 0.74 | wolf |\n", | ||
"| 0.66 | 0.45 | 0.57 | dog |\n", | ||
"+------+------+------+-----------+\n", | ||
"[6 rows x 4 columns]\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"dataBuffer = turi.SFrame({\n", | ||
" \"pet_types\": [\"cat\", \"dog\", \"wolf\", \"cat\", \"wolf\", \"dog\"],\n", | ||
" \"eyes\": [0.23, 0.64, 0.89, 0.26, 0.93, 0.66 ],\n", | ||
" \"nose\": [0.11, 0.68, 0.78, 0.08, 0.74, 0.57 ],\n", | ||
" \"head\": [0.34, 0.47, 0.66, 0.37, 0.68, 0.45 ]\n", | ||
"})\n", | ||
"print dataBuffer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create a nearest neighbor classifier\n", | ||
"**Turi Create** provides us with methods to create k-nearest neighbor models and classifiers.\n", | ||
"\n", | ||
"The k-nearest neighbor algorithm is one of the most essential algorithms in machine learning for both classification and regression. Although its concept is basic, but it's commonly used to this day in many applications that require pattern recognition. k-NN can also can be used in games, data analysis, and even image classification!\n", | ||
"\n", | ||
"In the following line of code, we have created a k-nearest neighbor classifier that will predict whether an input is a cat, dog, or wolf.\n", | ||
"\n", | ||
"We are using the property provided by Turi Create called [`nearest_neighbor_classifier`](https://apple.github.io/turicreate/docs/api/generated/turicreate.nearest_neighbor_classifier.NearestNeighborClassifier.html#turicreate.nearest_neighbor_classifier.NearestNeighborClassifier)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>Starting ball tree nearest neighbors model training.</pre>" | ||
], | ||
"text/plain": [ | ||
"Starting ball tree nearest neighbors model training." | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>| Tree level | Elapsed Time |</pre>" | ||
], | ||
"text/plain": [ | ||
"| Tree level | Elapsed Time |" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>| 0 | 72us |</pre>" | ||
], | ||
"text/plain": [ | ||
"| 0 | 72us |" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"classifier = turi.nearest_neighbor_classifier.create(dataBuffer, target=\"pet_types\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Edit the input\n", | ||
"This variable is an `SFrame` object that will be used as an input to predictnwhether an input is a cat, dog, or wolf.\n", | ||
"\n", | ||
"You may edit the numbers below but make sure it's a value between 0 to 1." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"userinput = turi.SFrame({\n", | ||
" \"eyes\": [0.32],\n", | ||
" \"nose\": [0.20],\n", | ||
" \"head\": [0.42]\n", | ||
"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Predict the input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+--------------+-------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+--------------+-------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>| Query points | % Complete. | Elapsed Time |</pre>" | ||
], | ||
"text/plain": [ | ||
"| Query points | % Complete. | Elapsed Time |" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+--------------+-------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+--------------+-------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>| 1 | 100 | 265us |</pre>" | ||
], | ||
"text/plain": [ | ||
"| 1 | 100 | 265us |" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>| Done | | 379us |</pre>" | ||
], | ||
"text/plain": [ | ||
"| Done | | 379us |" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre>+--------------+-------------+--------------+</pre>" | ||
], | ||
"text/plain": [ | ||
"+--------------+-------------+--------------+" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"+-------+-------------+\n", | ||
"| class | probability |\n", | ||
"+-------+-------------+\n", | ||
"| cat | 1.0 |\n", | ||
"+-------+-------------+\n", | ||
"[1 rows x 2 columns]\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"result = classifier.classify(userinput, max_neighbors=2)\n", | ||
"print result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.