{"id":2903,"date":"2024-03-18T17:15:18","date_gmt":"2024-03-18T12:15:18","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=2903"},"modified":"2024-03-19T08:43:03","modified_gmt":"2024-03-19T03:43:03","slug":"introduction-to-numpy-a-powerful-tool-for-data-science","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/18\/introduction-to-numpy-a-powerful-tool-for-data-science\/","title":{"rendered":"Introduction to NumPy: A Powerful Tool for Data Science"},"content":{"rendered":"\n<p>NumPy, short for Numerical Python, is one of the fundamental libraries for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. NumPy is widely used in scientific computing, data analysis, and machine learning due to its powerful array manipulation capabilities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Getting Started<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2907\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2-1-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Introduction to NumPy<\/figcaption><\/figure>\n\n\n\n<p>If numpy is not installed on your system, then install it usingthe following command. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!pip install numpy<\/code><\/pre>\n\n\n\n<p>Before using NumPy, you need to import it into your Python script or interactive session:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np<\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s explore some of the essential modules and functions provided by NumPy:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Arrays<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2908\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide4-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Numpy Arrays<\/figcaption><\/figure>\n\n\n\n<p>NumPy arrays can be created using various methods. Here are some common ones:<\/p>\n\n\n\n<p><strong>np.array()<\/strong>: Create an array from a Python list or tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = np.array(&#91;1, 2, 3, 4, 5])\nprint(arr)<\/code><\/pre>\n\n\n\n<p><strong>np.zeros()<\/strong>: Create an array filled with zeros.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>zeros_arr = np.zeros((2, 3))  # 2 rows, 3 columns\nprint(zeros_arr)<\/code><\/pre>\n\n\n\n<p><strong>np.ones()<\/strong>: Create an array filled with ones.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ones_arr = np.ones((3, 2))  # 3 rows, 2 columns\nprint(ones_arr)<\/code><\/pre>\n\n\n\n<p><strong>np.arange()<\/strong>: Create an array with a range of values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_arr = np.arange(0, 10, 2)  # start, stop, step\nprint(range_arr)<\/code><\/pre>\n\n\n\n<p><strong>np.linspace()<\/strong>: Create an array with evenly spaced values over a specified range.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>linspace_arr = np.linspace(0, 5, 10)  # start, stop, num\nprint(linspace_arr)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Array Attributes<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2909\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide5-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Numpy Array Attributes<\/figcaption><\/figure>\n\n\n\n<p>NumPy arrays have several attributes that provide information about the array:<\/p>\n\n\n\n<p><strong>ndim<\/strong>: Number of dimensions (axes) of the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(arr.ndim)<\/code><\/pre>\n\n\n\n<p><strong>shape<\/strong>: Tuple indicating the size of each dimension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(arr.shape)<\/code><\/pre>\n\n\n\n<p><strong>size<\/strong>: Total number of elements in the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(arr.size)<\/code><\/pre>\n\n\n\n<p><strong>dtype<\/strong>: Data type of the array elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(arr.dtype)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Array Operations<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2910\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide7-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Elementwise operation on NumPy Array<\/figcaption><\/figure>\n\n\n\n<p>NumPy allows various operations on arrays, including element-wise operations, mathematical functions, and linear algebra operations:<\/p>\n\n\n\n<p><strong>Element-wise Operations<\/strong>: NumPy supports arithmetic operations between arrays of the same shape.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr1 = np.array(&#91;1, 2, 3])\narr2 = np.array(&#91;4, 5, 6])\n\nsum_arr = arr1 + arr2\nprint(sum_arr)<\/code><\/pre>\n\n\n\n<p><strong>Mathematical Functions<\/strong>: NumPy provides many mathematical functions that can be applied element-wise to arrays.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2911\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide8-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Mathematical Functions on Numpy Array<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = np.array(&#91;1, 2, 3])\n\n# Square root of each element\nsqrt_arr = np.sqrt(arr)\nprint(sqrt_arr)<\/code><\/pre>\n\n\n\n<p><strong>Linear Algebra Operations<\/strong>: NumPy provides functions for linear algebra operations, such as matrix multiplication and determinant calculation.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-2912\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide9-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Linear Algebra Operations on NumPy Array<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>matrix1 = np.array(&#91;&#91;1, 2], &#91;3, 4]])\nmatrix2 = np.array(&#91;&#91;5, 6], &#91;7, 8]])\n\n# Matrix multiplication\nmatrix_product = np.dot(matrix1, matrix2)\nprint(matrix_product)<\/code><\/pre>\n\n\n\n<p><strong>Indexing and Slicing<\/strong><\/p>\n\n\n\n<p>NumPy arrays support indexing and slicing operations to access elements or subarrays:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr = np.array(&#91;1, 2, 3, 4, 5])\n\n# Accessing individual elements\nprint(arr&#91;0])   # First element\nprint(arr&#91;-1])  # Last element\n\n# Slicing\nprint(arr&#91;1:4])  # Elements from index 1 (inclusive) to 4 ()<\/code><\/pre>\n\n\n\n<p>NumPy is a powerful library for numerical computing in Python. In this tutorial, we&#8217;ve covered the basics of NumPy, including creating arrays, array attributes, array operations, indexing, and slicing. With NumPy, you can efficiently manipulate large datasets and perform complex mathematical operations, making it an essential tool for data science and scientific computing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy, short for Numerical Python, is one of the fundamental libraries for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. NumPy is widely used in scientific computing, data analysis, and machine learning due to its powerful array manipulation capabilities. Getting Started If numpy is not installed on your system, then install it usingthe following command. Before using NumPy, you need to import&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/18\/introduction-to-numpy-a-powerful-tool-for-data-science\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2906,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[468,476],"tags":[534,533],"class_list":["post-2903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","category-data-driven-applicatiosn","tag-data-science","tag-numpy"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-4-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-KP","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=2903"}],"version-history":[{"count":5,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2903\/revisions"}],"predecessor-version":[{"id":2927,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2903\/revisions\/2927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/2906"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}