

{"id":79302,"date":"2020-07-25T20:14:47","date_gmt":"2020-07-25T14:44:47","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79302"},"modified":"2021-05-09T13:13:34","modified_gmt":"2021-05-09T07:43:34","slug":"numpy-matrix-library","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/","title":{"rendered":"NumPy Matrix Library and Operations"},"content":{"rendered":"<p>The NumPy module consists of a matrix library. The numpy.matlib()is used in NumPy for matrix functions. These functions return matrix values as output. It uses the array elements as input. Let us learn about NumPy Matrix Library and various functions in it.<\/p>\n<h2>NumPy Matrix Library<\/h2>\n<h3>1. np.matlib.empty()Function<\/h3>\n<p>We use this function to return a new matrix. The entries of the matrix are uninitialized. This function takes three parameters.<\/p>\n<p><strong>Syntax-<\/strong> np.matlib.empty(shape,dtype,order)<\/p>\n<p><strong>parameters and description<\/strong><\/p>\n<ul>\n<li>shape- It is a tuple value that defines the shape of the matrix.<\/li>\n<li>dtype- It defines the data type of the matrix.<\/li>\n<li>order- It is used to define the order.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nimport numpy.matlib\r\nprint(np.matlib.empty((5,3)))  \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1.21713744e-316 1.01855798e-312 1.01855798e-312]<br \/>\n[9.54898106e-313 1.14587773e-312 1.01855798e-312]<br \/>\n[1.23075756e-312 1.10343781e-312 1.10343781e-312]<br \/>\n[9.76118064e-313 1.08221785e-312 1.10343781e-312]<br \/>\n[1.20953760e-312 5.73572782e+169 8.32980114e+151]]<\/div>\n<h3>2. np.matlib.zeros()Function<\/h3>\n<p>We use this function to initialize a new matrix. All the matrix elements are set to be zero.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\nimport numpy.matlib  \r\nprint(np.matlib.zeros((2,3))) \r\n \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[0. 0. 0.]<br \/>\n[0. 0. 0.]]<\/div>\n<h3>3. np.matlib.ones()Function<\/h3>\n<p>We use this function to initialize a new matrix. All the matrix elements are filled with 1 as its value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\nimport numpy.matlib  \r\nprint(np.matlib.ones((2,4)))  \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1. 1. 1. 1.]<br \/>\n[1. 1. 1. 1.]]<\/div>\n<h3>4. np.matlib.identity()Function<\/h3>\n<p>An identity matrix is a matrix with all its diagonal elements as 1 and all the other elements as zero. This function returns an identity matrix of a given size.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\nimport numpy.matlib  \r\nprint(np.matlib.identity(3))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1. 0. 0.]<br \/>\n[0. 1. 0.]<br \/>\n[0. 0. 1.]]<\/div>\n<h3>5. np.matlib.eye()Function<\/h3>\n<p>We use this function to initialize a matrix with 1 as the diagonal elements and 0 otherwise. It has the following parameters<\/p>\n<p><strong>Syntax-<\/strong> np.matlib.eye(n,m,k,dtype)<\/p>\n<p><strong>parameters and description<\/strong><\/p>\n<ul>\n<li>n: It represents the number of rows<\/li>\n<li>m: It represents the number of columns<\/li>\n<li>k: It denotes the index of the diagonal<\/li>\n<li>dtype: It defines the data type of output matrix<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\nimport numpy.matlib  \r\nprint(numpy.matlib.eye(n = 4, M = 4, k = 0, dtype = float))  \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[1. 0. 0. 0.]<br \/>\n[0. 1. 0. 0.]<br \/>\n[0. 0. 1. 0.]<br \/>\n[0. 0. 0. 1.]]<\/div>\n<h3>6. np.matlib.rand()Function<\/h3>\n<p>We use it to initialize a matrix filled with random values. The size of the matrix is given as input.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\nimport numpy.matlib  \r\nprint(numpy.matlib.rand(4,2)) \r\n \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[0.77051087 0.69406467]<br \/>\n[0.67043961 0.47559031]<br \/>\n[0.17870651 0.82540777]<br \/>\n[0.79233063 0.77516969]]<\/div>\n<h3>7. np.matlib.repmat()Function<\/h3>\n<p>We use this function to return an array with its element is repeated. We define the axis of repetition<\/p>\n<p><strong>Syntax-<\/strong> np.matlib.repmat(array,m,n)<br \/>\nWe take three arguments. The first one defines the array and the other two the axes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nimport numpy.matlib\r\narr = np.array(1)\r\nnp.matlib.repmat(arr, 4, 2)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([[1, 1],<br \/>\n[1, 1],<br \/>\n[1, 1],<br \/>\n[1, 1]])<\/div>\n<h3>8. np.matlib.randn()Function<\/h3>\n<p>With the use of this function, we can generate an array having random values from a standard normal distribution. We can specify the number of elements as a parameter.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nimport numpy.matlib\r\nnp.matlib.randn(3)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">matrix([[ 1.81789638, 0.71182264, -0.34353199]])<\/div>\n<h3>9. np.matmul() Function<\/h3>\n<p>The matrix multiplication function gives the multiplication of two matrices of the same shape. If the shape is not the same, then it gives an error.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np  \r\na = np.array([4,5,6])  \r\nb = np.array([7,8,9])  \r\n#matrix multiplication function\r\ni=np.matmul(a,b)\r\nprint(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">122<\/div>\n<h2>Summary<\/h2>\n<p>The matlib library is very useful for working with NumPy matrix. It takes in the ndarray object as input and returns the appropriate matrix. These functions have additional functionality due to its input parameters. We can define the size, data type, shape, and order of the resultant matrices<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The NumPy module consists of a matrix library. The numpy.matlib()is used in NumPy for matrix functions. These functions return matrix values as output. It uses the array elements as input. Let us learn about&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79802,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[22774,22773],"class_list":["post-79302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-matrix-functions","tag-numpy-matrix-library"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Matrix Library and Operations - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about numpy matrix library and various functions in Numpy like np.matlib.zero, np.matlib.ones, np.matlib.identity, np.matlib.eye etc with examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Matrix Library and Operations - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about numpy matrix library and various functions in Numpy like np.matlib.zero, np.matlib.ones, np.matlib.identity, np.matlib.eye etc with examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-25T14:44:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy Matrix Library and Operations - DataFlair","description":"Learn about numpy matrix library and various functions in Numpy like np.matlib.zero, np.matlib.ones, np.matlib.identity, np.matlib.eye etc with examples","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Matrix Library and Operations - DataFlair","og_description":"Learn about numpy matrix library and various functions in Numpy like np.matlib.zero, np.matlib.ones, np.matlib.identity, np.matlib.eye etc with examples","og_url":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-25T14:44:47+00:00","article_modified_time":"2021-05-09T07:43:34+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy Matrix Library and Operations","datePublished":"2020-07-25T14:44:47+00:00","dateModified":"2021-05-09T07:43:34+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/"},"wordCount":469,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg","keywords":["numpy matrix functions","numpy matrix library"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/","url":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/","name":"NumPy Matrix Library and Operations - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg","datePublished":"2020-07-25T14:44:47+00:00","dateModified":"2021-05-09T07:43:34+00:00","description":"Learn about numpy matrix library and various functions in Numpy like np.matlib.zero, np.matlib.ones, np.matlib.identity, np.matlib.eye etc with examples","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Matrix-Library.jpg","width":1200,"height":628,"caption":"NumPy Matrix Library"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-matrix-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"NumPy Matrix Library and Operations"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79302"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79302\/revisions"}],"predecessor-version":[{"id":93065,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79302\/revisions\/93065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79802"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}