{"id":88580,"date":"2023-11-27T18:00:57","date_gmt":"2023-11-27T12:30:57","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88580"},"modified":"2023-11-27T18:00:57","modified_gmt":"2023-11-27T12:30:57","slug":"numpy-linear-algebra","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/","title":{"rendered":"Numpy Linear Algebra with Examples"},"content":{"rendered":"<p>One of the key features of NumPy is its extensive collection of linear algebra functions. In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples to help beginners get started.<\/p>\n<h3>Importing NumPy<\/h3>\n<p><strong>To use NumPy&#8217;s linear algebra functions, you need to import the library into your Python script or notebook:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np<\/pre>\n<h3>Creating Matrices and Arrays in NumPy<\/h3>\n<p><strong>Before we start using linear algebra functions, let&#8217;s quickly learn how to create matrices and arrays using NumPy:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Create a 2x2 matrix\nmatrix = np.array([[1, 2], [3, 4]])\n\n# Create a 1D array\narray = np.array([5, 6, 7])<\/pre>\n<h3>Basic Linear Algebra Operations in NumPy<\/h3>\n<h4>1. Matrix Multiplication (dot)<\/h4>\n<p>Matrix multiplication is a fundamental operation in linear algebra. <strong>NumPy provides the dot function to perform matrix multiplication:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">matrix1 = np.array([[1, 2], [3, 4]])\nmatrix2 = np.array([[5, 6], [7, 8]])\n\nresult = np.dot(matrix1, matrix2)\nprint(result)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[19 22]<br \/>\n[43 50]]<\/p>\n<h4>2. Multi-Dot (linalg.multi_dot)<\/h4>\n<p>The multi_dot function calculates the dot product of two or more arrays efficiently by automatically selecting the fastest evaluation order.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">arrays = [np.array([1, 2]), np.array([3, 4]), np.array([5, 6])]\nmulti_dot_result = np.linalg.multi_dot(arrays)\nprint(multi_dot_result)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>44<\/p>\n<h4>3. Vector Dot Product (vdot)<\/h4>\n<p>The vdot function returns the dot product of two vectors. It differs from dot as it treats the input arrays as vectors and performs their dot product.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">vector1 = np.array([1, 2, 3])\nvector2 = np.array([4, 5, 6])\n\nvector_dot_product = np.vdot(vector1, vector2)\nprint(vector_dot_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>32<\/p>\n<h4>4. Inner Product (inner)<\/h4>\n<p><strong>The inner product of two arrays can be calculated using the inner function:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">array1 = np.array([1, 2, 3])\narray2 = np.array([4, 5, 6])\n\ninner_product = np.inner(array1, array2)\nprint(inner_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>32<\/p>\n<h4>5. Outer Product (outer)<\/h4>\n<p><strong>The outer product of two arrays can be calculated using the outer function:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">array1 = np.array([1, 2, 3])\narray2 = np.array([4, 5, 6])\n\nouter_product = np.outer(array1, array2)\nprint(outer_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[ 4 5 6]<br \/>\n[ 8 10 12]<br \/>\n[12 15 18]]<\/p>\n<h4>6. Matrix Multiplication (matmul)<\/h4>\n<p>The matmul function performs matrix multiplication between two arrays.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">matrix1 = np.array([[1, 2], [3, 4]])\nmatrix2 = np.array([[5, 6], [7, 8]])\n\nmatrix_product = np.matmul(matrix1, matrix2)\nprint(matrix_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[19 22]<br \/>\n[43 50]]<\/p>\n<h4>7. Tensor Dot Product (tensordot)<\/h4>\n<p>The tensordot function computes the tensor dot product along specified axes of two arrays.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">array1 = np.array([[1, 2], [3, 4]])\narray2 = np.array([[5, 6], [7, 8]])\n\ntensor_dot_product = np.tensordot(array1, array2, axes=1)\nprint(tensor_dot_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[[ 5 6]<br \/>\n[10 12]]<\/p>\n<p>[[21 24]<br \/>\n[28 32]]]<\/p>\n<h4>8. Determinant (det)<\/h4>\n<p><strong>The determinant of a square matrix is an important value used in various mathematical operations:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">matrix = np.array([[1, 2], [3, 4]])\ndeterminant = np.linalg.det(matrix)\nprint(determinant)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>-2.0000000000000004<\/p>\n<h4>9. Inverse (inv)<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">To find the inverse of a square matrix, you can use the inv function:\nmatrix = np.array([[1, 2], [3, 4]])\ninverse_matrix = np.linalg.inv(matrix)\nprint(inverse_matrix)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[-2. 1. ]<br \/>\n[ 1.5 -0.5]]<\/p>\n<h3>Solving Linear Systems in NumPy<\/h3>\n<p><strong>NumPy makes it easy to solve systems of linear equations using its linear algebra functions:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Ax = B\nA = np.array([[2, 3], [1, -1]])\nB = np.array([7, 1])\n\nsolution = np.linalg.solve(A, B)\nprint(solution)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[2. 1.]<\/p>\n<h3>Additional Linear Algebra Functions in NumPy<\/h3>\n<h4>1. Trace (trace)<\/h4>\n<p><strong>The trace of a square matrix is the sum of its diagonal elements:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">matrix = np.array([[1, 2], [3, 4]])\ntrace_sum = np.trace(matrix)\nprint(trace_sum)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>5<\/p>\n<h4>2. Einstein Summation (einsum)<\/h4>\n<p>The einsum function evaluates the Einstein summation convention on the operands. This allows you to perform a wide range of element-wise and matrix operations in a concise manner.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">array1 = np.array([1, 2, 3])\narray2 = np.array([4, 5, 6])\n\neinsum_result = np.einsum('i,i', array1, array2)\nprint(einsum_result)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>32<\/p>\n<h4>3. Matrix Power (linalg.matrix_power)<\/h4>\n<p>The matrix_power function raises a square matrix to the power of an integer n.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">matrix = np.array([[1, 2], [3, 4]])\npower = 3\n\nmatrix_to_power = np.linalg.matrix_power(matrix, power)\nprint(matrix_to_power)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[ 37 54]<br \/>\n[ 81 118]]<\/p>\n<h4>4. Kronecker Product (kron)<\/h4>\n<p>The kron function computes the Kronecker product of two arrays.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">array1 = np.array([[1, 2], [3, 4]])\narray2 = np.array([[5, 6], [7, 8]])\n\nkron_product = np.kron(array1, array2)\nprint(kron_product)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[ 5 6 10 12]<br \/>\n[ 7 8 14 16]<br \/>\n[15 18 20 24]<br \/>\n[21 24 28 32]]<\/p>\n<h3>Conclusion<\/h3>\n<p>These functions are indispensable tools for data analysis, scientific computing, and machine learning tasks. By understanding and utilizing these functions effectively, you&#8217;ll be better equipped to manipulate and analyze arrays and matrices in your Python projects. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the key features of NumPy is its extensive collection of linear algebra functions. In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples to&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88955,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[5250,5251,5247,5252,384],"class_list":["post-88580","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy-tutorials","tag-linear-algebra","tag-linear-algebra-in-numpy","tag-numpy","tag-numpy-linear-algebra","tag-numpy-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Numpy Linear Algebra with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code 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:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numpy Linear Algebra with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-27T12:30:57+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan 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 Linear Algebra with Examples - TechVidvan","description":"In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code 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:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/","og_locale":"en_US","og_type":"article","og_title":"Numpy Linear Algebra with Examples - TechVidvan","og_description":"In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-11-27T12:30:57+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Numpy Linear Algebra with Examples","datePublished":"2023-11-27T12:30:57+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/"},"wordCount":415,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#primaryimage"},"thumbnailUrl":"","keywords":["linear algebra","linear algebra in numpy","numpy","numpy linear algebra","numPy tutorial"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/","url":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/","name":"Numpy Linear Algebra with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-11-27T12:30:57+00:00","description":"In this tutorial, we will explore the fundamental linear algebra functions provided by NumPy, along with code examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-linear-algebra\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Numpy Linear Algebra with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88580"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88580\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}