Installation of Numpy Step1. In pycharm,go to terminal Step 2.Write following command Or Step 1. Goto project settings in Pycharm Step2. Select Python interpreter Step3. Search package to install and add
Arrays in python import array # initializing array with array values # initializes array with signed integers arr = array.array( 'i' , [ 1 , 2 , 3 ]) # printing original array print ( "The new created array is : " ,end = "") for i in range ( , 3 ): print (arr[i], end = " " ) Array module can be imported to create arrays in python but operations on such arrays was very slow Due to its slow response,it lead to use of Numpy
Datatype of Arrays in python TYPE CODE C TYPE PYTHON MINIMUM SIZE TYPE IN BYTES ‘b’ signed char int 1 ‘B’ unsigned char int 1 ‘i’ signed int int 2 ‘I’ unsigned int int 2 ‘f’ float float 4 ‘d’ double float 8 These are the datatypes which can be used while creating array using array module
Array creation using numpy We can use arange function from NumPy that creates a NumPy array for us with integers to n. import numpy arr=numpy.arange ( 5 ) fo r i i n rang e ( , le n (arr)): print (arr[i]) Output
Difference between code with numpy and without numpy Took 3000 microseconds Took 2000 microseconds for i in range ( , len (arr1)): arr3.append(arr1[i]+arr2[i]) print (arr3[i], end = " " ) Without numpy import datetime def printelements(n): arr1= range (n) arr2= range (n) arr3=[] start = datetime.datetime.now() printelements ( 30 ) diff = datetime.datetime.now() - start print ( "time taken" , diff.microseconds) import numpy as n import datetime def printelements(num): arr1=n.arange(num) arr2=n.arange(num) arr3=arr1+arr2 for i in range ( , len (arr1)): print (arr3[i], end = " " ) start = datetime.datetime.now() printelements ( 30 ) diff = datetime.datetime.now() - start print ( "time taken" , diff.microseconds) Adding two arrays With numpy
Datatypes in Numpy Python has an integer type, a float type, and a complex type, however, this is not enough for scientific computing and, for this reason, NumPy has a lot more data types.Datatypes are associated with different precision and different memory size. We can view the datatype of array created import numpy as n arr1=n.arange ( 1 2 ) print (arr1.dtype) The data type of array a is int32 (at least on my machine), but you may get int64 as output if you are using 64-bit Python.
Character codes associated with datatypes Character codes are included for backward compatibility with Numeric. Numeric is the predecessor of NumPy. Their use is not recommended, but the codes are provided here because they pop up in several places. You should instead use dtype objects. Example: Output :
Datatype(dtype) Objects & Constructors Data type objects are instances of the numpy.dtype class. Once again, arrays have a data type. Every element in a NumPy array has the same data type. Each dtype object has itemsize attribute which tell you the size of the data in bytes. dtype constructors We have a variety of ways to create data types. We can use either numeric datatypes or character codes to be passed to constructor to create object as shown below: D1=dtype(“float16) D2=dtype(“f”) Create the record : To create heterogenous datatype which contain multiple datatype D3 = dtype([('name', str, 40), ('numitems', int), ('price',float)])
Dtype constructor examples We can use numeric datatypes: dtype(float32) We can specify a single precision float with a character code: dtype('f') We can use a double precision float character code: dtype('d') We can use a complex character code: dtype(‘D') We can give the data type constructor a two-character code. The first character signifies the type; the second character is a number specifying the number of bytes in the type: dtype('f8') Example: Output:
Dtype Attributes Attribute Description dtype.char Gives a unique character code for each of the 21 different built-in types. E.g ‘D’ dtype.type corresponds to the type of object of the array elements e.g float32 dtype.str Gives string representation of the data type. It starts with a character representing endianness(<or>), then a character code, followed by a number corresponding to the number of bytes that each array item requires. Eg. ‘<f8’ <- Endianness, f-float,8-8 bytes dtype.byteorder (Endiannness) Endianness, here, means the way bytes are ordered within a 32 or 64-bit word. In big-endian order, the most significant byte is stored first. In little-endian order, the least significant byte is stored first. dtype.itemsize Gives size of data in bytes example 64
ENDIANNESS 0x000400 00 0x000400 01 0x000400 02 0x000400 03 0x000400 00 0x000400 01 0x000400 02 0x000400 03 Consider number 0x 12 6745 92 in 32-bit representation can be stored as − MSB LSB
Examples of dtype attributes
Creating Multidimensional Array Use array() function to create 2-D array from numpy import * matrix1= array([[ , 2 ],[ 3 , 4 ]]) Or row1=array([1,2]) row2=array([3,4],’I’) matrix1 = array([row1,row2]) print(matrix1) □[0,2] [3,4]
Creating Multidimensional Array Program to create a matrix and display it from numpy import * matrix1 = array([[ , 2 ],[ 3 , 4 ]]) for i in range ( , len (matrix1[ ])): for j in range ( , len (matrix1)): print (matrix1[i][j], end = " " ) print ( " \n " ) #len(matrix1[0]) give number of columns in first row #len(matrix1) gives number of rows Output To add two matrices simply create two matrices with data and third zero matrix matrix1 = array([[ , 2 ],[ 3 , 4 ]]) matrix2 = array([[ , 2 ],[ 3 , 4 ]]) matrix3 = array([[ ,0],[ 0,0 ]]) Then Simply add in loop matrix3[i][j]= matrix1[i][j]+matrix2[i][j]=