{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic core" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module contains all the basic functions we need in other modules of the fastai library (split with [`torch_core`](/torch_core.html#torch_core) that contains the ones requiring pytorch). Its documentation can easily be skipped at a first read, unless you want to know what a given function does." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.core import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global constants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`default_cpus = min(16, num_cpus())`
[source]
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

has_arg[source][test]

\n", "\n", "> has_arg(**`func`**, **`arg`**) → `bool`\n", "\n", "
×

No tests found for has_arg. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Check if `func` accepts `arg`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(has_arg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Examples for two [`fastai.core`](/core.html#core) functions. Docstring shown before calling [`has_arg`](/core.html#has_arg) for reference\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_arg(download_url,'url')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_arg(index_row,'x')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_arg(index_row,'a')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

ifnone[source][test]

\n", "\n", "> ifnone(**`a`**:`Any`, **`b`**:`Any`) → `Any`\n", "\n", "
×

Tests found for ifnone:

  • pytest -sv tests/test_core.py::test_ifnone [source]

To run tests please refer to this guide.

\n", "\n", "`a` if `a` is not None, otherwise `b`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(ifnone)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "param,alt_param = None,5\n", "ifnone(param,alt_param)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "param,alt_param = None,[1,2,3]\n", "ifnone(param,alt_param)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

is1d[source][test]

\n", "\n", "> is1d(**`a`**:`Collection`\\[`T_co`\\]) → `bool`\n", "\n", "
×

Tests found for is1d:

  • pytest -sv tests/test_core.py::test_is1d [source]

To run tests please refer to this guide.

\n", "\n", "Return `True` if `a` is one-dimensional " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(is1d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1]\n", " [ 2 3]\n", " [ 4 5]\n", " [ 6 7]\n", " [ 8 9]\n", " [10 11]]\n", "False\n" ] } ], "source": [ "two_d_array = np.arange(12).reshape(6,2)\n", "print( two_d_array )\n", "print( is1d(two_d_array) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is1d(two_d_array.flatten())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

is_listy[source][test]

\n", "\n", "> is_listy(**`x`**:`Any`) → `bool`\n", "\n", "
×

Tests found for is_listy:

  • pytest -sv tests/test_core.py::test_listy [source]

To run tests please refer to this guide.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(is_listy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check if `x` is a `Collection`. `Tuple` or `List` qualify" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_data = [1,2,3]\n", "is_listy(some_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_data = (1,2,3)\n", "is_listy(some_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "some_data = 1024\n", "print( is_listy(some_data) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print( is_listy( [some_data] ) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'a': 1, 'b': 2, 'c': 3}\n", "dict_keys(['a', 'b', 'c'])\n" ] } ], "source": [ "some_data = dict([('a',1),('b',2),('c',3)])\n", "print( some_data )\n", "print( some_data.keys() )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n" ] } ], "source": [ "print( is_listy(some_data) )\n", "print( is_listy(some_data.keys()) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print( is_listy(list(some_data.keys())) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

is_tuple[source][test]

\n", "\n", "> is_tuple(**`x`**:`Any`) → `bool`\n", "\n", "
×

Tests found for is_tuple:

  • pytest -sv tests/test_core.py::test_tuple [source]

To run tests please refer to this guide.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(is_tuple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check if `x` is a `tuple`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print( is_tuple( [1,2,3] ) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print( is_tuple( (1,2,3) ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Collection related functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

arange_of[source][test]

\n", "\n", "> arange_of(**`x`**)\n", "\n", "
×

No tests found for arange_of. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Same as [`range_of`](/core.html#range_of) but returns an array. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(arange_of)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arange_of([5,6,7])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(arange_of([5,6,7]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

array[source][test]

\n", "\n", "> array(**`a`**, **`dtype`**:`type`=***`None`***, **\\*\\*`kwargs`**) → `ndarray`\n", "\n", "
×

Tests found for array:

Some other tests where array is used:

  • pytest -sv tests/test_core.py::test_arrays_split [source]
  • pytest -sv tests/test_core.py::test_even_mults [source]
  • pytest -sv tests/test_core.py::test_idx_dict [source]
  • pytest -sv tests/test_core.py::test_is1d [source]
  • pytest -sv tests/test_core.py::test_itembase_eq [source]
  • pytest -sv tests/test_core.py::test_itembase_hash [source]
  • pytest -sv tests/test_core.py::test_one_hot [source]
  • pytest -sv tests/test_torch_core.py::test_model_type [source]
  • pytest -sv tests/test_torch_core.py::test_tensor_array_monkey_patch [source]
  • pytest -sv tests/test_torch_core.py::test_tensor_with_ndarray [source]
  • pytest -sv tests/test_torch_core.py::test_to_detach [source]

To run tests please refer to this guide.

\n", "\n", "Same as `np.array` but also handles generators. `kwargs` are passed to `np.array` with `dtype`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that after we call the generator, we do not reset. So the [`array`](/core.html#array) call has 5 less entries than it would if we ran from the start of the generator." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100.01\n", "101.01\n", "102.01\n", "103.01\n", "104.01\n" ] } ], "source": [ "def data_gen():\n", " i = 100.01\n", " while i<200:\n", " yield i\n", " i += 1.\n", "\n", "ex_data_gen = data_gen()\n", "for _ in range(5):\n", " print(next(ex_data_gen))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([105.01, 106.01, 107.01, 108.01, ..., 196.01, 197.01, 198.01, 199.01])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array(ex_data_gen)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([100, 101, 102, 103, ..., 196, 197, 198, 199])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex_data_gen_int = data_gen()\n", "\n", "array(ex_data_gen_int,dtype=int) #Cast output to int array" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

arrays_split[source][test]

\n", "\n", "> arrays_split(**`mask`**:`ndarray`, **\\*`arrs`**:`NPArrayableList`) → `SplitArrayList`\n", "\n", "
×

Tests found for arrays_split:

  • pytest -sv tests/test_core.py::test_arrays_split [source]

To run tests please refer to this guide.

\n", "\n", "Given `arrs` is [a,b,...] and `mask`index - return[(a[mask],a[~mask]),(b[mask],b[~mask]),...]. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(arrays_split)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]\n", "[14 13 12 11 10 9 8 7 6 5 4 3 2 1 0]\n", "[False False False False False False False False False False False True True True True]\n" ] } ], "source": [ "data_a = np.arange(15)\n", "data_b = np.arange(15)[::-1]\n", "\n", "mask_a = (data_a > 10)\n", "print(data_a)\n", "print(data_b)\n", "print(mask_a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(array([11, 12, 13, 14]),),\n", " (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arrays_split(mask_a,data_a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(15, 2)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack([data_a,data_b]).transpose().shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(array([[11, 3],\n", " [12, 2],\n", " [13, 1],\n", " [14, 0]]),), (array([[ 0, 14],\n", " [ 1, 13],\n", " [ 2, 12],\n", " [ 3, 11],\n", " [ 4, 10],\n", " [ 5, 9],\n", " [ 6, 8],\n", " [ 7, 7],\n", " [ 8, 6],\n", " [ 9, 5],\n", " [10, 4]]),)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arrays_split(mask_a,np.vstack([data_a,data_b]).transpose()) #must match on dimension 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

chunks[source][test]

\n", "\n", "> chunks(**`l`**:`Collection`\\[`T_co`\\], **`n`**:`int`) → `Iterable`\n", "\n", "
×

Tests found for chunks:

  • pytest -sv tests/test_core.py::test_chunks [source]

To run tests please refer to this guide.

\n", "\n", "Yield successive `n`-sized chunks from `l`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(chunks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can transform a `Collection` into an `Iterable` of 'n' sized chunks by calling [`chunks`](/core.html#chunks):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1]\n", "[2, 3]\n", "[4, 5]\n", "[6, 7]\n", "[8, 9]\n" ] } ], "source": [ "data = [0,1,2,3,4,5,6,7,8,9]\n", "for chunk in chunks(data, 2):\n", " print(chunk)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2]\n", "[3, 4, 5]\n", "[6, 7, 8]\n", "[9]\n" ] } ], "source": [ "for chunk in chunks(data, 3):\n", " print(chunk)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

df_names_to_idx[source][test]

\n", "\n", "> df_names_to_idx(**`names`**:`IntsOrStrs`, **`df`**:`DataFrame`)\n", "\n", "
×

Tests found for df_names_to_idx:

  • pytest -sv tests/test_core.py::test_df_names_to_idx [source]

To run tests please refer to this guide.

\n", "\n", "Return the column indexes of `names` in `df`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(df_names_to_idx)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a b\n", "0 1 2\n", "1 1 2\n", "2 1 2\n" ] } ], "source": [ "ex_df = pd.DataFrame.from_dict({\"a\":[1,1,1],\"b\":[2,2,2]})\n", "print(ex_df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_names_to_idx('b',ex_df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

extract_kwargs[source][test]

\n", "\n", "> extract_kwargs(**`names`**:`StrList`, **`kwargs`**:`KWArgs`)\n", "\n", "
×

No tests found for extract_kwargs. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Extract the keys in `names` from the `kwargs`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(extract_kwargs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 2, 'some_list': [1, 2, 3], 'param': 'mean'}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "key_word_args = {\"a\":2,\"some_list\":[1,2,3],\"param\":'mean'}\n", "key_word_args" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'param': 'mean'} {'a': 2, 'some_list': [1, 2, 3]}\n" ] } ], "source": [ "(extracted_val,remainder) = extract_kwargs(['param'],key_word_args)\n", "print( extracted_val,remainder )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

idx_dict[source][test]

\n", "\n", "> idx_dict(**`a`**)\n", "\n", "
×

Tests found for idx_dict:

  • pytest -sv tests/test_core.py::test_idx_dict [source]

To run tests please refer to this guide.

\n", "\n", "Create a dictionary value to index from `a`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(idx_dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 0, 'b': 1, 'c': 2}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx_dict(['a','b','c'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

index_row[source][test]

\n", "\n", "> index_row(**`a`**:`Union`\\[`Collection`\\[`T_co`\\], `DataFrame`, `Series`\\], **`idxs`**:`Collection`\\[`int`\\]) → `Any`\n", "\n", "
×

No tests found for index_row. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Return the slice of `a` corresponding to `idxs`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(index_row)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`a` is basically something you can index into like a dataframe, an array or a list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [0,1,2,3,4,5,6,7,8,9]\n", "index_row(data,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "index_row(pd.Series(data),7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01
090
181
272
363
454
545
636
727
818
909
\n", "
" ], "text/plain": [ " 0 1\n", "0 9 0\n", "1 8 1\n", "2 7 2\n", "3 6 3\n", "4 5 4\n", "5 4 5\n", "6 3 6\n", "7 2 7\n", "8 1 8\n", "9 0 9" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df = pd.DataFrame([data[::-1],data]).transpose()\n", "data_df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2\n", "1 7\n", "Name: 7, dtype: int64" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "index_row(data_df,7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

listify[source][test]

\n", "\n", "> listify(**`p`**:`OptListOrItem`=***`None`***, **`q`**:`OptListOrItem`=***`None`***)\n", "\n", "
×

Tests found for listify:

  • pytest -sv tests/test_core.py::test_listify [source]

To run tests please refer to this guide.

\n", "\n", "Make `p` listy and the same length as `q`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(listify)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_match = np.arange(12)\n", "listify('a',to_match)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'a', 'a', 'a', 'a']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listify('a',5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[77.1, 77.1, 77.1]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listify(77.1,3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listify( (1,2,3) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listify((1,2,3),('a','b','c'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

random_split[source][test]

\n", "\n", "> random_split(**`valid_pct`**:`float`, **\\*`arrs`**:`NPArrayableList`) → `SplitArrayList`\n", "\n", "
×

Tests found for random_split:

  • pytest -sv tests/test_core.py::test_random_split [source]

To run tests please refer to this guide.

\n", "\n", "Randomly split `arrs` with `valid_pct` ratio. good for creating validation set. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(random_split)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Splitting is done here with `random.uniform()` so you may not get the exact split percentage for small data sets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7],\n", " [8, 9],\n", " [10, 11],\n", " [12, 13],\n", " [14, 15],\n", " [16, 17],\n", " [18, 19]]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.arange(20).reshape(10,2)\n", "data.tolist()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(array([[ 0, 1],\n", " [ 2, 3],\n", " [ 4, 5],\n", " [ 6, 7],\n", " [ 8, 9],\n", " [10, 11],\n", " [12, 13],\n", " [14, 15],\n", " [16, 17],\n", " [18, 19]]),), (array([], shape=(0, 2), dtype=int64),)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random_split(0.20,data.tolist())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(array([[ 0, 1],\n", " [ 4, 5],\n", " [ 8, 9],\n", " [10, 11],\n", " [16, 17],\n", " [18, 19]]),), (array([[ 2, 3],\n", " [ 6, 7],\n", " [12, 13],\n", " [14, 15]]),)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random_split(0.20,pd.DataFrame(data))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

range_of[source][test]

\n", "\n", "> range_of(**`x`**)\n", "\n", "
×

No tests found for range_of. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Create a range from 0 to `len(x)`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(range_of)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range_of([5,4,3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range_of(np.arange(10)[::-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

series2cat[source][test]

\n", "\n", "> series2cat(**`df`**:`DataFrame`, **\\*`col_names`**)\n", "\n", "
×

Tests found for series2cat:

  • pytest -sv tests/test_core.py::test_series2cat [source]

To run tests please refer to this guide.

\n", "\n", "Categorifies the columns `col_names` in `df`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(series2cat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
01f
11e
21f
32g
42g
52g
\n", "
" ], "text/plain": [ " a b\n", "0 1 f\n", "1 1 e\n", "2 1 f\n", "3 2 g\n", "4 2 g\n", "5 2 g" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df = pd.DataFrame.from_dict({\"a\":[1,1,1,2,2,2],\"b\":['f','e','f','g','g','g']})\n", "data_df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 f\n", "1 e\n", "2 f\n", "3 g\n", "4 g\n", "5 g\n", "Name: b, dtype: object" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df['b']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 f\n", "1 e\n", "2 f\n", "3 g\n", "4 g\n", "5 g\n", "Name: b, dtype: category\n", "Categories (3, object): [e < f < g]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "series2cat(data_df,'b')\n", "data_df['b']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 1\n", "2 1\n", "3 2\n", "4 2\n", "5 2\n", "Name: a, dtype: category\n", "Categories (2, int64): [1 < 2]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "series2cat(data_df,'a')\n", "data_df['a']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

split_kwargs_by_func[source][test]

\n", "\n", "> split_kwargs_by_func(**`kwargs`**, **`func`**)\n", "\n", "
×

No tests found for split_kwargs_by_func. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Split `kwargs` between those expected by `func` and the others. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(split_kwargs_by_func)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "({'url': 'http://fast.ai', 'dest': './'},\n", " {'new_var': [1, 2, 3], 'testvalue': 42})" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "key_word_args = {'url':'http://fast.ai','dest':'./','new_var':[1,2,3],'testvalue':42}\n", "split_kwargs_by_func(key_word_args,download_url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

to_int[source][test]

\n", "\n", "> to_int(**`b`**:`Any`) → `Union`\\[`int`, `List`\\[`int`\\]\\]\n", "\n", "
×

Tests found for to_int:

  • pytest -sv tests/test_core.py::test_to_int [source]

To run tests please refer to this guide.

\n", "\n", "Recursively convert `b` to an int or list/dict of ints; raises exception if not convertible. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(to_int)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_int(3.1415)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 7]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [1.2,3.4,7.25]\n", "to_int(data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

uniqueify[source][test]

\n", "\n", "> uniqueify(**`x`**:`Series`, **`sort`**:`bool`=***`False`***) → `List`\\[`T`\\]\n", "\n", "
×

Tests found for uniqueify:

  • pytest -sv tests/test_core.py::test_uniqueify [source]

To run tests please refer to this guide.

\n", "\n", "Return sorted unique values of `x`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(uniqueify)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'f', 'g']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uniqueify( pd.Series(data=['a','a','b','b','f','g']) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Metaclasses" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

class PrePostInitMeta[source][test]

\n", "\n", "> PrePostInitMeta(**`name`**, **`bases`**, **`dct`**) :: `type`\n", "\n", "
×

No tests found for PrePostInitMeta. To contribute a test please refer to this guide and this discussion.

\n", "\n", "A metaclass that calls optional `__pre_init__` and `__post_init__` methods " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(PrePostInitMeta)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class _T(metaclass=PrePostInitMeta):\n", " def __pre_init__(self): self.a = 0; assert self.a==0\n", " def __init__(self): self.a += 1; assert self.a==1\n", " def __post_init__(self): self.a += 1; assert self.a==2\n", "\n", "t = _T()\n", "t.a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Files management and downloads" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

download_url[source][test]

\n", "\n", "> download_url(**`url`**:`str`, **`dest`**:`str`, **`overwrite`**:`bool`=***`False`***, **`pbar`**:`ProgressBar`=***`None`***, **`show_progress`**=***`True`***, **`chunk_size`**=***`1048576`***, **`timeout`**=***`4`***, **`retries`**=***`5`***)\n", "\n", "
×

Tests found for download_url:

  • pytest -sv tests/test_core.py::test_download_url [source]

To run tests please refer to this guide.

\n", "\n", "Download `url` to `dest` unless it exists and not `overwrite`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(download_url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

find_classes[source][test]

\n", "\n", "> find_classes(**`folder`**:`Path`) → `FilePathList`\n", "\n", "
×

Tests found for find_classes:

  • pytest -sv tests/test_core.py::test_find_classes [source]

To run tests please refer to this guide.

\n", "\n", "List of label subdirectories in imagenet-style `folder`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(find_classes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

join_path[source][test]

\n", "\n", "> join_path(**`fname`**:`PathOrStr`, **`path`**:`PathOrStr`=***`'.'`***) → `Path`\n", "\n", "
×

Tests found for join_path:

  • pytest -sv tests/test_core.py::test_join_paths [source]

To run tests please refer to this guide.

\n", "\n", "Return `Path(path)/Path(fname)`, `path` defaults to current dir. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(join_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

join_paths[source][test]

\n", "\n", "> join_paths(**`fnames`**:`FilePathList`, **`path`**:`PathOrStr`=***`'.'`***) → `FilePathList`\n", "\n", "
×

Tests found for join_paths:

Some other tests where join_paths is used:

  • pytest -sv tests/test_core.py::test_join_paths [source]

To run tests please refer to this guide.

\n", "\n", "Join `path` to every file name in `fnames`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(join_paths)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

loadtxt_str[source][test]

\n", "\n", "> loadtxt_str(**`path`**:`PathOrStr`) → `ndarray`\n", "\n", "
×

No tests found for loadtxt_str. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Return `ndarray` of `str` of lines of text from `path`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(loadtxt_str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

save_texts[source][test]

\n", "\n", "> save_texts(**`fname`**:`PathOrStr`, **`texts`**:`StrList`)\n", "\n", "
×

No tests found for save_texts. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Save in `fname` the content of `texts`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(save_texts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiprocessing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

num_cpus[source][test]

\n", "\n", "> num_cpus() → `int`\n", "\n", "
×

Tests found for num_cpus:

  • pytest -sv tests/test_core.py::test_cpus [source]

To run tests please refer to this guide.

\n", "\n", "Get number of cpus " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(num_cpus)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

parallel[source][test]

\n", "\n", "> parallel(**`func`**, **`arr`**:`Collection`\\[`T_co`\\], **`max_workers`**:`int`=***`None`***, **`leave`**=***`False`***)\n", "\n", "
×

No tests found for parallel. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Call `func` on every element of `arr` in parallel using `max_workers`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(parallel)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`func` must accept both the value and index of each `arr` element." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " \n", " 100.00% [5/5 00:00<00:00]\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Index: 0, Value: 0\n", "Index: 1, Value: 2\n", "Index: 2, Value: 4\n", "Index: 4, Value: 8\n", "Index: 3, Value: 6\n" ] } ], "source": [ "def my_func(value, index):\n", " print(\"Index: {}, Value: {}\".format(index, value))\n", " \n", "my_array = [i*2 for i in range(5)]\n", "parallel(my_func, my_array, max_workers=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

partition[source][test]

\n", "\n", "> partition(**`a`**:`Collection`\\[`T_co`\\], **`sz`**:`int`) → `List`\\[`Collection`\\[`T_co`\\]\\]\n", "\n", "
×

Tests found for partition:

  • pytest -sv tests/test_core.py::test_partition_functionality [source]

Some other tests where partition is used:

  • pytest -sv tests/test_core.py::test_partition [source]

To run tests please refer to this guide.

\n", "\n", "Split iterables `a` in equal parts of size `sz` " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(partition)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

partition_by_cores[source][test]

\n", "\n", "> partition_by_cores(**`a`**:`Collection`\\[`T_co`\\], **`n_cpus`**:`int`) → `List`\\[`Collection`\\[`T_co`\\]\\]\n", "\n", "
×

No tests found for partition_by_cores. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Split data in `a` equally among `n_cpus` cores " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(partition_by_cores)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data block API" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class ItemBase[source][test]

\n", "\n", "> ItemBase(**`data`**:`Any`)\n", "\n", "
×

Tests found for ItemBase:

Some other tests where ItemBase is used:

  • pytest -sv tests/test_core.py::test_itembase_eq [source]
  • pytest -sv tests/test_core.py::test_itembase_hash [source]

To run tests please refer to this guide.

\n", "\n", "Base item type in the fastai library. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(ItemBase, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All items used in fastai should subclass this. Must have a [`data`](/tabular.data.html#tabular.data) field that will be used when collating in mini-batches." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

apply_tfms[source][test]

\n", "\n", "> apply_tfms(**`tfms`**:`Collection`\\[`T_co`\\], **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for apply_tfms. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Subclass this method if you want to apply data augmentation with `tfms` to this [`ItemBase`](/core.html#ItemBase). " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(ItemBase.apply_tfms)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

show[source][test]

\n", "\n", "> show(**`ax`**:`Axes`, **\\*\\*`kwargs`**)\n", "\n", "
×

No tests found for show. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Subclass this method if you want to customize the way this [`ItemBase`](/core.html#ItemBase) is shown on `ax`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(ItemBase.show)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default behavior is to set the string representation of this object as title of `ax`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class Category[source][test]

\n", "\n", "> Category(**`data`**, **`obj`**) :: [`ItemBase`](/core.html#ItemBase)\n", "\n", "
×

Tests found for Category:

  • pytest -sv tests/test_core.py::test_itembase_eq [source]

Some other tests where Category is used:

  • pytest -sv tests/test_core.py::test_itembase_hash [source]

To run tests please refer to this guide.

\n", "\n", "Basic class for single classification labels. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(Category, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a [`Category`](/core.html#Category) with an `obj` of index [`data`](/tabular.data.html#tabular.data) in a certain classes list. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class EmptyLabel[source][test]

\n", "\n", "> EmptyLabel() :: [`ItemBase`](/core.html#ItemBase)\n", "\n", "
×

No tests found for EmptyLabel. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Should be used for a dummy label. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(EmptyLabel, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class MultiCategory[source][test]

\n", "\n", "> MultiCategory(**`data`**, **`obj`**, **`raw`**) :: [`ItemBase`](/core.html#ItemBase)\n", "\n", "
×

Tests found for MultiCategory:

  • pytest -sv tests/test_core.py::test_itembase_eq [source]

Some other tests where MultiCategory is used:

  • pytest -sv tests/test_core.py::test_itembase_hash [source]

To run tests please refer to this guide.

\n", "\n", "Basic class for multi-classification labels. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(MultiCategory, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a [`MultiCategory`](/core.html#MultiCategory) with an `obj` that is a collection of labels. [`data`](/tabular.data.html#tabular.data) corresponds to the one-hot encoded labels and `raw` is a list of associated string." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class FloatItem[source][test]

\n", "\n", "> FloatItem(**`obj`**) :: [`ItemBase`](/core.html#ItemBase)\n", "\n", "
×

Tests found for FloatItem:

  • pytest -sv tests/test_core.py::test_itembase_eq [source]

Some other tests where FloatItem is used:

  • pytest -sv tests/test_core.py::test_itembase_hash [source]

To run tests please refer to this guide.

\n", "\n", "Basic class for float items. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(FloatItem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Others" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

camel2snake[source][test]

\n", "\n", "> camel2snake(**`name`**:`str`) → `str`\n", "\n", "
×

Tests found for camel2snake:

  • pytest -sv tests/test_core.py::test_camel2snake [source]

To run tests please refer to this guide.

\n", "\n", "Change `name` from camel to snake style. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(camel2snake)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'device_data_loader'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "camel2snake('DeviceDataLoader')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

even_mults[source][test]

\n", "\n", "> even_mults(**`start`**:`float`, **`stop`**:`float`, **`n`**:`int`) → `ndarray`\n", "\n", "
×

Tests found for even_mults:

  • pytest -sv tests/test_core.py::test_even_mults [source]

To run tests please refer to this guide.

\n", "\n", "Build log-stepped array from `start` to `stop` in `n` steps. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(even_mults)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In linear scales each element is equidistant from its neighbors:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 3.25, 5.5 , 7.75, 10. ])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from 1 to 10 in 5 steps\n", "t = np.linspace(1, 10, 5)\n", "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.25\n", "2.25\n", "2.25\n", "2.25\n" ] } ], "source": [ "for i in range(len(t) - 1):\n", " print(t[i+1] - t[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In logarithmic scales, each element is a multiple of the previous entry:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 1.778279, 3.162278, 5.623413, 10. ])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = even_mults(1, 10, 5)\n", "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7782794100389228\n", "1.7782794100389228\n", "1.7782794100389228\n", "1.7782794100389228\n" ] } ], "source": [ "# notice how each number is a multiple of its predecessor\n", "for i in range(len(t) - 1):\n", " print(t[i+1] / t[i])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

func_args[source][test]

\n", "\n", "> func_args(**`func`**) → `bool`\n", "\n", "
×

No tests found for func_args. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Return the arguments of `func`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(func_args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('url',\n", " 'dest',\n", " 'overwrite',\n", " 'pbar',\n", " 'show_progress',\n", " 'chunk_size',\n", " 'timeout',\n", " 'retries')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "func_args(download_url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally, [`func_args`](/core.html#func_args) can be used with functions that do not belong to the fastai library" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('start', 'stop', 'num', 'endpoint', 'retstep', 'dtype')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "func_args(np.linspace)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

noop[source][test]

\n", "\n", "> noop(**`x`**)\n", "\n", "
×

Tests found for noop:

  • pytest -sv tests/test_core.py::test_noop [source]

To run tests please refer to this guide.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(noop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Return `x`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# object is returned as-is\n", "noop([1,2,3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

one_hot[source][test]

\n", "\n", "> one_hot(**`x`**:`Collection`\\[`int`\\], **`c`**:`int`)\n", "\n", "
×

Tests found for one_hot:

  • pytest -sv tests/test_core.py::test_one_hot [source]

To run tests please refer to this guide.

\n", "\n", "One-hot encode `x` with `c` classes. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(one_hot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One-hot encoding is a standard machine learning technique. Assume we are dealing with a 10-class classification problem and we are supplied a list of labels:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = [1, 4, 4, 5, 7, 9, 2, 4, 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "
Note: y is zero-indexed, therefore its first element (1) belongs to class 2, its second element (4) to class 5 and so on.
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "jekyll_note(\"\"\"y is zero-indexed, therefore its first element (1) belongs to class 2, its second element (4) to class 5 and so on.\"\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "y can equivalently be expressed as a matrix of 9 rows and 10 columns, where each row represents one element of the original y. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n", "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n", "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n", "[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n", "[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]\n", "[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", "[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]\n", "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n", "[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" ] } ], "source": [ "for label in y:\n", " print(one_hot(label, 10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

show_some[source][test]

\n", "\n", "> show_some(**`items`**:`Collection`\\[`T_co`\\], **`n_max`**:`int`=***`5`***, **`sep`**:`str`=***`','`***)\n", "\n", "
×

No tests found for show_some. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Return the representation of the first `n_max` elements in `items`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(show_some)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10,20,30...'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select 3 elements from a list\n", "some_data = show_some([10, 20, 30, 40, 50], 3) \n", "some_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(some_data) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10---20---30...'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the separator can be changed\n", "some_data = show_some([10, 20, 30, 40, 50], 3, sep = '---') \n", "some_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10---20---30'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_data[:-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`show_some`](/core.html#show_some) can take as input any class with \\_\\_len\\_\\_ and \\_\\_getitem\\_\\_ " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'n,i...'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Any(object):\n", " def __init__(self, data):\n", " self.data = data\n", " def __len__(self):\n", " return len(self.data)\n", " def __getitem__(self,i):\n", " return self.data[i]\n", " \n", "some_other_data = Any('nice')\n", "show_some(some_other_data, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

subplots[source][test]

\n", "\n", "> subplots(**`rows`**:`int`, **`cols`**:`int`, **`imgsize`**:`int`=***`4`***, **`figsize`**:`Optional`\\[`Tuple`\\[`int`, `int`\\]\\]=***`None`***, **`title`**=***`None`***, **\\*\\*`kwargs`**)\n", "\n", "
×

Tests found for subplots:

  • pytest -sv tests/test_core.py::test_subplots_multi_row_cols [source]
  • pytest -sv tests/test_core.py::test_subplots_single [source]

To run tests please refer to this guide.

\n", "\n", "Like `plt.subplots` but with consistent axs shape, `kwargs` passed to `fig.suptitle` with `title` " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(subplots)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

text2html_table[source][test]

\n", "\n", "> text2html_table(**`items`**:`Tokens`) → `str`\n", "\n", "
×

No tests found for text2html_table. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Put the texts in `items` in an HTML table, `widths` are the widths of the columns in %. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(text2html_table)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New Methods - Please document or move to the undocumented section" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

is_dict[source][test]

\n", "\n", "> is_dict(**`x`**:`Any`) → `bool`\n", "\n", "
×

Tests found for is_dict:

  • pytest -sv tests/test_core.py::test_dict [source]

To run tests please refer to this guide.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(is_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "Basic helper functions for the fastai library", "title": "core" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }