Numpy Flashcards
np.array([1, 2, 3, 4], dtype=’float32’)
explicitly set the data type of the resulting array
np.array([range(i, i + 3) for i in [2, 4, 6]])
nested lists result in multi-dimensional arrays
np.zeros(10, dtype=int)
Create a length-10 integer array filled with zeros
np.ones((3, 5), dtype=float)
Create a 3x5 floating-point array filled with ones
np.full((3, 5), 3.14)
Create a 3x5 array filled with 3.14
np.arange(0, 20, 2)
# Create an array filled with a linear sequence # Starting at 0, ending at 20, stepping by 2 # (this is similar to the built-in range() function)
np.linspace(0, 1, 5)
Create an array of five values evenly spaced between 0 and 1
np.random.random((3, 3))
# Create a 3x3 array of uniformly distributed # random values between 0 and 1
np.random.normal(0, 1, (3, 3))
# Create a 3x3 array of normally distributed random values # with mean 0 and standard deviation 1
np.random.randint(0, 10, (3, 3))
Create a 3x3 array of random integers in the interval [0, 10)
np.eye(3
Create a 3x3 identity matrix
np.empty(3)
# Create an uninitialized array of three integers # The values will be whatever happens to already exist at that memory location
x2[1:2, 0]
In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices
x2[0:, 0] = 15.8
x2
Values can also be modified using any of the above index notation
x[1::2]
every other element, starting at index 1