KEMBAR78
Numpy | PDF | Computer Engineering | Computer Science
0% found this document useful (0 votes)
3 views10 pages

Numpy

bekar hai bhai

Uploaded by

us1800439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Numpy

bekar hai bhai

Uploaded by

us1800439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Numpy

NumPy (Numerical Python) is an open-source Python library that’s


used in almost every field of science and engineering.
NumPy is a general-purpose array-processing package. It provides a
high-performance multidimensional array object and tools for
working with these arrays.
NumPy was created in 2005 by Travis Oliphant.

Why is NumPy Faster Than Lists?


 NumPy arrays are stored at one continuous place in memory
unlike lists, so processes can access and manipulate them very
efficiently.
 This behaviour is called locality of reference in computer
science.

NumPy Array Creation

>>> a = numpy.array
>>> print(a)
<buit-in function array>

Syntax:-numpy.array(object, dtype = None, copy = True, order = None, s


ubok = False, ndmin = 0)

S Parameter Description
N

1 object It represents the collection object. It can be a list, tuple, dictionary, set, e

2 dtype We can change the data type of the array elements by changing this op
the specified type. The default is none.

3 copy It is optional. By default, it is true which means the object is copied.


4 order There can be 3 possible values assigned to this option. It can be C (
order), R (row order), or A (any)

5 subok The returned array will be base class array by default. We can change
make the subclasses passes through by setting this option to true.

6 ndmin It represents the minimum dimensions of the resultant array.

Creating One-Dimensional array:-

>>> import numpy as np


>>> my_array=np.array([1,2,3,4])
>>> print(my_array)
O/P:- [1,2,3,4]
(OR)
>>> import numpy as np
>>> L= [1,2,3,4]
>>> my_arr=np.array(L)
>>> my_arr
O/P: array([1, 2, 3, 4])

Create an Array Using np.zeros():


import numpy as np
# create an array with 4 elements filled with zeros
array1 = np.zeros(4)
print(array1)
# Output: [0. 0. 0. 0.]

Create an Array With np.arange():


import numpy as np
# create an array with values from 0 to 4
array1 = np.arange(5)
print("Using np.arange(5):", array1)
# create an array with values from 1 to 8 with a step of 2
array2 = np.arange(1, 9, 2)
print("Using np.arange(1, 9, 2):",array2)

Create an Array With np.random.rand():


import numpy as np
# generate an array of 5 random numbers
array1 = np.random.rand(5)
print(array1)
array2=np.random.randint(1,10,4)
print(array2)
output:- array([4,4,2,7])
array3=np.random.seed(12)

Create an Empty NumPy Array:


import numpy as np
array1 = np.empty(4)
print(array1)

Create a 2-Dimensional NumPy Array: -


Array Creation from List of Lists:
import numpy as np
array1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(array1)
Create a 3-D NumPy Array:
import numpy as np
# create a 3D array with 2 "slices", each of 3 rows and 4 columns
array1 = np.array([
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]],

[[13, 14, 15, 16],


[17, 18, 19, 20],
[21, 22, 23, 24]]
])
print(array1)

Create N-D Arrays using np.zeros():


import numpy as np
# create 2D array with 2 rows and 3 columns filled with zeros
array1 = np.zeros((2, 3))
print("2-D Array: ")
print(array1)
# create 3D array with dimensions 2x3x4 filled with zeros
array2 = np.zeros((2, 3, 4))
print("\n3-D Array: ")
print(array2)

Array with a Specified Value:


import numpy as np
# Create a 2-D array with elements initialized to 5
numpy_array = np.full((2, 2), 5)
print("Array:", numpy_array)

Creating Arrays With np.random.rand():


import numpy as np
# Create a 2D array of 2 rows and 2 columns of random numbers
array1 = np.random.rand(2, 2)
print("2-D Array: ")
print(array1)
# Create a 3D array of shape (2, 2, 2) of random numbers
array2 = np.random.rand(2, 2, 2)
print("\n3-D Array: ")
print(array2)

Create Empty N-D NumPy Array:


