0% found this document useful (0 votes)
6 views

XIAINumpy (1)

Uploaded by

NAKSHATRA BANSAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

XIAINumpy (1)

Uploaded by

NAKSHATRA BANSAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

NUMPY - ARRAY

NumPy stands for Numerical Python. It is the core library for


scientific computing in Python. It consist of multidimensional
array objects, and tools for working with these arrays.
Arrays.
Numpy Array is a grid of values with same type, and is indexed
by a sequence of nonnegative integers. The number of elements
(dimensions) of it ,is the rank of the array and the shape of an
array depends upon a tuple of integers giving the size of the
array along each dimension.
1 D Array
An array is a container which hold a fixed number of items of
same data type. It is a contiguous(one after another) memory
location.
An array of one dimension is known as a one-dimensional
array or 1-D array.

In above diagram num is an array ,it’s first element is at 0 index


position ,next element is at 1 and so on till last element at n-1 index
position .At 0 index position value is 2 and at 1 index position value
is 5.
1 D Array
How to use/import NumPy Library
import numpy as np # Here np is an alias for NumPy

Creation of 1D array
One dimension array can be created using array method with list object
with one dimensional elements.

e.g.
import numpy as np # np is an alias for NumPy
a = np.array([5,2,3,4]) # Create a 1D Array
print(a) # Output - [5 2 3 4]
print(a[0], a[1], a[2]) # Output - 5 2 3
a[0] = 15 # Change an element of the array
print(a) # Output - [15 2 3 4]
1 D Array
Creation of 1D array using functions:
import numpy as np
p = np.empty(5) # Create an array of 5 elements with random values
print(p)

a1 = np.zeros(5) # Create an array of all zeros float values


print(a1) # Print "[0. 0. 0. 0. 0.]"

a2 = np.zeros(5, dtype = np.int) # Create an array of all zeros int values


print(a2) # Print "[0 0 0 0 0]"

b = np.ones(5) # Create an array of all ones


print(b) # Print "[1. 1. 1. 1. 1.]"

c = np.full(5, 7) # Create a constant array


print(c) # Print "[7 7 7 7 7]"
1 D Array
Difference between Numpy array and list

NUMPY ARRAY LIST

Numpy Array works on Python list are made for


homogeneous types heterogeneous types

Numpy Array does not support Python list support adding and
adding and removing of removing of elements
elements
Can’t contain elements of different can contain elements of different
types types

smaller memory consumption more memory consumption

faster runtime Runtime is comparatively slow


 Create numpy array from arange function
numpy.arange(start, stop, step, dtype)
Note: By default start =0, step=1 and data type =int

#example:1
import numpy as np
x = np.arange(5) # for float value specify dtype = float as argument
print(x) # print [0 1 2 3 4]

#example:2
import numpy as np
x = np.arange(10,20,2)
print (x) #print [10 12 14 16 18]
 Create 1D from array
Copy function is used to create the copy of the existing array.
e.g. program

import numpy as np
x = np.array([1, 2, 3])
y=x
z = np.copy(x)
x[0] = 10
print(x)
print(y)
print(z)

Note :- In the above code both x and y will point to the same array but z will
be the separate array so when we modify x, y changes, but not z.

Output:
[10 2 3]
[10 2 3]
[1 2 3]
1 D ARRAY JOINING

Joining of two or more one dimensional array is possible


with the help of concatenate() function of numpy object.

e.g.program

import numpy as np
a = np.array([1, 2, 3])
b = np.array([5, 6])
c=np.concatenate([a,b,a])
print(c) #print [1 2 3 5 6 1 2 3]
Basic arithmetic operation on 1D Array
We can perform all the basic arithmetic operation on array as follows:

import numpy as np
x = np.array([1, 2, 3,4])
y = np.array([1, 2, 3,4])
z=x+y
print(z) #print [2 4 6 8]
z=x-y
print(z) #print [0 0 0 0]
z=x*y
print(z) #print [ 1 4 9 16]
z=x/y
print(z) #print [1. 1. 1. 1.]
z=x+1
print(z) #print [2 3 4 5]
Use of Aggregate function on 1D Array

import numpy as np

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

print(x.sum()) #print 10

print(x.min()) #print 1

print(x.max()) #print 4

print(x.mean()) #print 2.5

print(np.median(x)) #print 2.5


2 D ARRAY

In above diagram num is an array of two dimension with 3 rows and


4 columns.subscript of rows is 0 to 2 and columns is 0 to 3.
2 D ARRAY
Creation of 2D array:
Two dimension array can be created using array method with list object
with two dimensional elements.

import numpy as np
a = np.array([[3, 2, 1],[4, 2, 3]]) # Create a 2D Array
print(a[0][1]) # Prints 2
a[0][1] = 15 # Change an element of the array
print(a) # Prints [[3 15 1]
[4 2 3]]
Creation of 2D array Using functions:
import numpy as np
p = np.empty([2,2]) # Create an array of 4 elements with random values
print(p)

a1 = np.zeros([2,2]) # Create 2d array of all zeros float values


print(a1) # Prints [[0. 0.][0. 0.]]

a2 = np.zeros([2,2], dtype = np.int) # Create an array of all zeros int values


print(a2) # Prints [[0 0] [0 0]]

b = np.ones([2,2]) # Create an array of all ones


print(b) # Prints [[1. 1.] [1. 1.]]

a2 = np.ones([2,2], dtype = np.int) # Create an array of all ones


print(a2) # Prints [[1 1] [1 1]]

c = np.full([2,2], 7) # Create a constant array


print(c) # Prints [[7 7] [7 7]]
2 D ARRAY

ARITHMATIC OPERATION USING FUNCTION:


import numpy as np
a = np.array([[2,4,6],[3,6,9]]) Output:
print(a) [[2 4 6]
[3 6 9]]
b=np.array([[2,2,2],[1,2,3]]) [[2 2 2]
print(b) [1 2 3]]

c=np.add(a,b) # Similar to c=a+b [[ 4 6 8]


print(c) [ 4 8 12]]

c=np.subtract(a,b) # Similar to c=a-b [[0 2 4]


print(c) [2 4 6]]

c=np.multiply(a,b) # Similar to c=a*b [[ 4 8 12]


print(c) [ 3 12 27]]

c=np.divide(a,b) # Similar to c=a/b [[1. 2. 3.]


print(c) [3. 3. 3.]]

You might also like