diff --git a/numpy-examples/README.md b/numpy-examples/README.md new file mode 100644 index 0000000000..f6ed18d653 --- /dev/null +++ b/numpy-examples/README.md @@ -0,0 +1,3 @@ +These files are used in the tutorial [NumPy Practical Examples: Useful Techniques](https://realpython.com/numpy-example-useful-practical-techniques/). + +The various csv files contain data that is uses to create and populate various NumPy arrays in both the main tutorial and in the various embedded exercises. The tutorial_code.ipynb is a Jupyter Notebook containing the code used in the tutorials. \ No newline at end of file diff --git a/numpy-examples/cashed_checks.csv b/numpy-examples/cashed_checks.csv new file mode 100644 index 0000000000..577981e719 --- /dev/null +++ b/numpy-examples/cashed_checks.csv @@ -0,0 +1,8 @@ +Check_ID,Amount,Date_Cashed +1341,150.00,2024-04-12 +1342,175.00,2024-04-16 +1343,30.00,2024-04-12 +1345,65.00,2024-04-12 +1346,430.00,2024-04-08 +1349,500.00,2024-04-08 +1350,220.00,2024-04-15 \ No newline at end of file diff --git a/numpy-examples/check_list_duplicates.csv b/numpy-examples/check_list_duplicates.csv new file mode 100644 index 0000000000..02a3ddbf9d --- /dev/null +++ b/numpy-examples/check_list_duplicates.csv @@ -0,0 +1,12 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy-examples/ex1a.csv b/numpy-examples/ex1a.csv new file mode 100644 index 0000000000..e97fc94b35 --- /dev/null +++ b/numpy-examples/ex1a.csv @@ -0,0 +1,4 @@ +5,10,15,20 +25,30,35,40 +45,50,55,60 +65,70,75,80 \ No newline at end of file diff --git a/numpy-examples/ex1b.csv b/numpy-examples/ex1b.csv new file mode 100644 index 0000000000..37ffd0470b --- /dev/null +++ b/numpy-examples/ex1b.csv @@ -0,0 +1 @@ +1,3,5 \ No newline at end of file diff --git a/numpy-examples/ex1c.csv b/numpy-examples/ex1c.csv new file mode 100644 index 0000000000..cfa0b54952 --- /dev/null +++ b/numpy-examples/ex1c.csv @@ -0,0 +1,3 @@ +2 +4 +6 \ No newline at end of file diff --git a/numpy-examples/file1.csv b/numpy-examples/file1.csv new file mode 100644 index 0000000000..8dbfc4399b --- /dev/null +++ b/numpy-examples/file1.csv @@ -0,0 +1,2 @@ +1.1,1.2,1.3 +1.4,1.5,1.6 \ No newline at end of file diff --git a/numpy-examples/file2.csv b/numpy-examples/file2.csv new file mode 100644 index 0000000000..dabb7f3a8a --- /dev/null +++ b/numpy-examples/file2.csv @@ -0,0 +1,2 @@ +2.1,2.2,2.3 +2.4,2.5,2.6 \ No newline at end of file diff --git a/numpy-examples/file3.csv b/numpy-examples/file3.csv new file mode 100644 index 0000000000..497c31700f --- /dev/null +++ b/numpy-examples/file3.csv @@ -0,0 +1,2 @@ +3.1,3.2,3.3 +3.4,3.5,3.6 \ No newline at end of file diff --git a/numpy-examples/issued_checks.csv b/numpy-examples/issued_checks.csv new file mode 100644 index 0000000000..79ebe66d15 --- /dev/null +++ b/numpy-examples/issued_checks.csv @@ -0,0 +1,11 @@ +Check_ID,Payee,Amount,Date_Issued +1341,K.Starmer,150.00,2024-03-29 +1342,R Sunak,175.00,2024-03-29 +1343,L Truss,30.00,2024-03-29 +1344,B Johnson,45.00,2024-03-22 +1345,T May,65.00,2024-03-22 +1346,D Cameron,430.00,2024-03-22 +1347,G Brown,100.00,2024-03-15 +1348,T Blair,250.00,2024-03-15 +1349,J Major,500.00,2024-03-15 +1350,M Thatcher,220.00,2024-03-15 \ No newline at end of file diff --git a/numpy-examples/london_temperatures.csv b/numpy-examples/london_temperatures.csv new file mode 100644 index 0000000000..ce1d205e15 --- /dev/null +++ b/numpy-examples/london_temperatures.csv @@ -0,0 +1,12 @@ +Jan,5 +Feb,7 +Mar,9 +Apr,11 +May,14 +Jun,16 +Jul,19 +Aug,19 +Sep,17 +Oct,13 +Nov,10 +Dec,7 \ No newline at end of file diff --git a/numpy-examples/long_file.csv b/numpy-examples/long_file.csv new file mode 100644 index 0000000000..0e63b07cb6 --- /dev/null +++ b/numpy-examples/long_file.csv @@ -0,0 +1,3 @@ +4.1,4.2,4.3 +4.4,4.5,4.6 +4.7,4.8,4.9 \ No newline at end of file diff --git a/numpy-examples/new_york_temperatures.csv b/numpy-examples/new_york_temperatures.csv new file mode 100644 index 0000000000..4ef0f4d9fd --- /dev/null +++ b/numpy-examples/new_york_temperatures.csv @@ -0,0 +1,12 @@ +Jan,2 +Feb,2 +Mar,4 +Apr,11 +May,16 +Jun,22 +Jul,25 +Aug,24 +Sep,20 +Oct,14 +Nov,12 +Dec,9 \ No newline at end of file diff --git a/numpy-examples/passengers.csv b/numpy-examples/passengers.csv new file mode 100644 index 0000000000..d5b924e62b --- /dev/null +++ b/numpy-examples/passengers.csv @@ -0,0 +1,22 @@ +passenger_no,first_name,last_name +1,Olivia,Smith +2,Amelia,Jones +3,Isla,Williams +4,Ava,Taylor +5,Ivy,Brown +6,Freya,Davies +7,Lily,Evans +8,Florence,Wilson +9,Mia,Thomas +10,Willow,Johnson +11,Noah,Roberts +12,Oliver,Robinson +13,George,Thompson +14,Arthur,Wright +14,Arthur,Wright +15,Muhammad,Walker +16,Leo,White +17,Harry,Edwards +18,Oscar,Hughes +19,Archie,Green +20,Henry,Hall \ No newline at end of file diff --git a/numpy-examples/passports.csv b/numpy-examples/passports.csv new file mode 100644 index 0000000000..729d3351f9 --- /dev/null +++ b/numpy-examples/passports.csv @@ -0,0 +1,22 @@ +passport_no,passenger_no,nationality +123656489,1,British +373456789,2,British +493456399,3,American +375456228,4,American +457345975,5,Austrian +345957363,6,Norewegian +567546577,7,French +453667456,8,German +456785778,9,Danish +456785778,9,Danish +679857668,10,Dutch +456657867,11,Dutch +467775488,12,French +884393487,13,Danish +122647657,15,Dutch +534677673,16,British +786737688,17,American +654672278,18,Spanish +683637288,19,Norwegian +768357788,20,British +768357788,21,American \ No newline at end of file diff --git a/numpy-examples/portfolio.csv b/numpy-examples/portfolio.csv new file mode 100644 index 0000000000..5e1e766100 --- /dev/null +++ b/numpy-examples/portfolio.csv @@ -0,0 +1,7 @@ +Company,Sector +Company_A,technology +Company_B,finance +Company_C,healthcare +Company_D,technology +Company_E,finance +Company_F,healthcare \ No newline at end of file diff --git a/numpy-examples/rome_temperatures.csv b/numpy-examples/rome_temperatures.csv new file mode 100644 index 0000000000..d70c14343a --- /dev/null +++ b/numpy-examples/rome_temperatures.csv @@ -0,0 +1,12 @@ +Jan,8 +Feb,9 +Mar,12 +Apr,14 +May,21 +Jun,23 +Jul,26 +Aug,24 +Sep,22 +Oct,18 +Nov,13 +Dec,10 \ No newline at end of file diff --git a/numpy-examples/share_prices-1.csv b/numpy-examples/share_prices-1.csv new file mode 100644 index 0000000000..9e6342db35 --- /dev/null +++ b/numpy-examples/share_prices-1.csv @@ -0,0 +1,7 @@ +Company,mon +Company_A,100.5 +Company_B,200.1 +Company_C,50.3 +Company_D,110.5 +Company_E,200.1 +Company_F,55.3 \ No newline at end of file diff --git a/numpy-examples/share_prices-2.csv b/numpy-examples/share_prices-2.csv new file mode 100644 index 0000000000..b29208eade --- /dev/null +++ b/numpy-examples/share_prices-2.csv @@ -0,0 +1,7 @@ +Company,tue +Company_A,101.2 +Company_B,199.8 +Company_C,50.5 +Company_D,101.2 +Company_E,200.8 +Company_F,50.5 \ No newline at end of file diff --git a/numpy-examples/share_prices-3.csv b/numpy-examples/share_prices-3.csv new file mode 100644 index 0000000000..7cce3ec69a --- /dev/null +++ b/numpy-examples/share_prices-3.csv @@ -0,0 +1,7 @@ +Company,Wed +Company_A,102 +Company_B,200.5 +Company_C,51.0 +Company_D,102 +Company_E,200.5 +Company_F,53.0 \ No newline at end of file diff --git a/numpy-examples/share_prices-4.csv b/numpy-examples/share_prices-4.csv new file mode 100644 index 0000000000..c7d58df53f --- /dev/null +++ b/numpy-examples/share_prices-4.csv @@ -0,0 +1,7 @@ +Company,Thu +Company_A,101.8 +Company_B,201.0 +Company_C,50.8 +Company_D,111.8 +Company_E,211.0 +Company_F,50.8 \ No newline at end of file diff --git a/numpy-examples/share_prices-5.csv b/numpy-examples/share_prices-5.csv new file mode 100644 index 0000000000..5f6d2d7b9f --- /dev/null +++ b/numpy-examples/share_prices-5.csv @@ -0,0 +1,7 @@ +Company,Fri +Company_A,112.5 +Company_B,200.8 +Company_C,51.2 +Company_D,97.5 +Company_E,200.8 +Company_F,52.2 \ No newline at end of file diff --git a/numpy-examples/short_file.csv b/numpy-examples/short_file.csv new file mode 100644 index 0000000000..30c26e4dec --- /dev/null +++ b/numpy-examples/short_file.csv @@ -0,0 +1 @@ +4.1,4.2,4.3 diff --git a/numpy-examples/tutorial_code.ipynb b/numpy-examples/tutorial_code.ipynb new file mode 100644 index 0000000000..453c501358 --- /dev/null +++ b/numpy-examples/tutorial_code.ipynb @@ -0,0 +1,576 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c33e52ac-a587-41c3-b487-6a1533043a54", + "metadata": {}, + "source": [ + "# **Code Used In NumPy Practical Examples: Useful Techniques**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29aff3d0-0427-4dd8-b14f-bc7ce9002a76", + "metadata": {}, + "outputs": [], + "source": [ + "!python -m pip install numpy\n", + "!python -m pip install matplotlib\n", + "!python -m pip install pathlib\n", + "!python -m pip install jupyterlab" + ] + }, + { + "cell_type": "markdown", + "id": "8b740161-a80e-4d37-8c26-2dca52c68dce", + "metadata": {}, + "source": [ + "**Example 1: Creating Multi-Dimensional Arrays From Files**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e64a11c1-a0b5-49f8-a0a6-dfe0adf674f4", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from pathlib import Path\n", + "\n", + "array = np.zeros(\n", + " (\n", + " 3,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "print(array.shape)\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8de90c08-9bdc-42f3-ad4e-d7a34fca8215", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array[3, 0] = np.loadtxt(\"short_file.csv\", delimiter=\",\")\n", + "\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63775265-deb1-4cca-bd7c-547b3d49f5ba", + "metadata": {}, + "outputs": [], + "source": [ + "array = np.zeros(\n", + " (\n", + " 4,\n", + " 2,\n", + " 3,\n", + " )\n", + ")\n", + "print(id(array))\n", + "\n", + "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", + "\n", + "array = np.insert(arr=array, obj=2, values=0, axis=1)\n", + "\n", + "array[3] = np.loadtxt(\"long_file.csv\", delimiter=\",\")\n", + "\n", + "print(id(array))\n", + "\n", + "array" + ] + }, + { + "cell_type": "markdown", + "id": "fd4544ac-53bc-449c-88a2-f2496bba8f55", + "metadata": {}, + "source": [ + "**Example 2: Reconciling Data Using Structured NumPy Arrays**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6d760bcd-1be5-4396-8eb8-d729768909d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['At The Back', 'Fast Eddie', 'Almost There'], dtype=' first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35dab65e-baf0-4a0a-b773-0ec7133b1b23", + "metadata": {}, + "outputs": [], + "source": [ + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "vectorized_in_profit = np.vectorize(in_profit)\n", + "vectorized_in_profit(monday_prices, friday_prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05eaa992-bed3-4af0-9508-c58be2f36381", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[vectorized_in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acf8cad6-e93a-4ee9-817c-ffffdc4b825e", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f789fa62-e8e0-45c8-973f-b33ee54e3262", + "metadata": {}, + "outputs": [], + "source": [ + "@np.vectorize\n", + "def in_profit(first_day, last_day):\n", + " if last_day > first_day:\n", + " return True\n", + " return False\n", + "\n", + "\n", + "monday_prices = portfolio[\"mon\"]\n", + "friday_prices = portfolio[\"fri\"]\n", + "\n", + "print(in_profit(monday_prices, friday_prices))\n", + "\n", + "portfolio[in_profit(monday_prices, friday_prices)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "257d37f9-5b7f-4ad6-beef-1c8f300a8347", + "metadata": {}, + "outputs": [], + "source": [ + "in_profit(3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfec4cdd-db86-4cfe-a325-0597ff2a4f94", + "metadata": {}, + "outputs": [], + "source": [ + "portfolio[np.where(friday_prices > monday_prices, True, False)]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}