English 中文(简体)
NumPy - Matrix Library
  • 时间:2024-12-22

NumPy - Matrix Library


Previous Page Next Page  

NumPy package contains a Matrix pbrary numpy.matpb. This module has functions that return matrices instead of ndarray objects.

matpb.empty()

The matpb.empty() function returns a new matrix without initiapzing the entries. The function takes the following parameters.

numpy.matpb.empty(shape, dtype, order)

Where,

Sr.No. Parameter & Description
1

shape

int or tuple of int defining the shape of the new matrix

2

Dtype

Optional. Data type of the output

3

order

C or F

Example

import numpy.matpb 
import numpy as np 

print np.matpb.empty((2,2)) 
# filled with random data

It will produce the following output −

[[ 2.12199579e-314,   4.24399158e-314] 
 [ 4.24399158e-314,   2.12199579e-314]] 

numpy.matpb.zeros()

This function returns the matrix filled with zeros.

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

It will produce the following output −

[[ 0.  0.] 
 [ 0.  0.]] 

numpy.matpb.ones()

This function returns the matrix filled with 1s.

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

It will produce the following output −

[[ 1.  1.] 
 [ 1.  1.]] 

numpy.matpb.eye()

This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters.

numpy.matpb.eye(n, M,k, dtype)

Where,

Sr.No. Parameter & Description
1

n

The number of rows in the resulting matrix

2

M

The number of columns, defaults to n

3

k

Index of diagonal

4

dtype

Data type of the output

Example

import numpy.matpb 
import numpy as np 
print np.matpb.eye(n = 3, M = 4, k = 0, dtype = float)

It will produce the following output −

[[ 1.  0.  0.  0.] 
 [ 0.  1.  0.  0.] 
 [ 0.  0.  1.  0.]] 

numpy.matpb.identity()

The numpy.matpb.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1.

import numpy.matpb 
import numpy as np 
print np.matpb.identity(5, dtype = float)

It will produce the following output −

[[ 1.  0.  0.  0.  0.] 
 [ 0.  1.  0.  0.  0.] 
 [ 0.  0.  1.  0.  0.] 
 [ 0.  0.  0.  1.  0.] 
 [ 0.  0.  0.  0.  1.]] 

numpy.matpb.rand()

The numpy.matpb.rand() function returns a matrix of the given size filled with random values.

Example

import numpy.matpb 
import numpy as np 
print np.matpb.rand(3,3)

It will produce the following output −

[[ 0.82674464  0.57206837  0.15497519] 
 [ 0.33857374  0.35742401  0.90895076] 
 [ 0.03968467  0.13962089  0.39665201]]

Note that a matrix is always two-dimensional, whereas ndarray is an n-dimensional array. Both the objects are inter-convertible.

Example

import numpy.matpb 
import numpy as np  

i = np.matrix( 1,2;3,4 ) 
print i 

It will produce the following output −

[[1  2] 
 [3  4]]

Example

import numpy.matpb 
import numpy as np  

j = np.asarray(i) 
print j 

It will produce the following output −

[[1  2] 
 [3  4]] 

Example

import numpy.matpb 
import numpy as np  

k = np.asmatrix (j) 
print k

It will produce the following output −

[[1  2] 
 [3  4]]
Advertisements