পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, March 24, 2017

NumPy - Matrix Library

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

matlib.empty()

The matlib.empty() function returns a new matrix without initializing the entries. The function takes the following parameters.
numpy.matlib.empty(shape, dtype, order)
Where,

S.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.matlib 
import numpy as np 

print np.matlib.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.matlib.zeros()

This function returns the matrix filled with zeros.
import numpy.matlib 
import numpy as np 
print np.matlib.zeros((2,2)) 
It will produce the following output −
[[ 0.  0.] 
 [ 0.  0.]]) 

numpy.matlib.ones()

This function returns the matrix filled with 1s.
import numpy.matlib 
import numpy as np 
print np.matlib.ones((2,2))
It will produce the following output −
[[ 1.  1.] 
 [ 1.  1.]] 

numpy.matlib.eye()

This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters.
numpy.matlib.eye(n, M,k, dtype)
Where,
S.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.matlib 
import numpy as np 
print np.matlib.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.matlib.identity()

The numpy.matlib.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.matlib 
import numpy as np 
print np.matlib.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.matlib.rand()

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

Example

import numpy.matlib 
import numpy as np 
print np.matlib.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.matlib 
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.matlib 
import numpy as np  

j = np.asarray(i) 
print j 
It will produce the following output −
[[1  2] 
 [3  4]] 

Example

import numpy.matlib 
import numpy as np  

k = np.asmatrix (j) 
print k
It will produce the following output −
[[1  2] 
 [3  4]]

No comments:

Post a Comment