Here are the solutions to the problem statements:
1. arr = np.arange(13,25).reshape(3,4)
2.
i) P = P + 10
ii) R = P * Q
iii) Q = Q / 7
iv) P = np.log(P)
v) Q = np.round(Q)
vi) R = P % 7
vii) Q = np.sqrt(Q)
1. CLASS- XII
PCT-2 UNIT - 1 CHAPTER -8 Data Visualization : Numpy Array
Prepared By -
Swati Patil
BE, MCA,PGDHRM
Contact -
8618134279 (Queries only through SMS)
https://pythonxiisolutions.blogspot.com prippython12.blogspot.com
2. Difference between List and Numpy Array
List Array
The list can be homogeneous or heterogeneous The array are homogeneous
Elements of List are not stored contiguously in memory.
2D list may have different number of rows and columns
can
Elements of an array are stored contiguously in
memory. For example, all rows of a 2D array must have
the same number of columns. Or a 3D array must have
the same number of rows and columns on each card.
Python list is by default 1 dimensional. But we can
create an N-Dimensional list. But then too it will be 1 D
list storing another 1D list
Arrays are multi dimensional
Element wise operation is not possible in list.
Example-
# adding 4 to each element of list
ls = ls + 4
Element wise operation is possible in array.
Example-
# adding 4 to each element of Numpy array
arr = arr + 4
range () is used arange() is used
3. Numerical Python (Num Pie)- It offers function for fast mathematical computation on arrays or
matrices.
PACKAGE:
numpy
import numpy
as np
# Create an array from a list
>>>L=[2,3,4,5]
>>>a=np.array(L)
# use iterator to create array
np.fromiter(iter(range(5)),dtype=int)
array([0, 1, 2, 3, 4])
# Find length of each element of array in bytes
>>>a.itemsize
8
#uninitialized array of specified shape and dtype
x = np.empty([3,2], dtype = int32)
# Find dimension of Numpy array, i.e no of elements
along each axis.
>>>a.shape # returns a Tuple
>>> (4,)
#a reshape function to resize an array.
>>>a.reshape(4,1)
# slicing array
s = slice(1,7,2) OR b = a[1:7:2]
[ 3 5 ] [ 3 5 ]
# ellipsis(...)
a=p.array([[1,2,3],[3,4,5],[4,5,6]])
print(a[...,1],a[1,...],a[...,1:])
[2 4 5] [3 4 5] [[2 3]
[4 5]
[5 6]]
#a new array of specified size, filled with zeros.
x = np.zeros(5)
[ 0. 0. 0. 0. 0.]
#a new array of specified size and type, filled with
ones.
x = np.ones(5)
[ 1. 1. 1. 1. 1.]
# Find data type of an array
>>>a.dtype
dtype('float64')
5. Dtypes of array
Data Type Size Range
np.int8 1 byte -128 to 127
np.int16 2 byte -32768 to 32767
np.int32 4 byte -216
to 216
-1
np.int64 8 byte -232
to 232
-1
np.float_ 8 byte 11exp + 52manti
np.float16 2byte 5exp + 10manti
np.float32 4 byte 8exp+ 23manti
np.float64 8 byte 11exp + 52manti
Problem Statements to work out :.
1.Create a 3 x 4 two-dimensional ndarray from the range of
integers 13..24
2.Write commands to perform following operations on two 4 x 4
ndarrays namely P and Q :
i>Adding 10 to P
ii> multiplication of P & Q
iii>Divide all elements of Q by 7
iv>Calculate log of all elements of P
v>Round all elements of Q to nearest integer
vi>calculate remainders of all elements of P when divided by 7
vi>Calculate square root of all elements of Q