In this post, we are going to explore arrays is created by the numpy package in Python. Understanding how arrays are created and manipulated is useful when you need to perform complex coding and or analysis. In particular, we will address the following,
- Creating and exploring arrays
- Math with arrays
- Manipulating arrays
Creating and Exploring an Array
Creating an array is simple. You need to import the numpy package and then use the np.array function to create the array. Below is the code.
import numpy as np example=np.array([[1,2,3,4,5],[6,7,8,9,10]])
Making an array requires the use of square brackets. If you want multiple dimensions or columns than you must use inner square brackets. In the example above I made an array with two dimensions and each dimension has it’s own set of brackets.
Also, notice that we imported numpy as np. This is a shorthand so that we do not have to type the word numpy but only np. In addition, we now created an array with ten data points spread in two dimensions.
There are several functions you can use to get an idea of the size of a data set. Below is a list with the function and explanation.
- .ndim = number of dimensions
- .shape = Shares the number of rows and columns
- .size = Counts the number of individual data points
- .dtype.name = Tells you the data structure type
Below is code that uses all four of these functions with our array.
example.ndim Out: 2 example.shape Out: (2, 5) example.size Out: 10 example.dtype.name Out: 'int64'
You can see we have 2 dimensions. The .shape function tells us we have 2 dimensions and 5 examples in each one. The .size function tells us we have 10 total examples (5 * 2). Lastly, the .dtype.name function tells us that this is an integer data type.
Math with Arrays
All mathematical operations can be performed on arrays. Below are examples of addition, subtraction, multiplication, and conditionals.
example=np.array([[1,2,3,4,5],[6,7,8,9,10]]) example+2 Out: array([[ 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12]]) example-2 Out: array([[-1, 0, 1, 2, 3], [ 4, 5, 6, 7, 8]]) example*2 Out: array([[ 2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]) example<3 Out: array([[ True, True, False, False, False], [False, False, False, False, False]], dtype=bool)
Each number inside the example array was manipulated as indicated. For example, if we typed example + 2 all the values in the array increased by 2. Lastly, the example < 3 tells python to look inside the array and find all the values in the array that are less than 3.
There are also several ways you can manipulate or access data inside an array. For example, you can pull a particular element in an array by doing the following.
example[0,0] Out: 1
The information in the brackets tells python to access the first bracket and the first number in the bracket. Recall that python starts from 0. You can also access a range of values using the colon as shown below
example=np.array([[1,2,3,4,5],[6,7,8,9,10]]) example[:,2:4] Out: array([[3, 4], [8, 9]])
In this example, the colon means take all values or dimension possible for finding numbers. This means to take columns 1 & 2. After the comma we have 2:4, this means take the 3rd and 4th value but not the 5th.
It is also possible to turn a multidimensional array into a single dimension with the .ravel() function and also to transpose with the transpose() function. Below is the code for each.
example=np.array([[1,2,3,4,5],[6,7,8,9,10]]) example.ravel() Out: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) example.transpose() Out: array([[ 1, 6], [ 2, 7], [ 3, 8], [ 4, 9], [ 5, 10]])
You can see the .ravel function made a one-dimensional array. The .transpose broke the array into several more dimensions with two numbers each.
We now have a basic understanding of how numpy array work using python. As mention before, this is valuable information to understand when trying to wrestling with different data science questions.