diff --git a/numpy-examples/full_portfolio.csv b/numpy-examples/full_portfolio.csv new file mode 100644 index 0000000000..2285eed597 --- /dev/null +++ b/numpy-examples/full_portfolio.csv @@ -0,0 +1,7 @@ +Company,Sector,Mon,Tue,Wed,Thu,Fri +Company_A,technology,100.5,101.2,102,101.8,112.5 +Company_B,finance,200.1,199.8,200.5,201.0,200.8 +Company_C,healthcare,50.3,50.5,51.0,50.8,51.2 +Company_D,technology,110.5,101.2,102,111.8,97.5 +Company_E,finance,200.1,200.8,200.5,211.0,200.8 +Company_F,healthcare,55.3,50.5,53.0,50.8,52.2 diff --git a/numpy-examples/issued_checks.csv b/numpy-examples/issued_checks.csv index 79ebe66d15..c4a57d0e5d 100644 --- a/numpy-examples/issued_checks.csv +++ b/numpy-examples/issued_checks.csv @@ -1,5 +1,5 @@ Check_ID,Payee,Amount,Date_Issued -1341,K.Starmer,150.00,2024-03-29 +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 @@ -8,4 +8,4 @@ Check_ID,Payee,Amount,Date_Issued 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 +1350,M Thatcher,220.00,2024-03-15 diff --git a/numpy-examples/passports.csv b/numpy-examples/passports.csv index 729d3351f9..91d80d959e 100644 --- a/numpy-examples/passports.csv +++ b/numpy-examples/passports.csv @@ -4,7 +4,7 @@ passport_no,passenger_no,nationality 493456399,3,American 375456228,4,American 457345975,5,Austrian -345957363,6,Norewegian +345957363,6,Norwegian 567546577,7,French 453667456,8,German 456785778,9,Danish @@ -19,4 +19,4 @@ passport_no,passenger_no,nationality 654672278,18,Spanish 683637288,19,Norwegian 768357788,20,British -768357788,21,American \ No newline at end of file +768357788,21,American diff --git a/numpy-examples/tutorial_code.ipynb b/numpy-examples/tutorial_code.ipynb index 453c501358..e24f9e54df 100644 --- a/numpy-examples/tutorial_code.ipynb +++ b/numpy-examples/tutorial_code.ipynb @@ -15,10 +15,8 @@ "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" + "# !python -m pip install numpy\n", + "# !python -m pip install matplotlib" ] }, { @@ -39,16 +37,10 @@ "import numpy as np\n", "from pathlib import Path\n", "\n", - "array = np.zeros(\n", - " (\n", - " 3,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", + "array = np.zeros((3, 2, 3))\n", "print(id(array))\n", "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + "for file_count, csv_file in enumerate(sorted(Path.cwd().glob(\"file?.csv\"))):\n", " array[file_count] = np.loadtxt(csv_file.name, delimiter=\",\")\n", "\n", "print(id(array))\n", @@ -63,15 +55,9 @@ "metadata": {}, "outputs": [], "source": [ - "array = np.zeros(\n", - " (\n", - " 4,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", + "array = np.zeros((4, 2, 3))\n", "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + "for file_count, csv_file in enumerate(sorted(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", @@ -86,24 +72,16 @@ "metadata": {}, "outputs": [], "source": [ - "array = np.zeros(\n", - " (\n", - " 4,\n", - " 2,\n", - " 3,\n", - " )\n", - ")\n", + "array = np.zeros((4, 2, 3))\n", "print(id(array))\n", "\n", - "for file_count, csv_file in enumerate(Path.cwd().glob(\"file?.csv\")):\n", + "for file_count, csv_file in enumerate(sorted(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" ] }, @@ -117,21 +95,10 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "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", + "def profit_with_bonus(first_day, last_day):\n", + " if last_day >= first_day * 1.01:\n", + " return (last_day - first_day) * 1.1\n", + " else:\n", + " return last_day - first_day\n", "\n", - "monday_prices = portfolio[\"mon\"]\n", - "friday_prices = portfolio[\"fri\"]\n", "\n", - "in_profit(monday_prices, friday_prices)" + "# The following causes an error because in_profit() doesn't know\n", + "# how to work with NumPy arrays:\n", + "#\n", + "# profit_with_bonus(portfolio[\"mon\"], portfolio[\"fri\"])" ] }, { @@ -476,27 +438,15 @@ "metadata": {}, "outputs": [], "source": [ - "def in_profit(first_day, last_day):\n", - " if last_day > first_day:\n", - " return True\n", - " return False\n", + "def profit_with_bonus(first_day, last_day):\n", + " if last_day >= first_day * 1.01:\n", + " return (last_day - first_day) * 1.1\n", + " else:\n", + " return last_day - first_day\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)]" + "vectorized_profit_with_bonus = np.vectorize(profit_with_bonus)\n", + "vectorized_profit_with_bonus(portfolio[\"mon\"], portfolio[\"fri\"])" ] }, { @@ -506,7 +456,7 @@ "metadata": {}, "outputs": [], "source": [ - "in_profit(3, 5)" + "profit_with_bonus(3, 5)" ] }, { @@ -517,18 +467,14 @@ "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", + "def profit_with_bonus(first_day, last_day):\n", + " if last_day >= first_day * 1.01:\n", + " return (last_day - first_day) * 1.1\n", + " else:\n", + " return last_day - first_day\n", "\n", - "print(in_profit(monday_prices, friday_prices))\n", "\n", - "portfolio[in_profit(monday_prices, friday_prices)]" + "profit_with_bonus(portfolio[\"mon\"], portfolio[\"fri\"])" ] }, { @@ -538,7 +484,7 @@ "metadata": {}, "outputs": [], "source": [ - "in_profit(3, 5)" + "profit_with_bonus(3, 5)" ] }, { @@ -548,7 +494,11 @@ "metadata": {}, "outputs": [], "source": [ - "portfolio[np.where(friday_prices > monday_prices, True, False)]" + "np.where(\n", + " portfolio[\"fri\"] > portfolio[\"mon\"] * 1.01,\n", + " (portfolio[\"fri\"] - portfolio[\"mon\"]) * 1.1,\n", + " portfolio[\"fri\"] - portfolio[\"mon\"],\n", + ")" ] } ], @@ -568,7 +518,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.7" } }, "nbformat": 4,