Know Numpy

Know Numpy

INTRODUCTION:

stands for Numerical Python and is used for handling large, multi-dimensional arrays and matrices. Unlike Python's built-in lists NumPy arrays provide efficient storage and faster processing for numerical and scientific computations. It offers functions for linear algebra and random number generation making it important for data science and machine learning.

CREATING ARRAYS

The numpy array object in Numpy is called ndarray. We can create an ndarray using this function.

import numpy as np

a = np.array([3,4,5,5])

print(a)

DIMENSIONS

1-D Arrays

arr = np.array([1, 2, 3, 4, 5])

print(arr)

[1 2 3 4 5]

2-D Arrays

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)

[[1,2,,3],

[4,5,6]]

3-D arrays

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(arr)

[[[1, 2, 3],

[4, 5, 6]],

[[1, 2, 3],

[4, 5, 6]]]

ATTRIBUTES OF ARRAYS

  • ndim: shows the dimension of an array, e.g, 1D, 2D
  • shape: shows the no of elements along with each axis, e.g, [[1, 2, 3], [4, 5, 6]] : (2,3)
  • dtype: tells. the data type of an array, e.g, [10.0, 20.0, 30.0]: dtype( 'float64')
  • size: shows the no of elements, e.g, [[1, 2, 3], [4, 5, 6]] : (9)

Here’s the text content from the image:


COMPARISON: Array vs List

Array

  • All elements of an array are of same data type.
  • Elements of an array are stored in contiguous memory locations.
  • Arrays are static and cannot be resized once they are created.
  • Arrays support element-wise operations.
  • A NumPy array occupies less space in memory.

List

  • A list can have elements of different data types.
  • List elements are not stored contiguously in memory.
  • Lists can be resized and modified easily.
  • Lists do not support element-wise operations.
  • More space in memory.



To view or add a comment, sign in

More articles by Fatima Jawed

  • Conditionals in Python

    Conditionals allow us to run code only when a specific condition is true. They help us make decisions and control how…

  • Concatenation in Python

    In Python, concatenation primarily applies to sequence data types, such as strings, lists, and tuples. The core…

    1 Comment
  • Harness the Potential of Python's Data Types

    Certainly! Whether you're a novice or an experienced coder, delving into the fascinating realm of Python data types is…

Explore content categories