Introduction to NumPy is the perfect starting point for anyone exploring data science or scientific computing with Python. NumPy provides powerful tools for working with arrays and performing fast numerical operations, making it a core library in the Python ecosystem.

Introduction to NumPy

Here what you learn in this article Introduction to NumPy.

  • Introduction to arrays
  • Difference between arrays and python lists
  • What is NumPy?
  • Why NumPy?
  • Installation of NumPy library
  • Creating Arrays Using NumPy

Introduction to Arrays

It is a fundamental linear Data Structure, A linear data structure is a way of organizing data where elements are arranged sequentially, and each element is connected to its previous and next element.

It is a collection of similar kind (homeogenious) of data items stored at contiguous locations.

It is simple and efficient in managing data collection of same type.

There are two types of arrays –

  1. 1 Dimensional Arrays
  2. 2 Dimensional Arrays

Difference between Arrays and Lists

FeatureArrayList
HomogeneitySame type elementsDifferent type elements
Size FlexibilityFixed-size once createdDynamic size; can grow or shrink
Memory EfficiencyMore memory efficientLess memory-efficient
Built-in MethodsFewer built-in methodsRich set of built-in methods
Module RequirementRequires NumPy moduleNo module required
UsageSuitable for numerical operations and when interfacing with C libraries.General purpose; suitable for a wide range of tasks.

What is NumPy?

NumPy stands for Numeric Python. It is python library created by Travis Oliphant in 2005. It was mainly written in C or C++ or Python (partially).

Numpy performs fast operations on Arrays . NumPy is faster than Python lists because:

  1. Contiguous memory allocation: NumPy arrays store data in a single continuous block of memory, enabling faster access and processing.
  2. Fixed data types: Unlike lists, which can store mixed data types, NumPy arrays use a single data type, reducing overhead.
  3. Vectorized operations: NumPy performs element-wise operations using highly optimized C and Fortran libraries, avoiding slow Python loops.

The core object of NumPy is ndarray that offers mathematical, logical, sorting, selection, linear algebra, statical operations etc.

Why NumPy?

As we have discussed earlier Python lists are slow in processing. NumPy works more faster than lists. The core object ndarray support lots of supporting functions and it helpful in data science and data analysis. It works on locality of reference principle.

The principle of locality of reference refers to the tendency of a program to access the same set of memory locations repeatedly over a short period of time, either:

  • Temporally (same location accessed repeatedly), or
  • Spatially (locations near recently accessed data are accessed soon after).

This principle helps improve performance by making better use of caches and memory hierarchies.

Installation of NumPy

Before working with numpy you need to check the NumPy is installed or not. To check numpy is installed or not:

  1. Open run and type cmd
  2. Type pip list
  3. Check numpy is there or not in the list
Introduction to Numpy

Installing numpy is quite easy:

  • Open run and type cmd
  • Type pip install numpy
  • Press enter after some processes it will be installed

Using NumPy

Use import statement after installation to work with NumPy

import numpy as np

Where “import” and “as” is a keyword and np is alias name

Now write “np.” to use NumPy

Checking version of NumPy

import numpy as np
print(np.__version__)

Creating Arrays

empty() – creates arrays whose initial content is random() and depends on the state of memory

array()– Accepts sequences as argument that can be two dimensional or three dimensional and so on.

zeros() –  creates array of zero in specified shape

ones() – creates array of ones in specified shape

arange() – creates arrays from the specified range, accept float value as step

linspace() – Create array of evenly spaced numbers

tile() – create an array by repeating a smaller array a specified number of times

eye() – creates a 2-D array with ones on the diagonal and zeros elsewhere. Identity matrix

identity() – creates a square identity matrix — a 2-D array with ones on the main diagonal and zeros elsewhere.

full() – creates a new array of a specified shape and fills it with a constant value

Watch this video for practical understanding:

Download the jupyter notebook file created in live session by following this link:

Download the notebook file creating arrays

If you are looking for python basic programs, follow this link:

Python programs for beginners

That wraps up our Introduction to NumPy! Whether you’re just starting your journey into machine learning or brushing up on core Python tools, mastering NumPy is a step in the right direction. If you found this post helpful, don’t forget to like, share, and drop a comment below—your feedback helps me create better content for learners like you!

Leave a Reply


Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://api.aspose.cloud/connect/token` resulted in a `429 Too Many Requests` response in /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}() #2 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run() #5 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Client.php(123): GuzzleHttp\Promise\Promise->wait() #10 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50640): GuzzleHttp\Client->send() #11 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50648): Aspose\Words\WordsApi->_requestToken() #12 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50654): Aspose\Words\WordsApi->_checkAuthToken() #13 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(50666): Aspose\Words\WordsApi->_getKey() #14 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(80): Aspose\Words\WordsApi->_checkRsaKey() #15 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/Util.php(24): Aspose\Words\WordsApi->__construct() #16 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/ExportEngine.php(87): AsposeWords\Util::getWordsApi() #17 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/src/AsposeWords/AutoExport.php(26): AsposeWords\ExportEngine->convert() #18 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/class-wp-hook.php(324): AsposeWords\AutoExport->export() #19 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #20 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/plugin.php(517): WP_Hook->do_action() #21 /home/u251245109/domains/tutorialaicsip.com/public_html/wp-includes/load.php(1279): do_action() #22 [internal function]: shutdown_action_hook() #23 {main} thrown in /home/u251245109/domains/tutorialaicsip.com/public_html/wp-content/plugins/aspose-doc-exporter/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113