Creating Numpy Arrays
- Numpy is a module that makes working with multidimensional list easier
- Numpy defines it’s own type called array
A numpy array is a multidimensional lists where:
- all values have the same type
- all sub-lists are the same length
Numpy arrays can be created from lists
an_array = numpy.array([[1, 2, 3], [4, 5, 6]])
- A couple of differences to note about numpy arrays
They print differently than lists
a_list = [[1, 2, 3], [4, 5, 6]] an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(a_list) print(an_array)
They also have a different type
a_list = [[1, 2, 3], [4, 5, 6]] an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(type(a_list)) print(type(an_array))
Can also create numby arrays filled with a single value
all_zeros = numpy.zeros((2, 3)) print(all_zeros) all_ones = numpy.ones((2, 3)) print(all_ones)
- Note the input to the functions specifie the number of rows and columns in the array
- Also note the extra set of parentheses, the function has a single input, an immutable list of the dimensions of the array
Can also create vectors (one dimensional arrays)
a_vector = numpy.arange(6) print(a_range)
- arange works just like range, you can give it a start, stop, and step
Unlike range, arange can use floating point numbers
a_vector = numpy.arange(1, 10, 0.5) print(a_vector)
- However, because of floating point precision it is difficult to predict how many numbers will be in the range
As an alternative you can use linspace
a_vector = numpy.linspace(1, 10 19)
- The inputs to linspace are start, stop, and num
Can also turn a vector into a matrix by using a method
a_vector = numpy.arange(7) a_matrix = a_vector.reshape(2, 3) print(a_matrix)
Can also create an array of random values
an_array = numpy.random.random((2, 3)) print(an_array)
Numpy Array Attributes
- Once you have a numpy array, you can get information about the array using attributes
- Attributes are information attached to a python object
Access the information by using the dot notation, like a function, but without the parentheses of a function
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(an_array.ndim) print(an_array.shape) print(an_array.size) print(an_array.dtype)
Operations
Can perform arithmetic operations on two arrays of the same size
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) all_ones = numpy.ones((2, 3)) sum = an_array + all_ones print(sum)
- This performs an elelment-wise operation
- That is each element is matched with the correspoding element in the other array to perform the operation
- Can perform any of the arithmetic or logical operators, +, -, *, /, //, **, <, >, etc.
This can be used to create an array of any single value
all_tens = numpy.ones((2, 3)) * 10 print(all_tens)
Can also use some functions that are like the math module functions
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(numpy.sqrt(an_array)) print(numpy.sin(an_array))
- Don’t need to import another module to use them, they are in the numpy module
Can also perform arithmetic operations on an array and a single value
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) sum = an_array + 1 print(sum)
- This has the effect of applying the same operation to every element in the array
Can also use methods
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(an_array.sum()) print(an_array.min()) print(an_array.max())
- These methods ignore the structure of the array and find the sum, min, and max of all the elements
It is possible to perform these along particular axes using an optional parameter
an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(an_array.sum(axis=0))
This sums along the first axis, or the rows in this example
Indexing and Slicing
One dimensional arrays can be indexed and sliced just like lists
a_list = [1, 2, 3, 4, 5, 6] an_array = numpy.array([1, 2, 3, 4, 5, 6]) print(a_list[2]) print(an_array[2]) print(a_list[1:3]) print(an_array[1:3])
Multi dimensional arrays are indexed and sliced differently than lists
a_list = [[1, 2, 3], [4, 5, 6]] an_array = numpy.array([[1, 2, 3], [4, 5, 6]]) print(a_list[1][2]) print(an_array[1, 2]) print(an_array[1:3, 0])
Fancy Indexing
Can also index using an array of indices
an_array = numpy.array([1, 2, 3, 4]) an_array[[0, 2]] = 0 print(an_array)
Or with an array of booleans
an_array = numpy.array([1, 2, 3, 4]) an_array[[True, False, True, False]] = 0 print(an_array)