From 2ccac29892a47553460fdf54ce4edb043d22fae2 Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Tue, 14 Apr 2020 17:11:11 -0500 Subject: [PATCH 1/7] Typo fix (#141) --- Python/Module4_OOP/Methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Module4_OOP/Methods.md b/Python/Module4_OOP/Methods.md index 92d9723a..153c2425 100644 --- a/Python/Module4_OOP/Methods.md +++ b/Python/Module4_OOP/Methods.md @@ -200,7 +200,7 @@ __main__.Dummy `dict.fromkeys` is an example of a class method that takes in an iterable, and returns a dictionary whose keys are the elements of that iterable, and whose values all default to `None`. ```python ->>> dict.fromkeys("abcd") +>>> dict.fromkeys("abcd",2.3) {'a': 2.3, 'b': 2.3, 'c': 2.3, 'd': 2.3} ``` From 2dfd1b7093fa708826a6ae32e03ad5af923b6e32 Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Wed, 15 Apr 2020 12:25:50 -0500 Subject: [PATCH 2/7] Fixed dict.fromkeys typo in Module 4, pep-8 compliant --- Python/Module4_OOP/Methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Module4_OOP/Methods.md b/Python/Module4_OOP/Methods.md index 153c2425..6bf8e4fc 100644 --- a/Python/Module4_OOP/Methods.md +++ b/Python/Module4_OOP/Methods.md @@ -200,7 +200,7 @@ __main__.Dummy `dict.fromkeys` is an example of a class method that takes in an iterable, and returns a dictionary whose keys are the elements of that iterable, and whose values all default to `None`. ```python ->>> dict.fromkeys("abcd",2.3) +>>> dict.fromkeys("abcd", 2.3) {'a': 2.3, 'b': 2.3, 'c': 2.3, 'd': 2.3} ``` From c65ffbe26bf0b9357e5ace8d6f5949ba9c4f008e Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Fri, 24 Apr 2020 10:20:05 -0500 Subject: [PATCH 3/7] Fixed multiple typos in Module 2 and Module 3 Module 2 Understanding the Modulo Operator, Reading Comprehension Solutions CHANGE FROM: If `n` is an integer, then 2 will divide into it evenly, and thus there is no remainder. CHANGE TO: If `n` is an even integer, then 2 will divide into it evenly, and thus there is no remainder. Module 2 Data Structures (Part II): Dictionaries, Reading Comprehension Solutions CHANGE FROM: >>> get_maxes(dict(a=1, b=2,c= 2, d=1)) CHANGE TO: >>> get_maxes(dict(a=1, b=2, c=2, d=1)) Module 3 Fixed links from "docs.scipy.org/doc/numpy/" to "numpy.org/doc/stable/" Module 3 "Vectorized" Operations, Reading Comprehension Solutions CHANGE FROM: # top-left top-right bottom-left bottom-right >>> x[:2, :2] + x[:2, -2:] + x[-2:, :2] + x[-2:, -2:] array([[20, 24], [36, 40]]) CHANGE TO: # top-left top-right bottom-left bottom-right >>> np.array([[np.sum(x[:2, :2]), np.sum(x[:2, 2:])], [np.sum(x[2:, :2]), np.sum(x[2:, 2:])]]) array([[10, 18], [42, 50]]) Module 3 "Vectorized" Operations, Reading Comprehension: Basic Sequential Functions CHANGE FROM: Thus a (32,32,3) array would be a 32x32 RBG image. CHANGE TO: Thus a (32,32,3) array would be a 32x32 RGB image. Module 3 Array Broadcasting, Reading Comprehension Solutions MOVE "Broadcast Compatibility: Solution" section to BEFORE "Basic Broadcasting: Solution" ADD and to moved section Module 3 Array Broadcasting, Reading Comprehension: Basic Broadcasting III CHANGE FROM: Thus a (32,32,3) array would be a 32x32 RBG image. CHANGE TO: Thus a (32,32,3) array would be a 32x32 RGB image. Module 3 Introduction to Basic and Advanced Indexing: Reading Comprehension: Ellipsis QUESTION ABOUT: Given a N-dimensional array, x, index into x such that you axis entry-0 of axis-0 QUESTION: "such that you axis"? Should it be "such that you access"? Module 3 Introduction to Basic and Advanced Indexing: Reading Comprehension Augmenting Array Data In-Place IN: >>> np.log(x[1:3], out=x[1:3]) >>> y += 3 REMOVE: (duplicate from #4, not relevant and not in solution) >>> y += 3 Module 3 Introduction to Basic and Advanced Indexing: Reading Comprehension Augmenting Array Data In-Place IN #7: CHANGE FROM: >>> y = y + 2 CHANGE TO (match answer key, and is already same question #3 asks): >>> x = np.square(x) Module 3 Introduction to Basic and Advanced Indexing: Reading Comprehension Augmenting Array Data In-Place AFTER #9, APPEND: ```python >>> np.square(y, out=y) ``` (answer is in Solutions, problem is missing) Module 3 Introduction to Basic and Advanced Indexing: Reading Comprehension Solutions: Assignment via advanced indexing Problem does not match solution. CHANGE FROM: Replace the diagonal elements of `x` with `(-1, -2, -3, -4)`, and add `1` to all values in `x` that are greater than `0.8`. CHANGE TO: Replace the diagonal elements of `x` with `(0, 1, 2, 3)`, and add `1` to all values in `x` that are greater than `0.8`. --- .../Basic_Objects.md | 2 +- .../DataStructures_II_Dictionaries.md | 2 +- .../AccessingDataAlongMultipleDimensions.md | 12 +++---- .../AdvancedIndexing.md | 20 +++++------ .../ArrayTraversal.md | 12 +++---- .../BasicArrayAttributes.md | 2 +- .../Module3_IntroducingNumpy/BasicIndexing.md | 12 ++++--- .../Module3_IntroducingNumpy/Broadcasting.md | 36 ++++++++++--------- .../FunctionsForCreatingNumpyArrays.md | 26 +++++++------- .../IntroducingTheNDarray.md | 6 ++-- .../VectorizedOperations.md | 20 +++++------ 11 files changed, 78 insertions(+), 72 deletions(-) diff --git a/Python/Module2_EssentialsOfPython/Basic_Objects.md b/Python/Module2_EssentialsOfPython/Basic_Objects.md index b9f5a4a7..5a6dd54c 100644 --- a/Python/Module2_EssentialsOfPython/Basic_Objects.md +++ b/Python/Module2_EssentialsOfPython/Basic_Objects.md @@ -1044,7 +1044,7 @@ It is very important to remember this issue of the limited numerical precision o **Understanding the modulo operator: Solution** -If `n` is an integer, then 2 will divide into it evenly, and thus there is no remainder. If `n` is odd, then `n / 2` must have a remainder of 1. Thus: +If `n` is an even integer, then 2 will divide into it evenly, and thus there is no remainder. If `n` is odd, then `n / 2` must have a remainder of 1. Thus: - `n % 2 ` = 0 if `n` is an even number - `n % 2 ` = 1 if `n` is an odd number diff --git a/Python/Module2_EssentialsOfPython/DataStructures_II_Dictionaries.md b/Python/Module2_EssentialsOfPython/DataStructures_II_Dictionaries.md index d3e9d010..1b7d4d7a 100644 --- a/Python/Module2_EssentialsOfPython/DataStructures_II_Dictionaries.md +++ b/Python/Module2_EssentialsOfPython/DataStructures_II_Dictionaries.md @@ -519,7 +519,7 @@ def get_maxes(dictionary): max_val = max(dictionary.values()) return tuple(k for k,v in dictionary.items() if v == max_val) ->>> get_maxes(dict(a=1, b=2,c= 2, d=1)) +>>> get_maxes(dict(a=1, b=2, c=2, d=1)) ('b', 'c') ``` diff --git a/Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.md b/Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.md index 0287cbb7..af775e97 100644 --- a/Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.md +++ b/Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.md @@ -228,7 +228,7 @@ Suppose you have an $N$-dimensional array, and only provide $j$ indices for the -Thus far, we have discussed some rules for accessing data in arrays, all of which fall into the category that is designated ["basic indexing"](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#basic-slicing-and-indexing) by the NumPy documentation. We will discuss the details of basic indexing and of ["advanced indexing"](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing), in full, in a later section. Note, however, that all of the indexing/slicing reviewed here produces a "view" of the original array. That is, *no data is copied* when you index into an array using integer indices and/or slices. Recall that slicing lists and tuples *do* produce copies of the data. +Thus far, we have discussed some rules for accessing data in arrays, all of which fall into the category that is designated ["basic indexing"](https://numpy.org/doc/stable/reference/arrays.indexing.html#basic-slicing-and-indexing) by the NumPy documentation. We will discuss the details of basic indexing and of ["advanced indexing"](https://numpy.org/doc/stable/reference/arrays.indexing.html#advanced-indexing), in full, in a later section. Note, however, that all of the indexing/slicing reviewed here produces a "view" of the original array. That is, *no data is copied* when you index into an array using integer indices and/or slices. Recall that slicing lists and tuples *do* produce copies of the data. @@ -417,7 +417,7 @@ array([[ 1, 5, 9], [ 3, 7, 11], [ 4, 8, 12]]) ``` -A complete listing of the available array-manipulation functions can be found in the [official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html). Among these functions, the reshape function is especially useful. +A complete listing of the available array-manipulation functions can be found in the [official NumPy documentation](https://numpy.org/doc/stable/reference/routines.array-manipulation.html). Among these functions, the reshape function is especially useful. ### Introducing the `reshape` Function The `reshape` function allows you to change the dimensionality and axis-layout of a given array. This adjusts the indexing interface used to access the array's underlying data, as was discussed in earlier in this module. Let's take a shape-(6,) array, and reshape it to a shape-(2, 3) array: @@ -497,10 +497,10 @@ For all straightforward applications of reshape, NumPy does not actually create ## Links to Official Documentation -- [The N-dimensional array](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html) -- [Array indexing](https://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing) -- [Indexing routines](https://docs.scipy.org/doc/numpy/reference/routines.indexing.html#indexing-routines) -- [Array manipulation routines](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html) +- [The N-dimensional array](https://numpy.org/doc/stable/reference/arrays.ndarray.html) +- [Array indexing](https://numpy.org/doc/stable/user/basics.indexing.html#indexing) +- [Indexing routines](https://numpy.org/doc/stable/reference/routines.indexing.html#indexing-routines) +- [Array manipulation routines](https://numpy.org/doc/stable/reference/routines.array-manipulation.html) ## Reading Comprehension Solutions diff --git a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md index a6ef69fd..64df9ccc 100644 --- a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md +++ b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md @@ -70,7 +70,7 @@ False >>> np.shares_memory(x, x[bool_index]) False ``` -The flexibility permitted by advanced indexing makes it a difficult topic to treat exhaustively without delving into somewhat terse and abstract notation. It is best to refer to [the official documentation](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing) for such a treatment of the topic. Here, we will discuss the essential aspects of advanced indexing, with the aim of the discussion being both thorough and accessible. +The flexibility permitted by advanced indexing makes it a difficult topic to treat exhaustively without delving into somewhat terse and abstract notation. It is best to refer to [the official documentation](https://numpy.org/doc/stable/reference/arrays.indexing.html#advanced-indexing) for such a treatment of the topic. Here, we will discuss the essential aspects of advanced indexing, with the aim of the discussion being both thorough and accessible.
@@ -392,7 +392,7 @@ Given the following array: ... [-0.29, 0.13, -0.26, 0.33]]) ``` -Use boolean array-indexing and NumPy's [logical functions](https://docs.scipy.org/doc/numpy/reference/routines.logic.html) to select the contents of `h` that satisfy the following conditions. Because you are dealing with floating-point numbers, [you should not require that two values are exactly equal](https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Basic_Objects.html#Understanding-Numerical-Precision); rather, make use of the function [numpy.isclose](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.isclose.html). +Use boolean array-indexing and NumPy's [logical functions](https://numpy.org/doc/stable/reference/routines.logic.html) to select the contents of `h` that satisfy the following conditions. Because you are dealing with floating-point numbers, [you should not require that two values are exactly equal](https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Basic_Objects.html#Understanding-Numerical-Precision); rather, make use of the function [numpy.isclose](https://numpy.org/doc/stable/reference/generated/numpy.isclose.html#numpy.isclose). 1. All negative entries in `h` 2. All entries in `h` "equal" to `0.01` or `0.33` @@ -403,7 +403,7 @@ Use boolean array-indexing and NumPy's [logical functions](https://docs.scipy.or ### Converting a Boolean Index-Array to Integer Index-Arrays: numpy.where -The function [numpy.where](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html) can be used to take a boolean-valued array, and produce the *tuple* of index-arrays that access the `True` entries of that array, via integer array indexing (discussed at the beginning of this section). +The function [numpy.where](https://numpy.org/doc/stable/reference/generated/numpy.where.html) can be used to take a boolean-valued array, and produce the *tuple* of index-arrays that access the `True` entries of that array, via integer array indexing (discussed at the beginning of this section). ```python # demonstrating `np.where` @@ -449,7 +449,7 @@ array([0], dtype=int64) ``` -Armed with NumPy's suite of [logical functions](https://docs.scipy.org/doc/numpy/reference/routines.logic.html), boolean-array indexing provides a sleek interface for accessing the particular contents of an array, irrespective of the array's shape and the layout of its contents. This method of indexing is especially powerful in the context of performing augmented updates to an array, which is the subject of the following subsection. +Armed with NumPy's suite of [logical functions](https://numpy.org/doc/stable/reference/routines.logic.html), boolean-array indexing provides a sleek interface for accessing the particular contents of an array, irrespective of the array's shape and the layout of its contents. This method of indexing is especially powerful in the context of performing augmented updates to an array, which is the subject of the following subsection.
@@ -537,7 +537,7 @@ Given the array ... [ 0.84, 0.76, 0.25, 0.07]]) ``` -Replace the diagonal elements of `x` with `(-1, -2, -3, -4)`, and add `1` to all values in `x` that are greater than `0.8`. +Replace the diagonal elements of `x` with `(0, 1, 2, 3)`, and add `1` to all values in `x` that are greater than `0.8`.
@@ -563,15 +563,15 @@ array([[ 3, 7, 11]]) ``` -The rules for resolving the various possible combinations of basic and advanced indexing are nontrivial. Refer to the [official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing) for a detailed description for these rules. In practice, basic and advanced indexing can typically be used independently from one another. +The rules for resolving the various possible combinations of basic and advanced indexing are nontrivial. Refer to the [official NumPy documentation](https://numpy.org/doc/stable/reference/arrays.indexing.html#combining-advanced-and-basic-indexing) for a detailed description for these rules. In practice, basic and advanced indexing can typically be used independently from one another. ## Links to Official Documentation -- [Advanced Indexing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing) -- [Logical Functions](https://docs.scipy.org/doc/numpy/reference/routines.logic.html) -- [numpy.where](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html) -- [Combining Basics and Advanced Indexing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing) +- [Advanced Indexing](https://numpy.org/doc/stable/reference/arrays.indexing.html#advanced-indexing) +- [Logical Functions](https://numpy.org/doc/stable/reference/routines.logic.html) +- [numpy.where](https://numpy.org/doc/stable/reference/generated/numpy.where.html) +- [Combining Basics and Advanced Indexing](https://numpy.org/doc/stable/reference/arrays.indexing.html#combining-advanced-and-basic-indexing) ## Reading Comprehension Solutions diff --git a/Python/Module3_IntroducingNumpy/ArrayTraversal.md b/Python/Module3_IntroducingNumpy/ArrayTraversal.md index da718d02..98c255a4 100644 --- a/Python/Module3_IntroducingNumpy/ArrayTraversal.md +++ b/Python/Module3_IntroducingNumpy/ArrayTraversal.md @@ -34,7 +34,7 @@ NumPy provides valuable tools for iterating over any array, such that each eleme [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')] ``` -Similarly, NumPy provides the [ndenumerate](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndenumerate.html#numpy.ndenumerate) function, which enumerates each element in an N-dimensional array, specifying the N-dimensional index for each element. +Similarly, NumPy provides the [ndenumerate](https://numpy.org/doc/stable/reference/generated/numpy.ndenumerate.html#numpy.ndenumerate) function, which enumerates each element in an N-dimensional array, specifying the N-dimensional index for each element. ```python >>> import numpy as np @@ -77,7 +77,7 @@ See that each triplet of integers specifies the index for the corresponding arra 9 ``` -See [the official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/routines.indexing.html#iterating-over-arrays) for a complete listing of functions that facilitate iterating over arrays. The official documentation also provides [a detailed treatment of array iteration](https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#iterating-over-arrays), which is far more detailed than is warranted here. Next, we must discuss the default ordering that NumPy uses when traversing a N-dimensional array. +See [the official NumPy documentation](https://numpy.org/doc/stable/reference/routines.indexing.html#iterating-over-arrays) for a complete listing of functions that facilitate iterating over arrays. The official documentation also provides [a detailed treatment of array iteration](https://numpy.org/doc/stable/reference/arrays.nditer.html#iterating-over-arrays), which is far more detailed than is warranted here. Next, we must discuss the default ordering that NumPy uses when traversing a N-dimensional array. @@ -203,7 +203,7 @@ According to this discussion, `reshape` can effectively "undo" itself: `np.arang ## Links to Official Documentation -- [Definition of row major ordering](https://docs.scipy.org/doc/numpy/glossary.html#term-row-major) -- [Definition of column major ordering](https://docs.scipy.org/doc/numpy/glossary.html#term-column-major) -- [Routines for iterating over arrays](https://docs.scipy.org/doc/numpy/reference/routines.indexing.html#iterating-over-arrays) -- [Detailed description of array iteration](https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#iterating-over-arrays) +- [Definition of row major ordering](https://numpy.org/doc/stable/glossary.html#term-row-major) +- [Definition of column major ordering](https://numpy.org/doc/stable/glossary.html#term-column-major) +- [Routines for iterating over arrays](https://numpy.org/doc/stable/reference/routines.indexing.html#iterating-over-arrays) +- [Detailed description of array iteration](https://numpy.org/doc/stable/reference/arrays.nditer.html#iterating-over-arrays) diff --git a/Python/Module3_IntroducingNumpy/BasicArrayAttributes.md b/Python/Module3_IntroducingNumpy/BasicArrayAttributes.md index 1cee79bf..8cd241a3 100644 --- a/Python/Module3_IntroducingNumpy/BasicArrayAttributes.md +++ b/Python/Module3_IntroducingNumpy/BasicArrayAttributes.md @@ -94,4 +94,4 @@ The size, in bytes (8 bits is 1 byte), of each element of the array. For example ## Links to Official Documentation -- [Array attributes](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#array-attributes) +- [Array attributes](https://numpy.org/doc/stable/reference/arrays.ndarray.html#array-attributes) diff --git a/Python/Module3_IntroducingNumpy/BasicIndexing.md b/Python/Module3_IntroducingNumpy/BasicIndexing.md index fae04f4b..1734bee2 100644 --- a/Python/Module3_IntroducingNumpy/BasicIndexing.md +++ b/Python/Module3_IntroducingNumpy/BasicIndexing.md @@ -546,7 +546,6 @@ Which of the following expressions updates the data originally referenced by `x` ```python # 5. >>> np.log(x[1:3], out=x[1:3]) ->>> y += 3 ``` ```python @@ -556,7 +555,7 @@ Which of the following expressions updates the data originally referenced by `x` ```python # 7. ->>> y = y + 2 +>>> x = np.square(x) ``` ```python @@ -570,6 +569,11 @@ Which of the following expressions updates the data originally referenced by `x` >>> f(y) ``` +```python +# 10. +>>> np.square(y, out=y) +``` +
@@ -584,8 +588,8 @@ Assignments to views of an array, augmented assignments, and NumPy functions tha ## Links to Official Documentation -- [Basic indexing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#indexing) -- [Definition of 'view'](https://docs.scipy.org/doc/numpy/glossary.html#term-view) +- [Basic indexing](https://numpy.org/doc/stable/reference/arrays.indexing.html#indexing) +- [Definition of 'view'](https://numpy.org/doc/stable/glossary.html#term-view) ## Reading Comprehension Solutions diff --git a/Python/Module3_IntroducingNumpy/Broadcasting.md b/Python/Module3_IntroducingNumpy/Broadcasting.md index 5c8a96fc..0f8c47d1 100644 --- a/Python/Module3_IntroducingNumpy/Broadcasting.md +++ b/Python/Module3_IntroducingNumpy/Broadcasting.md @@ -182,7 +182,7 @@ result-shape: 2 x 1 -NumPy provides the function [broadcast_to](https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_to.html#numpy.broadcast_to), which can be used to broadcast an array to a specified shape. This can help us build our intuition for broadcasting. Let's broadcast a shape-(3,4) array to a shape-(2,3,4) array: +NumPy provides the function [broadcast_to](https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html#numpy.broadcast_to), which can be used to broadcast an array to a specified shape. This can help us build our intuition for broadcasting. Let's broadcast a shape-(3,4) array to a shape-(2,3,4) array: ```python # Demonstrating `np.broadcast_to` @@ -426,7 +426,7 @@ Normalize `x` such that *each of its rows, within each sheet, will sum to a valu A digital image is simply an array of numbers, which instructs a grid of pixels on a monitor to shine light of specific colors, according to the numerical values in that array. -An RGB-image can thus be stored as a 3D NumPy array of shape-$(V, H, 3)$. $V$ is the number of pixels along the vertical direction, $H$ is the number of pixels along the horizontal, and the size-3 dimension stores the red, blue, and green color values for a given pixel. Thus a $(32, 32, 3)$ array would be a 32x32 RBG image. +An RGB-image can thus be stored as a 3D NumPy array of shape-$(V, H, 3)$. $V$ is the number of pixels along the vertical direction, $H$ is the number of pixels along the horizontal, and the size-3 dimension stores the red, blue, and green color values for a given pixel. Thus a $(32, 32, 3)$ array would be a 32x32 RGB image. You often work with a collection of images. Suppose we want to store N images in a single array; thus we now consider a 4D shape-(N, V, H, 3) array. For the sake of convenience, let's simply generate a 4D-array of random numbers as a placeholder for real image data. We will generate 500, 48x48 RGB images: @@ -663,19 +663,33 @@ The specific form of an equation can have a major impact on the memory-footprint **Reading Comprehension: Checking the equivalence of the three pairwise distance functions** -Use the function [numpy.allclose](https://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html) to verify that the three methods for computing the pairwise distances produce the same numerical results. +Use the function [numpy.allclose](https://numpy.org/doc/stable/reference/generated/numpy.allclose.html) to verify that the three methods for computing the pairwise distances produce the same numerical results. ## Links to Official Documentation -- [Basics of broadcasting](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html#broadcasting) -- [Broadcasting routines](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html#changing-number-of-dimensions) +- [Basics of broadcasting](https://numpy.org/doc/stable/user/basics.broadcasting.html#broadcasting) +- [Broadcasting routines](https://numpy.org/doc/stable/reference/routines.array-manipulation.html#changing-number-of-dimensions) ## Reading Comprehension Solutions + +**Broadcast Compatibility: Solution** + +1\. Incompatible + +2\. `3 x 4` + +3\. `8 x 3 x 1` + +4\. `9 x 2 x 5` + +5\. Incompatible + + **Basic Broadcasting: Solution** @@ -696,18 +710,6 @@ Generating the random array of 10,000, 2D points, and their "center-of-mass". ``` -**Broadcast Compatibility: Solution** - -1\. Incompatible - -2\. `3 x 4` - -3\. `8 x 3 x 1` - -4\. `9 x 2 x 5` - -5\. Incompatible - **Basic Broadcasting II: Solution** diff --git a/Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.md b/Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.md index 2fac459f..84669d4d 100644 --- a/Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.md +++ b/Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.md @@ -108,12 +108,12 @@ array([[ 0., 0., 0., 0.], array([ 1., 1., 1., 1.]) ``` -NumPy provides additional functions for creating constant-valued arrays. Please refer to [the official documentation](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#ones-and-zeros) for a complete listing. +NumPy provides additional functions for creating constant-valued arrays. Please refer to [the official documentation](https://numpy.org/doc/stable/reference/routines.array-creation.html#ones-and-zeros) for a complete listing. ## Creating Sequential Arrays: `arange` and `linspace` -The [arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html#numpy.arange) function allows you to initialize a sequence of integers based on a starting point (inclusive), stopping point (exclusive), and step size. This is very similar to the `range` function; however `arange` immediately creates this sequence as an array, whereas `range` produces a generator. +The [arange](https://numpy.org/doc/stable/reference/generated/numpy.arange.html#numpy.arange) function allows you to initialize a sequence of integers based on a starting point (inclusive), stopping point (exclusive), and step size. This is very similar to the `range` function; however `arange` immediately creates this sequence as an array, whereas `range` produces a generator. ```python >>> np.arange(0, 10, 1) # start (included): 0, stop (excluded): 10, step:1 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) @@ -127,7 +127,7 @@ array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) array([-5, -3, -1, 1, 3, 5]) ``` -The [linspace](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html) function allows you to generate $N$ *evenly-spaced* points within a user-specified interval $[i, j]$ ($i$ and $j$ are included in the interval). This is often used to generate a domain of values on which to evaluate a mathematical function (e.g. if you want to the sine function from $-\pi$ to $\pi$ on a finely-divided grid). +The [linspace](https://numpy.org/doc/stable/reference/generated/numpy.linspace.html) function allows you to generate $N$ *evenly-spaced* points within a user-specified interval $[i, j]$ ($i$ and $j$ are included in the interval). This is often used to generate a domain of values on which to evaluate a mathematical function (e.g. if you want to the sine function from $-\pi$ to $\pi$ on a finely-divided grid). ```python # generate five evenly-spaced points on the interval [-1, 1] @@ -143,7 +143,7 @@ array([ 3., 4.]) array([-3.14159265, ..., 3.14159265]) ``` -Numpy has other functions for creating sequential arrays, such as producing an array spaced evenly on a log-scaled interval. See the [official documentation](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#numerical-ranges) for a complete listing. +Numpy has other functions for creating sequential arrays, such as producing an array spaced evenly on a log-scaled interval. See the [official documentation](https://numpy.org/doc/stable/reference/routines.array-creation.html#numerical-ranges) for a complete listing. @@ -162,7 +162,7 @@ array([[ 0.09542611, 0.13183498, 0.39836068], >>> np.random.randn(5) array([-1.11262121, -0.35392007, 0.4245215 , -0.81995588, 0.65412323]) ``` -There are [many more functions](https://docs.scipy.org/doc/numpy/reference/routines.random.html#distributions) to read about that allow you to draw from a wide variety of statistical distributions. This only scratches the surface of random number generation in NumPy. +There are [many more functions](https://numpy.org/doc/stable/reference/routines.random.html#distributions) to read about that allow you to draw from a wide variety of statistical distributions. This only scratches the surface of random number generation in NumPy. @@ -191,7 +191,7 @@ array([[ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]], dtype=complex64) ``` -Refer to [the official NumPy documentation](https://docs.scipy.org/doc/numpy/user/basics.types.html#array-types-and-conversions-between-types) for the complete list of available array datatypes. +Refer to [the official NumPy documentation](https://numpy.org/doc/stable/user/basics.types.html#array-types-and-conversions-between-types) for the complete list of available array datatypes. @@ -212,14 +212,14 @@ array([[ 1, 2, 3], array([ 1, 2, 3, -1, -2, -3]) ``` -A complete listing of functions for joining arrays can be [found in the official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html#joining-arrays). There are also corresponding functions for splitting an array into independent arrays. +A complete listing of functions for joining arrays can be [found in the official NumPy documentation](https://numpy.org/doc/stable/reference/routines.array-manipulation.html#joining-arrays). There are also corresponding functions for splitting an array into independent arrays. ## Links to Official Documentation -- [Constant arrays](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#ones-and-zeros) -- [numpy.array](https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy-array) -- [Sequential arrays](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#numerical-ranges) -- [Random distributions](https://docs.scipy.org/doc/numpy/reference/routines.random.html#distributions) -- [Array types](https://docs.scipy.org/doc/numpy/user/basics.types.html#array-types-and-conversions-between-types) -- [Joining arrays](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html#joining-arrays) +- [Constant arrays](https://numpy.org/doc/stable/reference/routines.array-creation.html#ones-and-zeros) +- [numpy.array](https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy-array) +- [Sequential arrays](https://numpy.org/doc/stable/reference/routines.array-creation.html#numerical-ranges) +- [Random distributions](https://numpy.org/doc/stable/reference/routines.random.html#distributions) +- [Array types](https://numpy.org/doc/stable/user/basics.types.html#array-types-and-conversions-between-types) +- [Joining arrays](https://numpy.org/doc/stable/reference/routines.array-manipulation.html#joining-arrays) diff --git a/Python/Module3_IntroducingNumpy/IntroducingTheNDarray.md b/Python/Module3_IntroducingNumpy/IntroducingTheNDarray.md index 89840a5d..17a3d192 100644 --- a/Python/Module3_IntroducingNumpy/IntroducingTheNDarray.md +++ b/Python/Module3_IntroducingNumpy/IntroducingTheNDarray.md @@ -106,6 +106,6 @@ By the end of this module, these code snippets should make good sense, and NumPy ## Links to Official Documentation -- [The N-dimensional array](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html) -- [NumPy Basics](https://docs.scipy.org/doc/numpy/user/basics.html#numpy-basics) -- [NumPy reference](https://docs.scipy.org/doc/numpy/reference/index.html) +- [The N-dimensional array](https://numpy.org/doc/stable/reference/arrays.ndarray.html) +- [NumPy Basics](https://numpy.org/doc/stable/user/basics.html#numpy-basics) +- [NumPy reference](https://numpy.org/doc/stable/reference/index.html) diff --git a/Python/Module3_IntroducingNumpy/VectorizedOperations.md b/Python/Module3_IntroducingNumpy/VectorizedOperations.md index 1c893ee6..01061382 100644 --- a/Python/Module3_IntroducingNumpy/VectorizedOperations.md +++ b/Python/Module3_IntroducingNumpy/VectorizedOperations.md @@ -182,7 +182,7 @@ We will now take some time to survey the various types of vectorized mathematica - binary functions: $f(x,y)$ - functions that operate on sequences of numbers: $f(\{x_i\}_{i=0}^{n-1})$ -These represent a substantial portion of the essential mathematical tools in the NumPy library. An exhaustive list of NumPy's mathematical functions is available in the [official documentation](https://docs.scipy.org/doc/numpy/reference/ufuncs.html#math-operations). +These represent a substantial portion of the essential mathematical tools in the NumPy library. An exhaustive list of NumPy's mathematical functions is available in the [official documentation](https://numpy.org/doc/stable/reference/ufuncs.html#math-operations). ### Unary Functions A unary function is a mathematical function that only accepts one operand (i.e. argument): $f(x)$. NumPy supplies many familiar unary functions: @@ -605,7 +605,7 @@ Now, compute the following: ## Logical Operations -NumPy provides [a suite of logical operations](https://docs.scipy.org/doc/numpy/reference/routines.logic.html) that can operate on arrays. Many of these map logical operations over array entries in the same fashion as NumPy's mathematical functions. These functions return either a single boolean object, or a boolean-type array. +NumPy provides [a suite of logical operations](https://numpy.org/doc/stable/reference/routines.logic.html) that can operate on arrays. Many of these map logical operations over array entries in the same fashion as NumPy's mathematical functions. These functions return either a single boolean object, or a boolean-type array. ```python # check which entries of `x` are less than 6 @@ -641,7 +641,7 @@ True ## Linear Algebra -Lastly, we note that NumPy provides a suite of functions that can perform optimized computations and routines relevant to linear algebra. Included here are functions for performing matrix products and tensor products, solving eigenvalue problems, inverting matrices, and computing vector normalizations. Please refer to the [official NumPy documentation](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html) for a full listing of these functions. +Lastly, we note that NumPy provides a suite of functions that can perform optimized computations and routines relevant to linear algebra. Included here are functions for performing matrix products and tensor products, solving eigenvalue problems, inverting matrices, and computing vector normalizations. Please refer to the [official NumPy documentation](https://numpy.org/doc/stable/reference/routines.linalg.html) for a full listing of these functions. @@ -652,9 +652,9 @@ NumPy provides users with a wide variety of functions capable of performing oper ## Links to Official Documentation -- [Math functions](https://docs.scipy.org/doc/numpy/reference/ufuncs.html#math-operations) -- [Logic functions](https://docs.scipy.org/doc/numpy/reference/routines.logic.html) -- [Linear algebra functions](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html) +- [Math functions](https://numpy.org/doc/stable/reference/ufuncs.html#math-operations) +- [Logic functions](https://numpy.org/doc/stable/reference/routines.logic.html) +- [Linear algebra functions](https://numpy.org/doc/stable/reference/routines.linalg.html) ## Reading Comprehension Solutions @@ -686,10 +686,10 @@ Add the four quadrants of `x`, producing a shape-(2, 2) output. ... [ 8, 9, 10, 11], ... [12, 13, 14, 15]]) - # top-left top-right bottom-left bottom-right ->>> x[:2, :2] + x[:2, -2:] + x[-2:, :2] + x[-2:, -2:] -array([[20, 24], - [36, 40]]) + # top-left top-right bottom-left bottom-right +>>> np.array([[np.sum(x[:2, :2]), np.sum(x[:2, 2:])], [np.sum(x[2:, :2]), np.sum(x[2:, 2:])]]) +array([[10, 18], + [42, 50]]) ``` From 3215eb56f28bf8285a720a1ddeb692cf3a39ba68 Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Fri, 24 Apr 2020 10:30:22 -0500 Subject: [PATCH 4/7] Updated old scipy.org links to numpy.org links --- .../Module3_IntroducingNumpy/Problems/Approximating_pi.ipynb | 4 ++-- Python/Module3_IntroducingNumpy/Problems/ComputeAccuracy.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/Module3_IntroducingNumpy/Problems/Approximating_pi.ipynb b/Python/Module3_IntroducingNumpy/Problems/Approximating_pi.ipynb index ad788160..c93cbea6 100644 --- a/Python/Module3_IntroducingNumpy/Problems/Approximating_pi.ipynb +++ b/Python/Module3_IntroducingNumpy/Problems/Approximating_pi.ipynb @@ -30,7 +30,7 @@ "\n", "## Problem 1\n", "\n", - "Write code that simulates the dart throwing and tallying process, and keep a running estimate of $\\pi$ as \"darts are being thrown\". For simplicity, you can assume that the board is centered at $(0, 0)$, and that $r = 1$ (the radius of the circle). Use `numpy.random.rand` ([link to docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html)) to randomly generate the positions on the board where the darts land. Do this for $N = 10,000$ darts in total. For each dart thrown determine whether or not it landed within the circle, and update your estimate of $\\pi$ according to the formula: $N_{circle} / N_{total} \\approx \\pi / 4$\n", + "Write code that simulates the dart throwing and tallying process, and keep a running estimate of $\\pi$ as \"darts are being thrown\". For simplicity, you can assume that the board is centered at $(0, 0)$, and that $r = 1$ (the radius of the circle). Use `numpy.random.rand` ([link to docs](https://numpy.org/doc/stable/reference/generated/numpy.random.rand.html)) to randomly generate the positions on the board where the darts land. Do this for $N = 10,000$ darts in total. For each dart thrown determine whether or not it landed within the circle, and update your estimate of $\\pi$ according to the formula: $N_{circle} / N_{total} \\approx \\pi / 4$\n", "\n", "Keep in mind that each dart can land in $(x \\in [-1, 1], y \\in [-1, 1])$ and that a dart that lands at $(x, y)$ falls within the circle if \n", "\n", @@ -58,7 +58,7 @@ "source": [ "## Tips\n", "\n", - "It is helpful to know about NumPy's [cumulative-sum function](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html), `numpy.cumsum`. This is useful for keeping a running tally - i.e. keeping the history of the number of darts that have fallen within the circle, and not just the current count." + "It is helpful to know about NumPy's [cumulative-sum function](https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html), `numpy.cumsum`. This is useful for keeping a running tally - i.e. keeping the history of the number of darts that have fallen within the circle, and not just the current count." ] }, { diff --git a/Python/Module3_IntroducingNumpy/Problems/ComputeAccuracy.md b/Python/Module3_IntroducingNumpy/Problems/ComputeAccuracy.md index b2b59efa..17833303 100644 --- a/Python/Module3_IntroducingNumpy/Problems/ComputeAccuracy.md +++ b/Python/Module3_IntroducingNumpy/Problems/ComputeAccuracy.md @@ -60,7 +60,7 @@ Our model classified three out of five images correctly; thus, our accuracy fun To generalize this problem, assume that your classifier is dealing with $K$ classes (instead of $4$). Complete the following function. -**Tip:** You will find it useful to leverage [numpy's argmax function](https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html#numpy.argmax)`f +**Tip:** You will find it useful to leverage [numpy's argmax function](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax)`f ```python def classification_accuracy(classification_scores, true_labels): From 058121338809c29bd746a119f6210ebe0f2a5300 Mon Sep 17 00:00:00 2001 From: Patrick O'Shea <63477154+ArtichokeSap@users.noreply.github.com> Date: Fri, 24 Apr 2020 13:39:03 -0500 Subject: [PATCH 5/7] Update Python/Module3_IntroducingNumpy/VectorizedOperations.md Co-Authored-By: Ryan Soklaski --- Python/Module3_IntroducingNumpy/VectorizedOperations.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/Module3_IntroducingNumpy/VectorizedOperations.md b/Python/Module3_IntroducingNumpy/VectorizedOperations.md index 01061382..8edd973e 100644 --- a/Python/Module3_IntroducingNumpy/VectorizedOperations.md +++ b/Python/Module3_IntroducingNumpy/VectorizedOperations.md @@ -686,10 +686,10 @@ Add the four quadrants of `x`, producing a shape-(2, 2) output. ... [ 8, 9, 10, 11], ... [12, 13, 14, 15]]) - # top-left top-right bottom-left bottom-right ->>> np.array([[np.sum(x[:2, :2]), np.sum(x[:2, 2:])], [np.sum(x[2:, :2]), np.sum(x[2:, 2:])]]) -array([[10, 18], - [42, 50]]) +# top-left top-right bottom-left bottom-right +>>> x[:2, :2] + x[:2, -2:] + x[-2:, :2] + x[-2:, -2:] +array([[20, 24], + [36, 40]]) ``` From 82c5f1f166a96e021e1185dd0cf3a33d558ded6b Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Fri, 24 Apr 2020 15:53:16 -0500 Subject: [PATCH 6/7] Fixed typo "axis" to "access" in Module 3 --- Python/Module3_IntroducingNumpy/BasicIndexing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Module3_IntroducingNumpy/BasicIndexing.md b/Python/Module3_IntroducingNumpy/BasicIndexing.md index 1734bee2..74a6fde4 100644 --- a/Python/Module3_IntroducingNumpy/BasicIndexing.md +++ b/Python/Module3_IntroducingNumpy/BasicIndexing.md @@ -206,7 +206,7 @@ Basic indexing is triggered whenever a tuple of: integer, `slice`, `numpy.newaxi **Reading Comprehension: Ellipsis** -Given a $N$-dimensional array, `x`, index into `x` such that you axis entry-0 of axis-0, the last entry of axis-$N-1$, slicing along all intermediate dimensions. $N$ is at least $2$. +Given a $N$-dimensional array, `x`, index into `x` such that you access entry-0 of axis-0, the last entry of axis-$N-1$, slicing along all intermediate dimensions. $N$ is at least $2$. From ff2855fd8de889121f3b1bad58ffe6416e5c2dc5 Mon Sep 17 00:00:00 2001 From: Patrick O'Shea Date: Fri, 24 Apr 2020 16:05:07 -0500 Subject: [PATCH 7/7] Added Patrick O'Shea to Contributors --- Python/index.rst | 1 + Python/intro.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/Python/index.rst b/Python/index.rst index c37f76bf..1cb6cfab 100644 --- a/Python/index.rst +++ b/Python/index.rst @@ -45,6 +45,7 @@ The following people made significant contributions to PLYMI, adding problems wi - Annabelle Lew (GitHub:`@AnnabelleLew `_) - Petar Griggs (GitHub:`@petarmhg `_) - Sam Carpenter (GitHub:`@HardBoiled800 `_) +- Patrick O'Shea (GitHub:`@ArtichokeSap `_) About Me diff --git a/Python/intro.rst b/Python/intro.rst index a417399c..97d3801e 100644 --- a/Python/intro.rst +++ b/Python/intro.rst @@ -45,6 +45,7 @@ The following people made significant contributions to PLYMI, adding problems wi - Annabelle Lew (GitHub:`@AnnabelleLew `_) - Petar Griggs (GitHub:`@petarmhg `_) - Sam Carpenter (GitHub:`@HardBoiled800 `_) +- Patrick O'Shea (GitHub:`@ArtichokeSap `_) About Me