<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basic data containers in Python include
high-level number objects
Step1:  Zero-based indexing
Step2:  Works with any index-supporting objects, including strings
Step3:  another example
Given a 2D image, img, stored in row-major order, we want to find the linear position in the array of the element at
position (x, y). Using zero-based indexing, that linear position is simply img[y * width + x], whereas with one-based indexing it is img((y - 1) * width + x). Now there is a -1 in there!
Why not to use lists?
Lists in Python are quite general, can have arbitrary objects as elements.
Addition and scalar multiplication are defined for lists, but not what we want for numerical computation, e.g.
Addition results in concatenation
Step4:  And multiplication results in repeating
Step5:  Enter NumPy arrays
Aside
Step6:  NumPy arrays
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank.
Why it is useful
Step7:  Creating arrays
Manually
Step8:  The class is called
Step9:  Scalar array
Step10:  Equivalent to size(c) in MATLAB.
Common mistakes
Step11:  Do not use np.ndarray function to create an array
Functions for creating arrays
evenly spaced
Step12:  by a number of points
Step13:  filled with specific number
Zeros
Step14:  Ones
Step15:  Empty
Step16:  The function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.
Random numbers
Step17:  Special cases
Step18:  Missing data
Step19:  We will have a separate session on Masked Arrays in NumPy.
Looking for help
Interactive help
Step20:  with NumPy
Step21:  References
MATLAB to Python
<ASSISTANT_TASK:>
Python Code:
my_collection = [1, 4, 6, 10]
my_collection.append(100000)
my_collection.remove(1)
my_collection[1] = 'abcdef'
my_collection
Explanation: Basic data containers in Python include
high-level number objects: integer, floating point, complex, etc.
containers: lists (costless insertion and append), dictionaries (fast lookup)
Lists
One-dimensional
Can contain items of different types
Mutable, i.e. items can be added, dropped, or replaced
Similar to MATLAB's cell arrays
End of explanation
a = [10, 20, 30, 40, 50, 60, 70]
low, high = 2, 4
a[:low]
a[low:high]
a[high:]
Explanation: Zero-based indexing
End of explanation
s = 'qwerty'
s[1:-1]
Explanation: Works with any index-supporting objects, including strings:
End of explanation
x = [1, 2, 3]
y = [10, 20, 30]
x + y
Explanation: another example
Given a 2D image, img, stored in row-major order, we want to find the linear position in the array of the element at
position (x, y). Using zero-based indexing, that linear position is simply img[y * width + x], whereas with one-based indexing it is img((y - 1) * width + x). Now there is a -1 in there!
Why not to use lists?
Lists in Python are quite general, can have arbitrary objects as elements.
Addition and scalar multiplication are defined for lists, but not what we want for numerical computation, e.g.
Addition results in concatenation
End of explanation
x = [2, 3]
x * 3
Explanation: And multiplication results in repeating:
End of explanation
import numpy as np
Explanation: Enter NumPy arrays
Aside: import conventions
End of explanation
l = list(range(1000))
%timeit [i**2 for i in l]
a = np.arange(1000)
%timeit a**2
Explanation: NumPy arrays
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank.
Why it is useful: Memory-efficient container that provides fast numerical operations.
Let's compare it to list operations
End of explanation
a = np.array([3, 4, 5, 6])
a
Explanation: Creating arrays
Manually
End of explanation
type(a)
a.ndim
Explanation: The class is called
End of explanation
a0 = np.array(7)
a0.ndim
b = np.array([[10, 20, 30], [9, 8, 7]])
b
c = np.array([[[1], [2]], [[3], [4]]])
c.shape
c.max()
Explanation: Scalar array
End of explanation
try:
    a = np.array(1,2,3,4) # WRONG, throws ValueError
except ValueError as e:
    print(e)
a = [1,2,3,4]
a = np.array(a) # RIGHT
b = a.copy()
Explanation: Equivalent to size(c) in MATLAB.
Common mistakes
End of explanation
np.arange(1, 9, 2) # start, end (exclusive), step
Explanation: Do not use np.ndarray function to create an array
Functions for creating arrays
evenly spaced
End of explanation
np.linspace(0, 1, 6)   # start, end, num-points
np.logspace(-3,2,7)
Explanation: by a number of points
End of explanation
np.zeros((2, 3))
Explanation: filled with specific number
Zeros
End of explanation
np.ones((3, 2))
Explanation: Ones
End of explanation
np.empty([2,3])
Explanation: Empty
End of explanation
np.random.seed(1234)
np.random.rand(4)       # uniform in [0, 1]
np.random.randn(4)      # Gaussian
Explanation: The function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.
Random numbers
End of explanation
np.eye(3)
np.diag(np.array([1, 2, 3, 4]))
Explanation: Special cases
End of explanation
a = np.array([1, 2, 3, np.nan])
b = list(a)
a = np.array([1, 2, 3])
b = np.ma.masked_less(a, 2)
b
b.data
b.mask
Explanation: Missing data
End of explanation
np.rollaxis??
np.*space*?
Explanation: We will have a separate session on Masked Arrays in NumPy.
Looking for help
Interactive help
End of explanation
np.lookfor('create array')
Explanation: with NumPy: a built-in search engine
End of explanation
HTML(html)
Explanation: References
MATLAB to Python: A Migration Guide
NumPy docs
SciPy lectures
End of explanation
<END_TASK>