import numpy as np
# create an empty 2D array with 2 rows and 2 columns
array1 = np.empty((2, 2))
print("2-D Array: ")
print(array1)
# create an empty 3D array of shape (2, 2, 2)
array2 = np.empty((2, 2, 2))
print("\n3-D Array: ")
print(array2)

numpy.reshape() in Python: -

import numpy as np
array1 = np.arange(8)
print("Original array : \n", array1)
# shape array with 2 rows and 4 columns
array2 = np.arange(8).reshape(2, 4)
print("\narray reshaped with 2 rows and 4 columns : \n", array2)
# shape array with 4 rows and 2 columns
array3 = np.arange(8).reshape(4, 2)
print("\narray reshaped with 4 rows and 2 columns : \n", array3)
# Constructs 3D array
array4 = geek.arange(8).reshape(2, 2, 2)
print("\nOriginal array reshaped to 3D : \n", array4)

Common NumPy Attributes: -

Attributes Description

ndim returns number of dimension of the array

size returns number of elements in the array

dtype returns data type of elements in the array

shape returns the size of the array in each dimension.

itemsize returns the size (in bytes) of each elements in the array
data returns the buffer containing actual elements of the array in memory

import numpy as np
# create a 2-D array
array1 = np.array([[2, 4, 6], [1, 3, 5]])
check the dimension of array1
print(array1.ndim)

return total number of elements in array1


print(array1.size)

return a tuple that gives size of array in each dimension


print(array1.shape)

check the data type of array1


print(array1.dtype)

NumPy Array itemsize Attribute: -


# create a default 1-D array of integers
array1 = np.array([6, 7, 8, 10, 13])
# create a 1-D array of 32-bit integers
array2 = np.array([6, 7, 8, 10, 13], dtype=np.int32)
# use of itemsize to determine size of each array element of array1 and array2
print(array1.itemsize) # prints 8
print(array2.itemsize) # prints 4
NumPy Array data Attribute: -
array1 = np.array([6, 7, 8])
array2 = np.array([[1, 2, 3], [6, 7, 8]])
# print memory address of array1's and array2's data
print("\nData of array1 is: ",array1.data)
print("Data of array2 is: ",array2.data)

1D NumPy Array Slicing: -


import numpy as np
array1 = np.array([1, 3, 5, 7, 8, 9, 2, 4, 6])
print(array1[2:6]) # [5 7 8 9]
print(array1[0:8:2]) # [1 5 8 2]
print(array1[3:]) # [7 8 9 2 4 6]
print(array1[:]) # [1 3 5 7 8 9 2 4 6]
print(numbers[-3:]) # [8 10 12]
print(numbers[-5:-2]) # [4 6 8]
print(numbers[-1::-2]) # [12 8 4]
2D NumPy Array Slicing: -

Syntax of 2D NumPy Array Slicing


array[row_start:row_stop:row_step, col_start:col_stop:col_step]
Example: -
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15]])
print(array1[:2, :2])
# Output
[[ 1 3]
[ 9 11]]

NumPy Arithmetic Array Operations: -

import numpy as np
first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])

# using the + operator


result1 = first_array + second_array
print("Using the + operator:",result1)
# using the add() function
result2 = np.add(first_array, second_array)
print("Using the add() function:",result2)

# using the - operator


result1 = first_array - second_array
print("Using the - operator:",result1)
# using the subtract() function
result2 = np.subtract(first_array, second_array)
print("Using the subtract() function:",result2)

# using the * operator


result1 = first_array * second_array
print("Using the * operator:",result1)
# using the multiply() function
result2 = np.multiply(first_array, second_array)
print("Using the multiply() function:",result2)

# using the / operator


result1 = first_array / second_array
print("Using the / operator:",result1)
# using the divide() function
result2 = np.divide(first_array, second_array)
print("Using the divide() function:",result2)

# using the ** operator


result1 = array1 ** 2
print("Using the ** operator:",result1)
# using the power() function
result2 = np.power(array1, 2)
print("Using the power() function:",result2)

You might also like