Arrays With Python Numpy
Arrays With Python Numpy
Create and manipulate arrays with Numpy.
Join the DZone community and get the full member experience.Join For Free
Numpy, short for Numeric or Numerical Python, is a general-purpose, array-processing Python package written mostly in C. It provides high-level performance on multidimensional array objects and tools for working with these arrays. It is a successor to the two earlier scientific computing Python libraries, Numeric and Numarray.
Libraries within Python include basic linear algebra functions, Fourier transforms, random number generators, and tools for integrating Fortran and C/C++ code.
NumPy is used in various applications such as:
- Matrix Computations
- Numerical Analysis
- Image processing
- Signal processing
- Linear algebra
- A plethora of others
To install Numpy, type the following line into your command prompt.
pip install numpy
Import Numpy With Python
Before we can use Numpy, we will have to import it. Per convention, we are going to alias the module as "np."
import numpy as np
A Numpy array is a collection of homogeneous values (all of the same data type) and is indexed by a tuple of nonnegative integers. The number of dimensions (count of rows) is the rank of the array. The shape of an array is a tuple of integers, which indicates the size of the array along each dimension.
Creating a Numpy Array
We can initialize Numpy arrays from nested Python lists and access elements using square brackets:
An array can be created with numpy.array.
# Creating a 1-D array import numpy as np arr = np.array([8, 9, 10]) print(arr) print(type(arr))
Our output will look like this:
[8 9 10] <class 'numpy.ndarray'>
Indexing a Numpy Array
Just like arrays in many other languages, we are going to index Numpy arrays with nonnegative integers. For example,
# printing the first three indexes in our numpy array print(arr) print(arr) print(arr)
will yield the following output:
8 9 10
Checking Array Dimensions and Shape
#Checking the dimensions of the array print(arr.ndim) # Checking shape of the array print(arr.shape)
The dimension of the array will be one. This shouldn't be surprising, since "arr" is a one-dimensional array. The shape of the array will give us (3,), where three is the number of elements in the array.
We'll see what this looks like for a multidimensional array soon.
Checking Array Length
Similarly to the shape attribute, the len() function will give us the number of elemens in a one-dimensional array. (For multidimensional arrays, len() will show the number of rows in the array.)
The output for this will be:
Creating a 2-D Array
To create a 2-D array, we will use the same array() function we did when creating a one-dimensional array; however, for this, we'll nest multiple lists inside of the main set of brackets for our argument.
# Creating an array with a shape of 2 x 3 arr2d = np.array([[8, 9, 10], [11, 12, 13]]) print(arr2d)
The above code will output:
[[8 9 10] [11 12 13]]
Checking Array Dimension and Shape
When we check the dimensions of the array with the ndim() function, we get two as our output. This makes sense, given that our array is two-dimensional. The shape of the array will be given as (n, m), where n is the number of rows and m is the number of columns in the array. In this case, "arr2d.shape" will yield (2, 3) as output.
Indexing a 2-D Array
We can look to the previous use of shape to understand how indexing a two-dimensional works. The n in (n, m) represents the row you want to access (the index of an array in two-dimensional array), while m represents the index of the element you want to access inside of the individual array.
Here's a short example:
# Printing the first element in the first array print(arr2d[0, 0]) # Printing the second element in the first array print(arr2d[0, 1]) # Printing the third element in the first array print(arr2d[0, 2]) # Printing the first element in the second array print(arr2d[1, 0]) # Printing the second element in the second array print(arr2d[1, 1]) # Printing the third element in the second array print(arr2d[1, 2])
The output for the above code will look like this:
8 9 10 11 12 13
Creating Constant Arrays
Numpy gives us several options for creating constant arrays. The first two functions, zeros() and ones() take the shape of an array as input and then fill that array with zeros and ones, respectively...as their names might suggest.
# Creating an array of size (3, 2) and filling it with zeros zero_array = np.zeros((3, 2)) # Creating an array of (3, 2) and filling it with ones ones_array = np.ones((3, 2))
The third option, fill(), takes a second argument, in addition to the shape of the array, that allows you to choose a number you want to fill the array with.
# Creating an array with size (3, 2) and filling it with nines. nines_array = np.fill((3, 2), 9)
Finally, the eye() function allows us to create a multi-dimensional array with ones going down the diagonal and zeros elsewhere; the function takes an integer that represents the number of rows and columns of the array.
# Creating a three-dimensional array with ones going down the diagonal diagonal_ones_arr = np.eye(3) # Printing the array print(diagonal_ones_arr)
The output will be:
[[1 0 0] [0 1 0] [0 0 1]]
Filling an Array With Random Values
We can use Numpy's random method in order to generate multi-dimensional arrays with random floats between zero and one (exclusive) inserted into them. Again, the function takes the size of the array as input.
random_arr = np.random.random((4, 4)) print(random_arr)
The output will look like this:
[[0.79463968 0.53404146 0.99421718 0.02112594] [0.89371598 0.30352261 0.41096905 0.64765438] [0.38907842 0.03502517 0.96261726 0.84007885] [0.19656946 0.73345558 0.3992765 0.35990511]]
Like Python lists, Numpy arrays can also be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:
# create the array of size 3 by 4 mult_arr = np.array ( [ [ 5, 6, 7, 10 ], [ 4, 3, 2, 1 ], [ 15, 12, 17, 20 ] ] )
Use slicing to pull out a subarray, consisting of the first two rows and columns one and two; for that we create sub_arr having a shape of (2, 2):
sub_arr = mult_arr [:2, 1:3] print(sub_arr)
[[6 7] [3 2]]
Integer Array Indexing
When you index into Numpy arrays using slicing, the resulting array will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array.
An example of integer array indexing is as follows:
ar = np.array([[4, 5], [6, 7], [8, 9]]) print (ar[[ 0, 1, 2], [ 0, 1, 0]])
The returned array will have a shape of (3,) and print [4 7 8]. The above example of integer array indexing is similar to, and will have the same output as, the following line of code
print(np.array([ar[0, 0], ar[1, 1 ], ar[2, 0 ]]))
Reshaping an Array
We can use the reshape function to change the shape of our array.
arr2 = np.array ([[8, 9, 10], [1, 2, 3]]) arr3 = arr2.reshape(3, 2) print(arr3)
The reshaped array will now have a shape of (3, 2), as out argument in the reshape() function would suggest.
[[ 8 9] [10 1] [ 2 3]]
Numpy Arange() Function
The numpy.arange() function returns a ndarray object containing evenly spaced values within the given range. For integer arguments, the method is equivalent to a Python built-in range function, but it returns the ndarray rather than a list.
The function takes four parameters:
start: a number representing the start of the intervval. By default, it is zero.
stop: a number representing the end of. the interval.
step: a number representing the interval between each value in the range. The defualt is one.
dtype: The type of an output array.
Let's look at an example.
import numpy as np nparr = np.arange(5) #stop=5 print(nparr)
The output will be:
[0 1 2 3 4]
>>> nparr = np.arange( 2, 5 ) # start=2 stop 5
[2 3 4]
Let's look at another example, this time with custom values for start and step.
nparr = np.arange(2, 22, 2) # start = 2 stop = 22 step = 2 print(nparr)
[2 4 6 8 10 12 14 16 18 20]
Finally, let's add custom input for all of our parameters.
nparr = np.arange(2, 22, 2, float) # start = 2 stop = 22 step = 2 and dtype = float print(nparr)
[2. 4. 6. 8. 10. 12. 14. 16. 18. 20.]
Published at DZone with permission of Chandu Siva . See the original article here.
Opinions expressed by DZone contributors are their own.