Before getting introduced to Numpy library, we need to be familiar with a very widely used data structure called 'array'. An array is a collection of homogenous variables. Here homogenous means variables of the same data type. And so an array can be a collection of integers (int datatypes), collection of fractions/decimal values (float datatypes) or a collection of characters (char datatype) also referred to as a string.
A list in very similar to an array in structure differing only by the characteristic that, a list is a collection of heterogenous variables, i.e., it can contain a mixture of variables with various datatypes.
Both arrays and lists can be accessed in the same way, using an index, which is a reference to the position of the element within the collection. Index begins from zero and ends with n-1, where n is the total length of the array (which is the number of elements in the array or list).
NumPy is a library in Python that supports large, multidimensional arrays and matrices. It facilitates scientific computing with high level mathematical functions to operate on these arrays.
NumPy object is a multi-dimensional array. It is a table of elements of the same type, indexed by a tuple of positive integers. In Numpy, dimensions are called axes. The number of axes is rank. (ref: scipy.org)
Consider a 3D space of x, y and z coordinates. A point in 3D space [1.0, 3.0, 5.0] is an array of rank 1. If there are several such points as shown below, then the dimensions are 4 and 3.
[[ 1.0, 0.3, 4.5],
[ 0.5, 1.5, 2.3],
[ 6.0, 4.6, 3.5],
[ 4.5, 3.5, 6.3]]
To use a library, we need to import them. Any python library can be referenced by an alias that is mentioned during the import. For example, NumPy library is most commonly imported in the short form as np:
import numpy as np
There are several ways to initialize an array in numpy :
Ref: http://www.numpy.org
The command shape is used to determine the dimensions of the array. The library numpy is imported initially. Numpy directly could be used to run operations such as numpy.array(). However, using short forms of libraries such as np is a common practice.
Find the shape of the array below and assign the shape to the variable: two_d_shape and print it out.
[[1, 0.3, 4.5],
[2, 4.0, 6.0]]
import numpy as np
two_d_array = np.array([[ 1 , 0.3, 4.5],
[2, 4.0, 6.0]])
Note that few numbers are float and few are int. Note the values are converted to float after the initiliazation.
two_d_shape = two_d_array.shape
print(two_d_shape)
ref_tmp_var = False
try:
ref_assert_var = False
two_d_shape_ = two_d_array.shape
if two_d_shape == two_d_shape_:
ref_assert_var = True
else:
ref_assert_var = False
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
else:
if ref_assert_var:
ref_tmp_var = True
else:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
In this lesson we shall study various operations on the numpy arrays.
To add another array of the same dimension use the function add.
C = numpy.add(A, B)
To subtract an array from another array use
D = A - B
To multiply a constant with the elements of the array, the * symbol is used:
K = 10
Y = K * np.array([1, 2, 3, 4, 5])
To multiply a vector with another vector:
Y = A.dot(B)
The other common operations such as square root and exponential functions can be computed with the extensions that are common to most other languages such as :
numpy.sqrt(B), numpy.exp(A),
In this lesson, we can observe that most operations we can conceive of with respect to arrays in the area of data science, can be accessed through the commonly available extensions in other languages as well.
Given two arrays:
A = [1, 2, 3, 4]
B = [2, 3, 4, 5]
import numpy as np
for power in B:
A**power will contain the array. Append this to another empty array.
A = np.array([1, 2, 3, 4])
B = np.array([2, 3, 4, 5])
C = A.dot(B)
ref_tmp_var = False
try:
ref_assert_var = False
A_ = np.array([1, 2, 3, 4])
B_ = np.array([2, 3, 4, 5])
C_ = A_.dot(B_)
if C == C_:
ref_assert_var = True
else:
ref_assert_var = False
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
else:
if ref_assert_var:
ref_tmp_var = True
else:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var