Skip to content

Instantly share code, notes, and snippets.

@FavioVazquez
Last active May 29, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FavioVazquez/dbeb94e228df3988fd683f33b233e12e to your computer and use it in GitHub Desktop.
Save FavioVazquez/dbeb94e228df3988fd683f33b233e12e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install --user pysnooper"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Restart the kernel before trying the package"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fibonacci numbers\n",
"\n",
"The Fibonacci numbers are the numbers in the following integer sequence.\n",
"0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..\n",
"\n",
"In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:\n",
"\n",
"$$\n",
"F_n = F_{n-1} + F_{n-2}\n",
"$$\n",
"\n",
"with $F_0 = 0$ and $F_1 = 1$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pysnooper\n",
"\n",
"@pysnooper.snoop()\n",
"def fibonacci(n): \n",
" if n<0: \n",
" print(\"Incorrect input\") \n",
" # First Fibonacci number is 0 \n",
" elif n==1: \n",
" return 0\n",
" # Second Fibonacci number is 1 \n",
" elif n==2: \n",
" return 1\n",
" else: \n",
" return fibonacci(n-1)+fibonacci(n-2)\n",
"\n",
"fibonacci(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see here all the recursion needed and the step by step process for the algorithm to work."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pandas stuff (Data from [here](https://github.com/guipsamora/pandas_exercises/blob/master/06_Stats/US_Baby_Names/Exercises_with_solutions.ipynb))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"baby_names = pd.read_csv('https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/06_Stats/US_Baby_Names/US_Baby_Names_right.csv')\n",
"baby_names.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def count_distinct_values(df, colname):\n",
" df_t = pd.DataFrame(df[colname].value_counts(dropna=False))\n",
" df_t.index.names = [colname]\n",
" df_t.columns = ['Count']\n",
" df_t.sort_index(inplace=True)\n",
" return df_t"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"count_distinct_values(baby_names, \"Name\").head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try it with PySnooper. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@pysnooper.snoop(depth=2)\n",
"def count_distinct_values_snooper(df, colname):\n",
" df_t = pd.DataFrame(df[colname].value_counts(dropna=False))\n",
" df_t.index.names = [colname]\n",
" df_t.columns = ['Count']\n",
" df_t.sort_index(inplace=True)\n",
" return df_t"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"count_distinct_values_snooper(baby_names, \"Name\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see a lot more information about what's the program doing. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!git status"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!git add ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!git commit -m \"Add PySnooper Notebook\""
]
}
],
"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