In [ ]:
x = []
for i in range(1,11):
if i > 2:
x.append(i**2)
print(x[3])
How would you fix the following code so it does what you expect it to do?
In [ ]:
some_list = [1,2,3]
a_list_copy = some_list
some_list[1] = 273
In [5]:
import numpy as np
an_array = np.array([1,2,3,4,5,6,7,8,9,10],dtype=np.uint8)
print(an_array[3])
In [ ]:
import numpy as np # IMPORT numpy (calling it "np" for convenience)
an_array = np.array([1,2,3,4,5,6,7,8,9,10],dtype=int) # Create a numpy array of integers
print(an_array[3]) # print the fourth element
In [6]:
some_list = [1,2,3]
print(some_list)
print(some_list + 5)
In [7]:
some_list = [1,2,3]
for i in range(len(some_list)):
some_list[i] = some_list[i] + 5
In [8]:
import numpy as np
some_array = np.array([1,2,3])
some_array = some_array + 5
print(some_array)
In [9]:
import numpy as np
a_list = list(range(100000))
an_array = np.array(range(100000),dtype=int)
In [10]:
%timeit for i in range(100000): a_list[i] + 5
%timeit an_array + 5
for i in range(blah blah)
is in pure python. This is convenient, but slow.an_array + 5
actually runs the loop, but in compiled C. This is super fast.
In [1]:
import numpy as np
x = np.array([1,2,3])
y = np.array([4,5,6])
print(x + y)
In [2]:
print(np.sin(y))
You can do any math you want on all elements of the array at once.
In [3]:
an_array = np.array([[1,2],[3,4]],dtype=int)
print(an_array)
In [4]:
an_array = np.zeros((2,2),dtype=float)
print(an_array)
an_array[0,0] = 1
an_array[0,1] = 2
an_array[1,0] = 3
an_array[1,1] = 4
print(an_array)
cast
them via np.array(ARRAY_LIKE_THING). np.zeros
, np.ones
, np.eye
)
In [12]:
an_array = np.array([[1,2,3],[4,5,6],[7,8,9]],dtype=int)
print(an_array[-1])
print("")
print(an_array[0,0])
print("")
print(an_array[:,0].reshape(3,1))
print("")
print(an_array[:,:])
an_array.shape[1]
Out[12]:
Like lists, but with commas between dimensions:
some_array[i,j]
would access the $(i-1)^{th},(j-1)^{th}$ element.some_array[:,:]
would spit out the whole first two dimensions.some_array[i,j,k,l,m,n]
would also work for a 6th dimensional array.One major motivation for the authors who created numpy
was to speed-up mathematical operations on vector, matrix, and tensor-like data structures.
np.linalg
: Linear algebra module.np.fft
: Fourier Transform module.np.random
: Random sampling (from various statistical distributions) module.
In [14]:
# Define two vector-like arrays
x = np.array([3,5])
y = np.array([4,6])
print("Element wise sum:" , x + y ) # Addition
print("Element wise difference:", x - y ) # Substraction
print("Element wise product:", x * y) # Product
print("Element wise division:", x / y) # division
print("Dot product:", x @ y) # Dot product
In [15]:
# Define a matrix
M = np.array([
[5, 3],
[2, 7]
])
print("Matrix transpose:\n", M.T) # Transpose
print("Vector-matrix dot product", np.dot(M, x)) # Dot product
print("Matrix determinant:", np.linalg.det(M)) # Determinant
print("Matrix inverse:\n", np.linalg.inv(M)) # inverse
Solve a system of equations using numpy.linalg.solve
function.
Example: $$ 3 x + 2y - z = 1 \\ 2 x - 5y + 4z = -2 \\ -x + \frac{1}{2} y - z = 0 $$
Written in matrix form: $$ A \vec{x} = \vec{b} $$
$$ \left[ \begin{array}{ccc} 3 & 2 & -1 \\ 2 & -5 & 4 \\ -1 & \frac{1}{2} & -1 \\ \end{array} \right] % \left[ \begin{array}{c} x \\ y \\ z \end{array} \right] = \left[ \begin{array}{c} 1 \\ -2 \\ 0 \end{array} \right] $$</small>
In [16]:
A = np.array([
[ 3, 2, -1],
[ 2, -2, 4],
[-1, 0.5, -1]
])
b = np.array([1, -2, 0])
print("x :", np.linalg.solve(A, b))
In [17]:
M = np.array([
[5, 3],
[2, 7]
])
λs, eigvec = np.linalg.eig(M)
print("λ 1:", λs[0])
print("Eigen vector 1:", eigvec[0])
print("\n")
print("λ 2:", λs[1])
print("Eigen vector 2:", eigvec[1])
In [ ]: