CHAPTER 16 - Arrays and Slicing Arrays in Python



AD

We will discuss creating arrays, including linspace, arange, reshape, and the ones function. We will demonstrate addition, determinants, and transposing arrays. We end the chapter with examples of slicing arrays.

Arrays differ from lists. We can perform mathematical operations with arrays. (Linear algebra is the study of matrices. Matrices are two dimensional arrays.)

16.1.0 Creating arrays

Run the following program. Notice that rows and columns start with the 0 designation. For example the number 2 is in location (0,1). NumPy is the Python module that handles arrays. It is installed if you completed Chapter 12, where matplotlib was installed.

# Create a 2D array
# Rows and Col start at 0.
# For example: 2 is designated as location (0,1)
# 1 2 3
# 4 5 6
# 7 8 9
import numpy as np
myArray = np.array([[1,2,3],[4,5,6],[7,8,9]])
print (myArray)

# Output
# [[1 2 3]
# [4 5 6]
# [7 8 9]]

16.2.0 Creating arrays with np.arange().reshape()

Here arange is short for “a range” and specifies the number range for the array entries. Reshape specifies the number of rows, and columns. For example:

import numpy as np
a = np.arange(6).reshape(2,3)
print (a)

# Output
# [[0 1 2]
# [3 4 5]]

16.2.1 Compare linspace and arange

Linspace and arange divide a range into steps. The first two arguments are the same, but linspace uses the number of samples. Arange uses step size. Compare the results below.

import numpy as np
a = np.linspace(0,1000,10)
print('linspace= ', a)

b = np.arange(0,1000,100)
print('arange= ',b)

# Output
# linspace= [ 0. 111.11111111 222.22222222 333.33333333 444.44444444
# 555.55555556 666.66666667 777.77777778
# 888.88888889 1000. ]
# arange= [ 0 100 200 300 400 500 600 700 800 900]

16.3.0 numpy.zeros() and numpy.ones()

The np.zeros() function fills an array with zeros.

import numpy as np
a = np.zeros(6).reshape(2,3)
print (a)

# Output
# [[0. 0. 0.]
# [0. 0. 0.]]

The np.ones() function fills an array with ones.

import numpy as np
a = np.ones(6).reshape(2,3)
print (a)

# Output
# [[1. 1. 1.]
# [1. 1. 1.]]

16.4.0 Determinants

We can find the determinant of a matrix as follows:

import numpy as np
my_array = ([[3,6,8],
       [2,1,9],
       [8,7,3]])
print(my_array)

#Calculate the determinant of the matrix
det = np.linalg.det(my_array)
print(det)

# Output
# [[3, 6, 8], [2, 1, 9], [8, 7, 3]]
# 263.99999999999994

16.5.0 Transpose of matrix

We can determine the transpose of a matrix as follows.

import numpy as np
my_array = ([[3,6,8],
       [2,1,9],
       [8,7,3]])
print(my_array)

# Calculate the transpose of the matrix
my_array_transposed = np.transpose(my_array)
print(my_array_transposed)

# Output
# [[3, 6, 8], [2, 1, 9], [8, 7, 3]]
# [[3 2 8]
# [6 1 7]
# [8 9 3]]

16.7.0 Add two matrices together

import numpy as np
A_array = ([[3,6,8],
       [2,1,9],
       [8,7,3]])

B_array = ([[1,1,8],
       [2,1,1],
       [1,7,3]])

#Perform addition on the two matrices.
my_array = np.add(A_array, B_array)
print(my_array)

# Ouput
# [[ 4 7 16]
# [ 4 2 10]
# [ 9 14 6]]

16.8.0 Slicing arrays with Python

Using arrays, there are times we only want to use part of an array. Especially in machine learning. Often we need to split the array that contains X and Y values. Typically the X values are the first columns of the array, and the Y value is the final column of the array. Notice that all rows and columns start with 0. Thus a three column array will have rows 0, 1, 2; and columns 0, 1, 2. In a slicing operation we have row, then column; for example array[row,column]. The : in the operation signifies all rows or columns. The slicing action itself occurs just before the location indicated. For example, array[ : , 0:2] includes all rows, and includes column 0 and 1. This saves the first two columns. We will create an array and practice slicing it.

# Create a 2D array
# Rows and Col start at 0.
# 2 is designated as location (0,1)
# 1 2 3
# 4 5 6
# 7 8 9
import numpy as np
myArray = np.array([[1,2,3],[4,5,6],[7,8,9]])
print (myArray)

# Output
# [[1 2 3]
# [4 5 6]
# [7 8 9]]

# Slice off the last column
# Save first two columns
# 0 is the first column
# the slice occurs on #1
# : indicates all rows or columns
X = myArray[:, 0:2]
print(X)

# Output
# [[1 2]
# [4 5]
# [7 8]]

#Save the last column
Z = myArray[:, 2]
print(Z)

# Output
# [3 6 9]

# Select upper left corner
Y = myArray[0:2, 0:2]
print(Y)

# Output
# [[1 2]
# [4 5]]





Engineering Python

SALARSEN.COM

Table of Contents
Ch1-Install Python
Ch2-Install PyCharm
Ch3-Save Work
Ch4-Add Project
Ch5-Variables
Ch6-Print&Input
Ch7-Lists
Ch8-Loops
Ch9-If&Logical
Ch10-Functions
Ch11-Bubble Sort
Ch12-Plotting
Ch13-Files
Ch14-Print Format
Ch15-Dict&Comp&Zip
Ch16-Arrays
Ch17-Electrical
Ch18-Regression
Ch19-Differential
Ch20-Secant