import numpy as np
np.array(['a', 'b'])
array(['a', 'b'], dtype='<U1')
np.zeros(3)
array([0., 0., 0.])
np.full(3, 2.71)
array([2.71, 2.71, 2.71])
np.arange(0, 10, 2) # 2-spaced values over [0, 10)
array([0, 2, 4, 6, 8])
A = np.arange(6)
A.shape
(6,)
A.reshape((-1, 2)) # '-1' requests #rows to be inferred
array([[0, 1], [2, 3], [4, 5]])
np.linspace(0, 10)
array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653, 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469, 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286, 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102, 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918, 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735, 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551, 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367, 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184, 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
a = np.arange(3) # show with a = [0, 1, 2] at pythontutor.com|
copy = a.copy()
copy[0] = 10 # does not change a
view = a
view[1] = 11 # changes a
print(f'a={a}, copy={copy}, view={view}')
a=[ 0 11 2], copy=[10 1 2], view=[ 0 11 2]
np.array([1, 2, 3])
array([1, 2, 3])
A = np.array([1, np.sqrt(2), np.e, np.pi])
A
array([1. , 1.41421356, 2.71828183, 3.14159265])
1 < A
array([False, True, True, True])
np.sum(1 < A)
3
np.array(['apple', 'banana', 'cherry'])
array(['apple', 'banana', 'cherry'], dtype='<U6')
np.amax(A)
3.141592653589793
np.argmax(A)
3
x = 1 + np.arange(5)
print(x)
np.std(x, ddof=1)
[1 2 3 4 5]
1.5811388300841898
B = np.array([13, 10, 12, 11])
B
array([13, 10, 12, 11])
np.sort(B)
array([10, 11, 12, 13])
np.argsort(B)
array([1, 3, 2, 0])
B[np.argsort(B)]
array([10, 11, 12, 13])
np.flip(np.sort(B)) # np.flip() reverses an array
array([13, 12, 11, 10])
price = np.array((10, 15, 12)) # use np.argsort() to sort two parallel arrays
n_shares = np.array((1, 2, 5)) # while keeping them parallel
indicesToSortPrice = np.argsort(price)
indicesToSortPrice
array([0, 2, 1])
price[indicesToSortPrice]
array([10, 12, 15])
n_shares[indicesToSortPrice]
array([1, 5, 2])
np.isin(np.array([1, 2, 3]), np.array([2, 7]))
array([False, True, False])
[] enclose a list; and [] are for indexing into a sequence (string, tuple, list) or array, e.g. price[2]
() enclose a tuple; and () enclose the arguments to a function, e.g. np.sort(price)
We have used {} only in a print(f'...') statement to say, "give the value of the enclosed variable," e.g print(f'x={x}')
x = 1 + np.arange(5)
x
array([1, 2, 3, 4, 5])
n = len(x)
np.sqrt(np.sum((x - np.mean(x))**2) / (n - 1)) # no loop; inspect parts
1.5811388300841898
# We could not do this with a list--thus the Q03 exercises
# to find std(x), where x was a list, required a loop. NumPy was
# written to make this kind of computation easier, faster, and
# less memory-intensive.
y = [1, 2, 3]
# y - 2 # uncomment to see error: can't do this with a list (without a loop)
A
array([1. , 1.41421356, 2.71828183, 3.14159265])
1 < A
array([False, True, True, True])
A < 3
array([ True, True, True, False])
(1 < A) & (A < 3)
array([False, True, True, False])
(1 < A) | (A < 3)
array([ True, True, True, True])
(1 < A) ^ (A < 3)
array([ True, False, False, True])
~(1 < A)
array([ True, False, False, False])
A[0]
1.0
A[3]
3.141592653589793
A[-1]
3.141592653589793
A[0:2]
array([1. , 1.41421356])
A[:2]
array([1. , 1.41421356])
A[0:]
array([1. , 1.41421356, 2.71828183, 3.14159265])
x = np.arange(10, 20)
x
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
a = np.array([0, 9])
a
array([0, 9])
x[a]
array([10, 19])
x[np.array([0, -1])]
array([10, 19])
A
array([1. , 1.41421356, 2.71828183, 3.14159265])
indices = np.nonzero(1 < A)
indices
(array([1, 2, 3]),)
A[indices]
array([1.41421356, 2.71828183, 3.14159265])
A[1 < A]
array([1.41421356, 2.71828183, 3.14159265])
x
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
x % 2
array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1])
(x % 2) == 0
array([ True, False, True, False, True, False, True, False, True, False])
x[(x % 2) == 0]
array([10, 12, 14, 16, 18])
np.all(1 < A)
False
np.any(1 < A)
True
for value in x:
print(f' value={value}')
value=10 value=11 value=12 value=13 value=14 value=15 value=16 value=17 value=18 value=19
for i in np.arange(len(x)):
print(f' i={i}, x[{i}]={x[i]}')
i=0, x[0]=10 i=1, x[1]=11 i=2, x[2]=12 i=3, x[3]=13 i=4, x[4]=14 i=5, x[5]=15 i=6, x[6]=16 i=7, x[7]=17 i=8, x[8]=18 i=9, x[9]=19
np.savetxt('A.txt', A)
C = np.loadtxt('A.txt', float)
C
array([1. , 1.41421356, 2.71828183, 3.14159265])