Basic SciPy functionality

SciPy Basic Functionality

By default, all NumPy functions are available through the SciPy namespace. When SciPy is imported, there’s no need to explicitly import NumPy functions. NumPy’s primary object is a homogeneous multidimensional array. It’s a table of elements (usually numbers), all of the same type, indexed by tuples of positive integers. In NumPy, dimensions are called axes. The number of axes is called the rank.

Now, let’s review the basic functionality of vectors and matrices in NumPy. Since SciPy is built on NumPy arrays, a basic understanding of NumPy is essential, as most linear algebra covers only matrices.

NumPy Vector

A vector can be created in a variety of ways. We’ll cover some of them below.

Converting Python Array-Like Objects to NumPy

Let’s consider the following example.

import numpy as np
list = [1,2,3,4]
arr = np.array(list)
print arr

The output of the above program will be as follows.

[1 2 3 4]

Built-in NumPy Array Creation

NumPy has built-in functions for creating arrays from scratch. Some of these functions are explained below.

Using zeros()

The zeros(shape) function creates an array of the specified shape filled with zeros. The default dtype is float64. Let’s consider the following example.

import numpy as np
print np.zeros((2, 3))

The output of the above program will be as follows.

array([[ 0., 0., 0.],
[ 0., 0., 0.]])

Using ones()

The ones(shape) function creates an array filled with 1 values. It is identical to zero in all other respects. Let us consider the following example.

import numpy as np
print np.ones((2, 3))

The output of the above program will be as follows.

array([[ 1., 1., 1.],
[ 1., 1., 1.]])

Using the range() Function

The arange() function will create an array with regularly increasing values. Let us consider the following example.

import numpy as np
print np.arange(7)

The above program will produce the following output.

array([0, 1, 2, 3, 4, 5, 6])

Defining the Data Type of a Value

Let’s consider the following example.

import numpy as np
arr = np.arange(2, 10, dtype = np.float)
print arr
print "Array Data Type:", arr.dtype

The above program will produce the following output.

[ 2. 3. 4. 5. 6. 7. 8. 9.]
Array Data Type: float64

Using the linspace() Function

The linspace() function creates an array with a specified number of elements, evenly spaced between the specified start and end values. Let’s consider the following example.

import numpy as np
print np.linspace(1., 4., 6)

The above program will produce the following output.

array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])

Matrix

A matrix is a specialized two-dimensional array that maintains its two-dimensional nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power). Let’s consider the following example.

import numpy as np
print np.matrix('1 2; 3 4')

The above program will produce the following output.

matrix([[1, 2],
[3, 4]])

Conjugate transpose of a matrix

This function returns the (complex) conjugate transpose of self. Let us consider the following example.

import numpy as np
mat = np.matrix('1 2; 3 4')
print mat.H

The above program will produce the following output.

matrix([[1, 3],
[2, 4]])

Transpose of a matrix

This function returns the transpose of itself. Let’s consider the following example.

import numpy as np
mat = np.matrix('1 2; 3 4')
mat.T

The above program will produce the following output.

matrix([[1, 3],
[2, 4]])

When we transpose a matrix, we create a new matrix whose rows are the same as the columns of the original matrix. On the other hand, the conjugate transpose swaps the row and column indices of each matrix element. The inverse of a matrix is a matrix that, when multiplied by the original matrix, produces the same matrix.

Leave a Reply

Your email address will not be published. Required fields are marked *