,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
It is a predefined library of python which is used to perform a mathematical operation using predefined methods.
NumPy uses array-type data to perform the operation.
it contains array-type data to accept input data.
Operation using NumPy:-
Using NumPy, a developer can perform the following operations −
⦁ Mathematical and logical operations on arrays.
⦁ Fourier transforms and routines for shape manipulation.
⦁ Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.
What is the Difference between NumPy Array and List?
1) Numpy array has n-th dimensional means it can be one dimension to nth dimension but the list has only 1 or 2 dimension approach.
2) Numpy array is used to contain data of data science to implement multiple functionalities of scipy, NumPy, pandas, and matplotlib but the list is used to provide python core functionality.
3) Numpy array speed and performance are best as compare to List.
4) Numpy array space is fixed but the list is resizable.
5) Numpy array can contain datatype and dimension attribute to store similar type of elements using proper sequence.
Q) Can we convert the list objects to NumPy array?
Answer) Yes, we can convert the NumPy array to a list.
import numpy as np
arr = [10,20,30,70,11]
a = np.array(arr)
print(a)
How to install NumPy:-
before installation of NumPy python, pip, and python environment paths should be set.
open Command Prompt and Type:-
pip install NumPy
It is called NDArray, which means we can create one-dimension,two-dimension, and multidimensional arrays using Numpy.
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
Display elements on NumPy Multidimensional Array?
open Command Prompt and Type:-
pip install NumPy
First, you should install the Jupyter notebook offline version or online version.
https://jupyter.org/try
Numpy array:-It is called NDArray, which means we can create one-dimension,two-dimension, and multidimensional arrays using Numpy.
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
Display elements on NumPy Multidimensional Array?
a = np.array(([1,2],[3,4]))
for i in range(0,2):
for j in range(0,2):
print(a[i][j],end=' ')
print()
Assignments:-
1) WAP to calculate the sum and multiply two different matrices using NumPy array?
2) WAP to calculate Sum of row elements into NumPy array?
3) WAP to display total prime elements into NumPy array?
Solve these assignments of NumPy array?
WAP to find max element in NumPy array?
WAP to sort the elements of the NumPy array?
WAP to split one array into two different subarrays?
WAP to merge two NumPy arrays into one array?
Solution of this program?
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
size= len(a)+len(b)
c = np.array([int]*size)
for i in range(0,size):
if i<len(a):
c[i]=a[i]
else:
c[i]=b[i-len(a)]
print(c)
WAP to display prime elements in Numpy array
How many ways to declare NumPy array:-
1) using NumPy array():-
array() is used to convert list objects to NumPy array
It is used to create a NumPy array using common type elements and specific elements both.
arrayname = numpy.array([elements,....],dtyle='datatype'])
Example:-
import numpy as np
a = np.array([1,2,3,4,5])print(a)
2) using numpy arange():-
It is used to create a NumPy array using starting and ending points with increment value
arrayname = numpy.arange(start,end,increment)
Example
import numpy as np
arr = np.arange(1,10,2,dtype='float')
print(arr)
3) using linspace():-
It is used to display elements based on the range with the same distance interval, distance will be calculated by starting value and ending value.
arrayname = numpy.linspace(start,end,increment)
import numpy as np
arr = np.linspace(1,10,5)
arr
4) using empty():-
It is used to display random elements, we will provide a number of rows and columns using empty().
It will create random elements.
arrayname = numpy.empty((row,column),dtype='datatype')
import numpy as np
arr = np.empty((3,3),dtype='int')
arr
Numpy Operation:-
Numpy provides various predefined methods to manage array operation
1) ndmin:- it is used to convert array to multiple dimension
a = np.array([1,2,3,4],ndmin=2)
2) dtype:- It is used to convert array elements to different types of elements.
a = np.array([1,2,3,4],dtype=complex)
3) shape:- it will return a number of rows and column
print(a.shape) //
a complete example of NumPy:-
import numpy as np
Numpy provides various predefined methods to manage array operation
1) ndmin:- it is used to convert array to multiple dimension
a = np.array([1,2,3,4],ndmin=2)
2) dtype:- It is used to convert array elements to different types of elements.
a = np.array([1,2,3,4],dtype=complex)
3) shape:- it will return a number of rows and column
print(a.shape) //
a complete example of NumPy:-
import numpy as np
#a = np.array([1,2,3,4],ndmin=2)
a = np.array([[1, 2], [3, 4]])
#a = np.array([1,2,3,4],dtype=complex)
print(a)
print(a.shape)
4) numpy.zeros():- this method will create zero values array element
import numpy as np
print(np.zeros((2,2)))
5) numpy.ones():- this method will create one values array elements
import numpy as np
print(np.ones((2,2)))
6) numpy.reshape():- it is used to transpose the matrix or multi-dimension array.
for example, if the matrix is 2*3 then it will convert into 3*2
numpy.reshape(a, newShape, order='C')
import numpy as np
e = np.array([(1,2,3), (4,5,6)])
print(e)
var = e.reshape(3,2)
a = np.array([[1, 2], [3, 4]])
#a = np.array([1,2,3,4],dtype=complex)
print(a)
print(a.shape)
4) numpy.zeros():- this method will create zero values array element
import numpy as np
print(np.zeros((2,2)))
5) numpy.ones():- this method will create one values array elements
import numpy as np
print(np.ones((2,2)))
6) numpy.reshape():- it is used to transpose the matrix or multi-dimension array.
for example, if the matrix is 2*3 then it will convert into 3*2
numpy.reshape(a, newShape, order='C')
import numpy as np
e = np.array([(1,2,3), (4,5,6)])
print(e)
var = e.reshape(3,2)
print(var)
var=np.reshape(e,(3,2),order='C')
var=np.reshape(e,(3,2),order='C')
print(var)
7) flatten:- It is used to display array in column style, Multi dimension array to single dimension array we will use flatten().
7) flatten:- It is used to display array in column style, Multi dimension array to single dimension array we will use flatten().
numpy.flatten(order='C')
import numpy as np
e = np.array([(1,2,3), (4,5,6)])
var=e.flatten()
print(var)
8) hstack() and vstack():-
hstack():- it is used to append array data horizontally and display results using a single dimension array.
a = np.array([1,2,3])
b = np.array([4,5,6])
c= numpy.hstack((a,b))
vstack():- it is used to append array data vertically and display results into a multi-dimension array.
a = np.array([1,2,3])
b = np.array([4,5,6])
d = numpy.vstack((a,b))
Program to implement vstack and hstack:-
a = np.array([1,2,3])
b = np.array([4,5,6])
c= numpy.hstack((a,b))
vstack():- it is used to append array data vertically and display results into a multi-dimension array.
a = np.array([1,2,3])
b = np.array([4,5,6])
d = numpy.vstack((a,b))
Program to implement vstack and hstack:-
import numpy as np
a = np.array([1,2,3])
b = np.array([4,7,8])
c = np.hstack((a,b))
d= np.hstack((a,b))
9) numpy.random.normal():- It is used to display random numbers based on start, distance, and last index.
numpy.random.normal(loc, scale, size)
Here
numpy.linspace(start, stop, size, endpoint)
It is used to subdivide the range data based on size
example of linespace():-
import numpy as np
a=np.linspace(0.0, 1.0, num=6)
print(a)
,........................................................................................................................
## Determinant 2*2 matrix ###
i = [[5,6],[7,8]]
5*8-7*6
np.linalg.det(i)
Numpy Eigen Function Example:-
This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.
a = np.array([1,2,3])
b = np.array([4,7,8])
c = np.hstack((a,b))
d= np.hstack((a,b))
9) numpy.random.normal():- It is used to display random numbers based on start, distance, and last index.
numpy.random.normal(loc, scale, size)
Here
- Loc: the mean. The center of distribution
- scale: standard deviation.
- Size: number of returns
## Generate random nmber from normal distribution normal_array = np.random.normal(5, 0.5, 10) print(normal_array) [5.56171852 4.84233558 4.65392767 4.946659 4.85165567 5.61211317 4.46704244 5.22675736 4.49888936 4.68731125]10) linspace():- it is used to provide a sequence of data using starting point, ending point, and size.
numpy.linspace(start, stop, size, endpoint)
It is used to subdivide the range data based on size
example of linespace():-
import numpy as np
a=np.linspace(0.0, 1.0, num=6)
print(a)
,........................................................................................................................
LogSpace
LogSpace returns even spaced numbers on a log scale. Logspace has the same parameters as np.linspace.numpy.logspace(start, stop, num, endpoint)
import numpy as np
a=np.logspace(3, 4, num=3) print(a)
it will provide 10**3 and 10**3.5 , 10**4 10 is the default base of log
.................................................................
Indexing and slicing in Numpy:-
Indexing is used to display a particular element of the NumPy array.
slicing is the show data of a particular range
Q) WAP to display first row of numpy array?
import numpy as np e = np.array([(1,2,3), (4,5,6)]) print(e[0])
Another example of slicing :-
Q) Display Particular row in numpy array?
import numpy as np
e = np.array([(1,2,3), (4,5,6)]) print(e[:2])
Q) Display numpy array elements using index array?
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) arr = x[np.array([1, 3, -3])] print("\n Elements are : \n",arr)
Q) Display numpy array elements using starting index, ending index and step
Syntax
arr[start:last:step]
Example of Display NumPy array using starting, ending and step's.
import numpy as np arr = np.arange(20) print(arr) arr1 = arr[1:5:2] print("\n Elements are : \n",arr1)
Advance Indexing:-
1) Indexing by tuple index
We can fetch ndarray using paired index So the index of the elements, in this case, are (0,0),(1,0),(2,1) and the corresponding elements are selected.
import numpy as np
a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
print(a)
print(a[[0 ,1 ,2 ],[0 ,0 ,1]])
2) Indexing by boolean:-
using this we can provide condition to fetch elements from numpy array.
arr = np.array([23,67,89,11,22]) print(arr[arr>60])
1) How to read data from excel file ?
We can read data from excel file using two different module, xlrd openpyxl .
xlrd is only used to read .xlx file extension for old xlx file
openpyxl is used to read .xls and .xlsx both
1) Example to read xlx file using xlrd module.
import xlrd # Give the location of the file loc = ("d://covidnew.xls") # To open Workbook wb = xlrd.open_workbook(loc) sheet = wb.sheet_by_index(0) # For row 0 and column 0 print(sheet.cell_value(0, 0)) print("Total no of rows is ",sheet.nrows) print("Total no of columns is ",sheet.ncols)
data repository can be download by following link.
https://github.com/shivaconceptsolution/repository
Now i am explaining another module openpyxl to read data from xlsx file.
pip install openpyxl
Complete Program Explanation to Read Data From openpyxl
Solution:-
from openpyxl import load_workbook wb = load_workbook('d://covid.xlsx') ws = wb.active ar =[] #Empty List col = ws.max_column ro = ws.max_row for i in range(1,(ro+1)): k=0 ar1=[]#Empty List for j in range(1,(col+1)): c1 = ws.cell(row = i, column = j) ar1.append(c1.value) k+=1 ar.append(ar1) print(ar) #arr = np.concatenate((ar)) #arr1 = np.hstack((ar))# Joining Numpy Array using hstack(). #arr2 = np.vstack((ar))# Joining Numpy Array using vstack().
NumPy Statistical Functions with Example:-
it is used to provide in-built() to implement statistical operation using min,max,deviation,variance,median,mean
import numpy as np
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)
print(np.min(normal_array))
### Max
print(np.max(normal_array))
### Mean
print(np.mean(normal_array))
### Median
print(np.median(normal_array))
### Sd
print(np.std(normal_array))
normal_array = np.random.normal(5, 0.5, 10)
print(normal_array)
print(np.min(normal_array))
### Max
print(np.max(normal_array))
### Mean
print(np.mean(normal_array))
### Median
print(np.median(normal_array))
### Sd
print(np.std(normal_array))
Another example of statistical function:-
import numpy as np
arr = np.array([23,11,78,11,90,160]) print(np.min(arr)) print(np.max(arr)) print(np.median(arr)) print(np.mean(arr)) print(np.std(arr)) print(np.average(arr))
arr= np.array([[1,23,78],[98,60,75],[79,25,48]])
print(arr) print(np.amax(arr),np.amin(arr))
a = np.array([20,20,20]) print(a) print(np.percentile(a,10,0))
Assignements of Numpy Statistical functions:-
ASSIGNMENTS:-
create repository using excel file that contain rno, name, branch, semester, total marks gender ................................................................ 1) Display Record of max obtained marks student, Min obtained marks 2) Display agreegate perecentage of male, female and all 3) Display agreegate percentage of branch wise and all 4) Display agreegate percetage using branch and semester 5) max marks for each branch and semester for male , female and all
........................................................................
Numpy dot product in python:-
It is used to cross multiply matrix row elements, it will perform multiplication similar to matmul() for one dimension and two dimensions and above 2 dimensions it will modify the shape but matmul() will not modify the shape of the result.
For matmul:
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
For np.dot:
For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b
NumPy Matmul():- it is used to multiply matrix elements with row element to column elements.
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[3,4],[5,6]])
z= x.dot(y)
print(z)
#h = [[1,2],[3,4]]
#i = [[5,6],[7,8]]
z = np.matmul(x,y)
print(z)
x = np.array([1,2,3]) y = np.array([4,5,6]) z = np.dot(x,y) print(z) z = np.matmul(x,y) print(z)
x = np.array([[[1]]]) y = np.array([[[4]]]) z = np.dot(x,y) print(type(z),z.shape) z = np.matmul(x,y) print(type(z),z.shape)
Determinant
Last but not least, if you need to compute the determinant, you can use np.linalg.det(). Note that numpy takes care of the dimension.## Determinant 2*2 matrix ###
i = [[5,6],[7,8]]
5*8-7*6
np.linalg.det(i)
np.linalg.matrix_rank(A)):- It return number of columns in matrix
np.trace(A)) :- It return sum of main diagonal of matrix
print(np.linalg.det(A)) :- It return determinant of matrix
print(np.linalg.inv(A)) :- It provide inverse of matrix
print( np.linalg.matrix_power(A, 3)) :- it will multiple the matrix three times
M = np.array([[2, 1], [1, 2]]) result = np.linalg.matrix_power(M, 3)
:
:
Example of Determinant Formula:-
import numpy as np
0 print(np.linalg.matrix_rank(A)) print(np.trace(A)) print(np.linalg.det(A)) print(np.linalg.inv(A)) print( np.linalg.matrix_power(A, 3))
Numpy Eigen Function Example:-
This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.
Tutorials for Eigen Value and Eigen Vector you can refer this site:-
https://www.mathsisfun.com/algebra/eigenvalue.html
c, d = scs.eigh(a)
print("Eigen value is :", c)
print("Eigen vector is :", d)
numpy.linalg.eig(a):
eigh():- Returns two objects, a 1-D array containing the eigenvalues of a matrix, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).
from numpy import linalg as scsc, d = scs.eigh(a)
print("Eigen value is :", c)
print("Eigen vector is :", d)
This function is used to compute the eigenvalues and right eigenvectors of a square array.
import numpy as np
from numpy import linalg as scs
a = np.diag((1, 2, 3))
print("Array is :",a)
# calculating an eigenvalue
# using eig() function
c, d = scs.eig(a)
print("Eigen value is :",c)
print("Eigen vector is :",d)
Numpy DateTime Function:-
import numpy as np
from numpy import linalg as scs
a = np.diag((1, 2, 3))
print("Array is :",a)
# calculating an eigenvalue
# using eig() function
c, d = scs.eig(a)
print("Eigen value is :",c)
print("Eigen vector is :",d)
...........................................................................
How to get the difference between two dates using NumPy?
import numpy as np
import datetime
from datetime import date
curdate = np.datetime64(datetime.datetime.now())
print(curdate)
dob = np.datetime64('2017-02-12')
ms=curdate-dob
print(np.timedelta64(ms,'D'))
Get Year:-
import numpy as np
import datetime
from datetime import date
curdate = np.datetime64(datetime.datetime.now())
print(curdate)
dob = np.datetime64('2017-02-12')
ms=curdate-dob
dd=str(np.timedelta64(ms,'D'))
print(dd)
y = list(dd.split(' '))
print(int(y[0])//365)
How to get the difference between two dates using Python?
today = datetime.date.today()
f_date = date(2014, 7, 2)
l_date = date(2021, 1, 1)
delta = today - l_date
print(delta.days)
How to work with Images on Numpy Array
pip install OpenCV-python
1) Using np.full() method :
it is the simplest approach to generate an image.
import numpy as np
import cv2
array_created = np.full((500, 500, 3),198, dtype = np.uint8)
# displaying the image
cv2.imshow("image", array_created)
cv2.waitKey()
Method 2: By creating an array using np.zeroes() :
Example 1st:-
import numpy as np
import cv2
# creating array using np.zeroes()
array = np.zeros([500, 500, 3],
dtype = np.uint8)
#print(array)
# setting RGB color values as 255,255,255
array[:, :] = [245, 45, 5]
print(array)
# displaying the image
cv2.imshow("image", array)
cv2.waitKey()
Example 2nd:-
import numpy as np
import numpy
import cv2
img = numpy.zeros([5,5,3])
img[:,:,0] = numpy.ones([5,5])*64/255.0
img[:,:,1] = numpy.ones([5,5])*128/255.0
img[:,:,2] = numpy.ones([5,5])*192/255.0
cv2.imwrite('color_img.jpg', img)
cv2.imshow("image", img)
cv2.waitKey()
How to Convert images to NumPy array?
Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format.
Modules Needed:
NumPy: By default in higher versions of Python like 3.x onwards,
NumPy is available and if not available(in lower versions),
one can install by using
pip install numpy
Pillow: This has to be explicitly installed in later versions too.
It is a preferred image manipulation tool. In Python 3,
Pillow python library which is nothing but the upgradation of PIL only.
It can be installed using
pip install Pillow
One can easily check the version of installed Pillow by using the below code
import PIL
print('Installed Pillow Version:', PIL.__version__)
Output:
Installed Pillow Version: 7.2.0
Loading the images via Pillow Library
Let us check for an image that is in the PNG or JPEG format.
The image can be referred via its path.
Image class is the heart of PIL. It has open() function which opens up
an image and digital file format can be retrieved as well as pixel format.
from PIL import Image
# sample.png is the name of the image
# file and assuming that it is uploaded
# in the current directory or we need
# to give the path
image = Image.open('Sample.png')
# summarize some details about the image
print(image.format)
print(image.size)
print(image.mode)
Converting an image into NumPy Array
Python provides many modules and API for converting an image into a NumPy
array. Let’s discuss a few of them in detail.
Using NumPy module
The Numpy module in itself provides various methods to do the same.
These methods are –
Method 1: Using asarray() function
asarray() function is used to convert PIL images into NumPy arrays.
This function converts the input to an array
# Import the necessary libraries
from PIL import Image
from numpy import asarray
# load the image and convert into
# numpy array
img = Image.open('Sample.png')
# asarray() class is used to convert
# PIL images into NumPy arrays
numpydata = asarray(img)
# <class 'numpy.ndarray'>
print(type(numpydata))
# shape
print(numpydata.shape)
Output :
<class 'numpy.ndarray'>
(200, 400, 3)
Method 2: Using numpy.array() function
By using numpy.array() function which takes an image as the argument and
converts to NumPy array
from PIL import Image
import numpy
img= Image.open("Sample.png")
np_img = numpy.array(img)
print(np_img.shape)
Output :
(200, 400, 3)
In order to get the value of each pixel of the NumPy array image,
we need to print the retrieved data that got either from asarray() function
or array() function.
# Import the necessary libraries
from PIL import Image
from numpy import asarray
# load the image and convert into
# numpy array
img = Image.open('Sample.png')
numpydata = asarray(img)
# data
print(numpydata)
Another Example of cv2 to show images on the different color combinations:-
import cv2
import numpy as np
img=cv2.imread("D:\\a.jpg")
cv2.imshow("output",img)
cv2.waitKey(0)
B,G,R=cv2.split(img)
zeros=np.zeros(img.shape[:2],dtype="uint8")
cv2.imshow("Red",cv2.merge([zeros,zeros,R]))
cv2.imshow("Green",cv2.merge([zeros,G,zeros]))
cv2.imshow("Blue",cv2.merge([B,zeros,zeros]))
cv2.waitKey(0)
ASSIGNMENTS:-
CONVERT JSON FILE TO NUMPY ARRAY?
CONVERT REMOTE JSON https://shivaconceptsolution.com/webservices/showreg.php
to NUMPY ARRAY?
CONVERT CSV TO ARRAY?
Compare two images that are equal or not?
Solution of This Assignment:-
import cv2
import numpy as np
img=cv2.imread('D:\\a.jpg')
numpydata =np.array(img)
img1=cv2.imread('D:\\b.jpg') #7997
numpydata1=np.array(img1)
if np.all(numpydata==numpydata1):
print("same image")
else:
print("not same")
cv2.imshow("open",img)
cv2.waitKey(0)
cv2.imshow("open1",img1)
cv2.waitKey(0)
Another Example:-
import cv2
img=cv2.imread("d:\\a.jpg")
img_hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv",img_hsv)
cv2.imshow("hue:-",img_hsv[:,:,0])
cv2.imshow("saturation:-",img_hsv[:,:,1])
cv2.imshow("value:-",img_hsv[:,:,2])
cv2.waitKey(0)
Another Example of PIL
import PIL
import numpy as np
img=Image.open('C:\\Users\DELL\\Pictures\\cdgi\\FB_IMG_1475301504207.jpg')
numpydata =np.array(img)
img1=Image.open('C:\\Users\DELL\\Pictures\\cdgi\\FB_IMG_1475301507997.jpg')
numpydata1=np.array(img1)
if np.all(numpydata==numpydata1):
print("same image")
else:
print("not same")
import numpy as np
img=Image.open('C:\\Users\DELL\\Pictures\\cdgi\\FB_IMG_1475301504207.jpg')
numpydata =np.array(img)
img1=Image.open('C:\\Users\DELL\\Pictures\\cdgi\\FB_IMG_1475301507997.jpg')
numpydata1=np.array(img1)
if np.all(numpydata==numpydata1):
print("same image")
else:
print("not same")
If you want to do more practice on Image Processing then
you can visit cv2 site.
https://www.analyticsvidhya.com/blog/2021/05
/image-processing-using-opencv-with-practical-examples/
How to read text from Image File Example?
pip install opencv -python
pip install pytesseract
https://osdn.net/projects/sfnet_tesseract-ocr-alt/downloads/
tesseract-ocr-setup-3.02.02.exe/
import cv2
import pytesseract
# Mention the installed location of Tesseract-OCR in your system
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\
Tesseract-OCR\\tesseract.exe'
# Read image from which text needs to be extracted
img = cv2.imread("d://sample4.jpg")
# Preprocessing the image starts
# Convert the image to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Performing OTSU threshold
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
# Specify structure shape and kernel size.
# Kernel size increases or decreases the area
# of the rectangle to be detected.
# A smaller value like (10, 10) will detect
# each word instead of a sentence.
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
# Appplying dilation on the threshold image
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)
# Finding contours
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
# Creating a copy of image
im2 = img.copy()
# A text file is created and flushed
file = open("recognized.txt", "w+")
file.write("")
file.close()
# Looping through the identified contours
# Then rectangular part is cropped and passed on
# to pytesseract for extracting text from it
# Extracted text is then written into the text file
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# Drawing a rectangle on copied image
rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Cropping the text block for giving input to OCR
cropped = im2[y:y + h, x:x + w]
# Open the file in append mode
file = open("d:\\recognized.txt", "a")
# Apply OCR on the cropped image
text = pytesseract.image_to_string(cropped)
# Appending the text into file
file.write(text)
file.write("\n")
# Close the file
file.close
Maximum Size
Maximum size images, minium sizes
Image to text conversion and text to speech conversion in data science?
1) first install two different module
1.1) pip install pyttsx3
1.2) pip install gtts
1.3) download
https://osdn.net/projects/sfnet_tesseract-ocr-alt/
downloads/tesseract-ocr-setup-3.02.02.exe/
.exe file and install it and provide their path.
Complete code of this
import pytesseract
from PIL import Image
import os
from gtts import gTTS
import pyttsx3
img = Image.open('d://sample_image.png')
print(img)
pytesseract.pytesseract.tesseract_cmd ='C://Program Files (x86)
//Tesseract-OCR//tesseract.exe'
result = pytesseract.image_to_string(img)
with open('d://abc.txt',mode ='w+') as file:
file.write(result)
print(result)
engine = pyttsx3.init()
engine.say(result)
engine.runAndWait()
rate = engine.getProperty("rate")
print(rate)
engine.setProperty("rate", 300)
engine.say(result)
engine.runAndWait()
engine.setProperty("rate", 100)
engine.say(result)
engine.runAndWait()
ReplyDelete
For Web Scrapping Visit this site.
https://realpython.com/beautiful-soup-web-scraper-python/
Web scrapping means to get raw data from website using web url.
Example 1st:-
from urllib.request import urlopen
page = urlopen('http://olympus.realpython.org/profiles/aphrodite')
page
html_bytes = page.read()
html = html_bytes.decode("utf-8")
html
Example2:-
title_index = html.find("<title>")
start_index = title_index + len("<title>")
end_index = html.find("</title>")
title = html[start_index:end_index]
title
Get title using regular expression:-
import re
title_pattern = r'<title>(.*?)</title>'
title_match = re.search(title_pattern, html, re.IGNORECASE)
title_match.group(1)
Example4:-
pip install beautifulsoup4
Web Scarping using HTML Parser:-
# beauty_soup.py
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = "https://facebook.com/"
page = urlopen(url)
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
soup
print(soup.get_text())
Example 5:-
Submit Login form using Web Scraping:-
pip install mechanicalsoup
import mechanicalsoup
browser = mechanicalsoup.Browser()
url = "http://olympus.realpython.org/login"
login_page = browser.get(url)
login_html = login_page.soup
# 2
form = login_html.select("form")[0]
form.select("input")[0]["value"] = "zeus"
form.select("input")[1]["value"] = "ThunderDude"
# 3
profiles_page = browser.submit(form, login_page.url)
print(profiles_page.url)
NUMPY Predefine Array?
all()
any()
take()
put()
apply_along_axis()
apply_over_axes()
argmin()
argmax()
nanargmin()
nanargmax()
amax()
amin()
insert()
delete()
append()
around()
flip()
fliplr()
flipud()
triu()
tril()
tri()
empty()
empty_like()
zeros()
zeros_like()
ones()
ones_like()
full_like()
diag()
diagflat()
diag_indices()
asmatrix()
bmat()
eye()
roll()
identity()
arange()
place()
extract()
compress()
rot90()
tile()
reshape()
ravel()
isinf()
isrealobj()
isscalar()
isneginf()
isposinf()
iscomplex()
isnan()
iscomplexobj()
isreal()
isfinite()
isfortran()
exp()
exp2()
fix()
hypot()
absolute()
ceil()
floor()
degrees()
radians()
npv()
fv()
pv()
power()
float_power()
log()
log1()
log2()
log10()
dot()
vdot()
trunc()
divide()
floor_divide()
true_divide()
random.rand()
random.randn()
ndarray.flat()
expm1()
bincount()
rint()
equal()
not_equal()
less()
less_equal()
greater()
greater_equal()
prod()
square()
cbrt()
logical_or()
logical_and()
logical_not()
logical_xor()
array_equal()
array_equiv()
sin()
cos()
tan()
sinh()
cosh()
tanh()
arcsin()
arccos()
arctan()
arctan2()
Assignments of the Numpy Array?
MOST Important Array Interview Questions for Interview?
Level 1:-
Check if a key is present in every segment of size k in an array?
Find the minimum and maximum element in an array
Write a program to reverse the array
Write a program to sort the given array
Find the Kth largest and Kth smallest number in an array
Find the occurrence of an integer in the array
Sort the array of 0s, 1s, and 2s
Range and Coefficient of array
Move all the negative elements to one side of the array
Find the Union and Intersection of the two sorted arrays
Level 2
Write a program to cyclically rotate an array by one
Find the missing integer
Count Pairs with given sum
Find duplicates in an array
Sort an Array using the Quicksort algorithm
Find common elements in three sorted arrays
Find the first repeating element in an array of integers
Find the first non-repeating element in a given array of integers
Find the largest three elements in an array Time
Rearrange the array in alternating positive and negative items
Find if there is any subarray with sum equal to zero
Find Largest sum contiguous Subarray
Find the factorial of a large number
Find Maximum Product Subarray
Find longest consecutive subsequence
Find the minimum element in a rotated and sorted array
Find all elements that appear more than N/K times
GCD of given index ranges in an array
Minimize the maximum difference between the heights
Minimum number of jumps to reach the end
Find the two repetitive elements in a given array
Find a triplet that sums to a given value
Construct a N*M matrix from the user input
Find the row with the maximum number of 1’s
Print the matrix in a Spiral manner
Find whether an array is a subset of another array
Implement two Stacks in an array
Majority Element
Wave Array
Trapping Rainwater
Level 3
Maximum Index
Max sum path in two arrays
Find Missing And Repeating
Stock buy and sell Problem
Pair with given sum in a sorted array
Chocolate Distribution Problem
Longest Consecutive Subsequence
Print all possible combinations of r elements in a given array
How to Write data on xlrd?
import xlwt
EXCEL_FILES_FOLDER = 'd:/'
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Student')
excel_file_path = EXCEL_FILES_FOLDER+'write_excel.xls'
def addDataToSheet(worksheet):
# first row
worksheet.write(1, 0, "2001")
worksheet.write(1, 1, "James")
worksheet.write(1, 2, "Computer")
worksheet.write(1, 3, "A")
# Second row
worksheet.write(2, 0, "2002")
worksheet.write(2, 1, "Jhones")
worksheet.write(2, 2, "Electronics")
worksheet.write(2, 3, "A+")
# Third row
worksheet.write(3, 0, "2003")
worksheet.write(3, 1, "Micheal")
worksheet.write(3, 2, "Civil")
worksheet.write(3, 3, "C")
addDataToSheet(worksheet)
workbook.save(excel_file_path)
Read data from xlrd?
import xlrd
import xlwt
loc = ("d://Book1.xls")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.write(0,2,30)
wb.save()
# For row 0 and column 0
print(sheet.cell_value(0, 0))
print("Total no of rows is ",sheet.nrows)
print("Total no of columns is ",sheet.ncols)
Convert Excel data to Numpy Array and display in flatten pattern?
ReplyDeleteCreate 10 students percentage into two different excel sheet and merge them using hstack and vstack()
# DATA SCIENCE ( 6 to 7 PM BATCH)
ReplyDelete# CODE To Convert Excel data to Numpy Array and Display in Flatten Pattern.
#Stacking Along Rows(hstack())
#Stacking Along Columns(vstack())
import numpy as np
import openpyxl
from openpyxl import Workbook # Call a Workbook() function of openpyxl
wb = Workbook() # to create a new blank Workbook object
# Get workbook active sheet
# from the active attribute.
ws = wb.active
ws.title = "Student's Record" # One can change the name of the title
s = [['Student Name','Age','DOB',"Roll. No.","Maximum Age"],["ROHIT",40,"21-08-1980",123],["KUMAR",30,"15-09-1990",45]]
ar =[]#Empty List
for i in range(0,len(s)):
k=0
ar1=[]#Empty List
for j in range(1,len(s[1])+1):
c1 = ws.cell(row = i+1, column = j)
c1.value=s[i][k]# writing values to cells
ar1.append(c1.value)
k+=1
ar.append(ar1)
arr = np.concatenate((ar))# Joining Numpy Array using mconcatenate() function.
arr1 = np.hstack((ar))# Joining Numpy Array using hstack().
arr2 = np.vstack((ar))# Joining Numpy Array using vstack().
wb.save(filename = 'excel_array.xlsx')
print("Numpy Array :-")
print(arr,"\n","arr1\n",arr1,"\n","arr2\n",arr2)
It is without excel file
Delete# CODE To Convert Excel data to Numpy Array and Display in Flatten Pattern Using Concatenate() Function
ReplyDelete# Excel data to Numpy Array Using Stacking Along Columns
# Excel data to Numpy Array Using Stacking Along Rows
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('sample_book.xlsx')
ws = wb.active
ar =[]#Empty List
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
ar1.append(c1.value)
k+=1
ar.append(ar1)
arr = np.concatenate((ar))
arr1 = np.hstack((ar))# Joining Numpy Array using hstack().
arr2 = np.vstack((ar))# Joining Numpy Array using vstack().
print("Numpy Array :-")
print(arr,"\n","arr1\n",arr1,"\n","arr2\n",arr2)
# CODE To Merge two different excel sheet using hstack() and vstack()
ReplyDeleteimport openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('sample_book.xlsx')
wa = openpyxl.load_workbook('excel_array.xlsx')
ws = wb.active
we = wa.active
ar =[]#Empty List
ae = []
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
ae1 = []
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
c2 = we.cell(row = i+1, column = j)
ar1.append(c1.value)
ae1.append(c2.value)
k+=1
ar.append(ar1)
ae.append(ae1)
arr1 = np.hstack((ar,ae))# Joining Numpy Array using hstack().
arr2 = np.vstack((ar,ae))# Joining Numpy Array using vstack().
print("Numpy Array :-")
print("\n","arr1\n","\n",arr1,"\n","\n","arr2\n","\n",arr2)
# CODE To Merge two different excel sheet using hstack() and vstack()
ReplyDeleteimport openpyxl
import numpy as np
from openpyxl import load_workbook
wb = openpyxl.load_workbook('Record.xlsx')
wa = openpyxl.load_workbook('Book1.xlsx')
ws = wb.active# for workbook 1
we = wa.active# for workbook 2
ar =[]#Empty List
ae = []
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
ae1 = []
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
c2 = we.cell(row = i+1, column = j)
ar1.append(c1.value)
ae1.append(c2.value)
k+=1
ar.append(ar1)
ae.append(ae1)
arr1 = np.hstack((ar,ae))# Joining Numpy Array using hstack().
arr2 = np.vstack((ar,ae))# Joining Numpy Array using vstack().
print("Numpy Array :-")
print("\n","Arr1 Value\n","\n",arr1,"\n","\n","Arr2 Value\n","\n",arr2)
# CODE To Convert Excel data to Numpy Array and Display in Flatten Pattern Using Concatenate() Function
ReplyDeleteimport openpyxl
import numpy as np
from openpyxl import load_workbook
wb = openpyxl.load_workbook('Record.xlsx')
ws = wb.active
ar =[]#Empty List
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
ar1.append(c1.value)
k+=1
ar.append(ar1)
arr = np.concatenate((ar))
arr1 = np.hstack((ar))# Joining Numpy Array using hstack().
arr2 = np.vstack((ar))# Joining Numpy Array using vstack().
print("Numpy Array :-")
print(arr,"\n","arr1\n",arr1,"\n","arr2\n",arr2)
# Excel data to Numpy Array Using Stacking Along Columns
# Excel data to Numpy Array Using Stacking Along Rows
It throws error
Deletebecause concatenate function takes two array (or more than two) as argument and axis.
ReplyDelete# Data Science (6 to 7 PM BATCH)
# CONVERT REMOTE JSON https://shivaconceptsolution.com/webservices/showreg.php to NUMPY ARRAY.
import json
import urllib
import numpy as np
url = "https://shivaconceptsolution.com/webservices/showreg.php"
# open a connection to a URL using urllib
json_url = urllib.request.urlopen(url)
# parse json object
data = json.loads(json_url.read())
# here we create new data_file.json file with write mode using file i/o operation
with open("Sample1.txt", "w") as p:
# write json data into file
json.dump(data, p)
# Opening JSON file with read mode.
with open("Sample1.txt", "r") as p:
# Reading from file and returns JSON object as a dictionary
data = json.load(p)
# Closing file
p.close()
print(type(data))
# Pretty Printing JSON string back
#print(json.dumps(data,indent=4,sort_keys=True))
for p_id, p_info in data.items():
print( p_id,"\n")
for key in p_info:
# to return a group of the key-value
# pairs in the dictionary
result = key.items()
# Convert object to a list
data = list(result)
# Convert list to an array
numpyArray = np.array(data)
# print the numpy array
print(numpyArray)
print(len(numpyArray))
print(type(numpyArray))
# Data Science (6 to 7 PM BATCH)
ReplyDelete# Program to CONVERT CSV TO ARRAY.
import csv #Import the csv library.
import numpy as np
# With the file open, create a new csv.reader object.
with open('data3.csv', 'r') as f:
#Pass in the keyword argument delimiter=";" to make sure that the records are
# split up on the semicolon character instead of the default comma character.
wines = list(csv.reader(f, delimiter=';'))
## Here We get a LIST (wines)
print("\n\n",type(wines),"\n\n",wines)
wines1 = np.array(wines)
# List Slicing.(wines[1:])
# Specify the keyword argument dtype( dtype=np.str) to make sure each element is converted to a String.
wines = np.array(wines[1:], dtype=np.str)
# we’ll now get a NumPy array
print()
print(type(wines),"\nNumPy array\n",wines,"\n\n",type(wines1),"\nNumPy array 1\n",wines1)
#Convert CSV to Numpy Array
ReplyDeleteimport csv
results = []
with open("data3.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats
for row in reader: # each row is a list
results.append(row)
print(results)
# Basic Methods And Function of numpy
ReplyDeleteimport numpy as np
a=np.array([1,2,3])
print(a[1])
print(a.ndim) #it will print the type of array here one dimensional array.
a=np.array([[1,2],[2,3],[3,4]])
print(a.ndim) # it will print the type of array here two dimensional array.
print(a.itemsize) # it will print the size of an datatype means if int in python the it will print 4
print(a.dtype) # it will print the type of datatype
print("Min: ",a.min())# it will print the minimum element of array
print("Max: ",a.max())# it will print the maximum element of array
a=np.array([[1,2],[7,3],[5,4]],dtype=np.float64)
print(a.dtype)
print(a.size)# it will print the total elements in array
print(a.itemsize)# output 8 because float64 contain 8 bits
print(a.shape) # it will print the dimensions of array (rows=3,column=2)
print(a.reshape(2,3))
print(a.ravel())# it will print the array in flatten format (straight row) convert into one dimension
a=np.array([[1,2],[2,3],[3,4]],dtype=complex)# it will print the array into complex numbers
print(a)
print(np.zeros((3,4)))# it will create an array of zeros of dimensional 3 rows and 4 column
print(np.ones((3,4)))
l=range(5)# it will create a list (of numbers 0 to 4)for Example 0,1,2,3,4
print(l)
print(l[0])
print(l[1])
# OR similar to numpy array using arange function
print(np.arange(1,5))# it will create an array of range 1 to 4 elements
print(np.arange(1,5,2))# it will print 1,3 because 2 is the steps taken to jump
print(np.linspace(1,5,10))# 10 is the number of total counts betwoon 1 to 5
print(np.linspace(1,5,5))
a=np.array([[1,2],[3,4],[5,6]])
print("Sum: ",a.sum())# it will print the sum of array elements.
print(a.sum(axis=0))# here Axis=0 means coloumn sum
print(a.sum(axis=1))# here Axis=0 means Rows sum
print("\n Square Root of Elements:\n",np.sqrt(a))# here square root of the numbers.
print("Standard Devatation of array of whole arrar elements: ",np.std(a))
a=np.array([[1,2],[3,4]])
b=np.array([[5,6],[7,8]])
print("Addition: \n",a+b)# it will print the addition of both the array all mathematical operation (+,-,*,/)
print("Matrix Product: \n",a.dot(b))# it will print the matrix product of this two indidual array elements
print(np.arange(1,5,2))# it will print 1,3 because 2 is the steps taken to jump
#Slicing in array
ReplyDeleteimport numpy as np
n=[6,7,8]# list slicing
print(n[0:2])# it will print 6,7 because 0 is the starting position and 2 is number of elements(0+1)
#no of element is 2 so 6,7 return
print(n[-1])# it will print the last element
a=np.array([6,7,8])# numpy array slicing
print(a[0:2])# print same in numpy array
print(a[-1])# print 8
a=np.array([[6,7,8],[1,2,3],[9,3,2]]) # multidimensional array
print(a[1,2])# it will print 3 because 1 is a row and 2 is the coulmn
print(a[0:2,2])# it will print 8 and 3 because 0 is not included
print(a[-1])# print last array
print(a[-1,0:2])
print(a[:,1:3])# because (:) is for all the rows the and we want to print only 2 and 3 coulmn so 1: 3 means 0,1,2 totals
#total is 3 coulmn so 1:3 is for 1 st column and 3 rd column
a=np.array([[6,7,8],[1,2,3],[9,3,2]]) # multidimensional array
for row in a:
print(row)
print(type(row)) # this is also ndarray
for cell in a.flat:
print(cell)# it will print in flatten form
# STACKING
ReplyDelete# arange is used to create a dynamic array with range 6(0,1,2,3,4,5) and
# reshape is a method to print in dimension3 rows and 2 column
a=np.arange(6).reshape(3,2)
b=np.arange(6,12).reshape(3,2)
print("\n",a)
print("\n",b)
print("\n",np.vstack((a,b)))# print in vertical format and combine both the arrays
print("\n",np.hstack((a,b)))# print in horizontal format
a=np.arange(30).reshape(2,15)
print(a)
print("\n Split array in three equal format:",np.hsplit(a,3))# here 3 is the number of peice you have to cut this original array (a)
# this is same for (VERTICAL SPLIT) also np.vsplit(a,2)
res=np.hsplit(a,3)
print("\n 1st array:\n",res[0])
print("\n 2nd array:\n",res[1])
print("\n 3rd array:\n",res[2])
# Boolean Arrays
ReplyDeletea=np.arange(12).reshape(3,4)
print(a)
b = a>4
print(b)
print(type(b))
print(a[b])# in this whenever b found True print the original value into the a array
# a[b]=-1 # if b found true they replace the value if true with -1
# Iterating array
ReplyDeletea=np.arange(12).reshape(3,4)
print(a)
'''
for row in a:
for cell in row:# used to flatten the array
print(cell)
'''
# OR
'''
for cell in a.flatten():
print(cell)
'''
# OR
for cell in np.nditer(a,order='C'):# it will print same as flatten id you use 'C'.
print(cell)
'''
The Order 'C' means rows wise printing means 0,1,2,3,4,5,6,7....and so on...
and
the Order 'F' means forton column wise printing means 0,4,8,1,5,9,2,6,10.....so on..
'''
for x in np.nditer(a,order='F',flags=['external_loop']): # print each column seprate of Forton Order
print("\nSeprate each Column \n",x)
# Modify Elements of Array
ReplyDeletea=np.arange(12).reshape(3,4)
print(a)
for x in np.nditer(a,op_flags=['readwrite']):
x[...]=x*x # means that square each elements of array
print(a)
# for additing both these array into one .
b=np.arange(3,15,4).reshape(3,1)
print(b)
print("\nCombine Both The arrya")
for x,y in np.nditer([a,b]):
print(x,y)
#Statitics Operation
ReplyDelete#Reading Data From The Excel File
#Two worksheet
#worksheet= A
#worksheet= B
import openpyxl
import numpy as np
l1 = 0
l2 = 1
wb = openpyxl.load_workbook('Record.xlsx')
sheets = wb.sheetnames
ws = wb[sheets[l1]]
print(ws)
ws1 = wb[sheets[l2]]
print(ws1)
print("\n\nSheet 1 Data \n")
ar =[]#Empty List
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
ar1.append(c1.value)
k+=1
ar.append(ar1)
arr = np.concatenate((ar))
print(arr)
print("\n")
print("Sheet 2 Data \n")
a =[]#Empty List
col1 = ws1.max_column
ro1 = ws1.max_row
for k in range(1,(ro1+1)):
k1=0
ar2=[]#Empty List
for p in range(1,(col1+1)):
c12 = ws1.cell(row = k, column = p)
ar2.append(c12.value)
k1+=1
a.append(ar2)
arrr = np.concatenate((a))
print(arrr)
#Basic statistics Operation
import statistics as st
wb = openpyxl.load_workbook('Record.xlsx', data_only=True)
ws = wb.active
list1 = []
ro = ws.max_row
for k in range(len(wb.sheetnames)):
wb.active = k
ws = wb.active
for i in range(0,ro-1):
val = ws.cell(row = 2+i, column = 5)
list1.append(val.value)
print("Number of Total values: {0}".format(len(list1)))
print("Sum of Total values: {0}".format(sum(list1)))
print("Minimum value in the table: {0}".format(min(list1)))
print("Maximum value in the table: {0}".format(max(list1)))
print("Mean: {0}".format(st.mean(list1)))
print("Median: {0}".format(st.median(list1)))
print("Standard deviation: {0}".format(st.stdev(list1)))
import numpy as np
ReplyDeletedef max (ar):
max=0
for i in ar:
if max<i:
max=i
return max
ar=np.array([10,96,30,40])
s=max(ar)
print(s)
import numpy as np
ReplyDeletenum=np.array([1,2,3,5,4])
a=2
count=0
for i in num:
while a<i:
if i%a==0:
count=count+1
break
a=a+1
if count==0:
print("prime:",i)
else:
print("not prime:",i)
#Find the minimum and maximum element in an array
ReplyDeletel=[]
n=int(input("enter the length of array:="))
for i in range(0,n):
num=int(input("enter the elements:-"))
l.append(num)
arr=np.array(l)
m=arr[0]
mi=arr[0]
for i in range(0,n):
if arr[i]>m:
m=arr[i]
for i in range(0,n):
if arr[i]<mi:
mi=arr[i]
print("max is:-",m,"min is:-",mi)
#Write a program to reverse the array
ReplyDeletearr=np.array([1,2,3,6,59,8,2,3,6])
for i in range(len(arr)-1,-1,-1):
print(arr[i])
type(arr)
#Write a program to reverse the array
ReplyDeletearr=np.array([1,2,3,6,59,8,2,3,6])
for i in range(len(arr)-1,-1,-1):
print(arr[i])
type(arr)
#Write a program to sort the given array
ReplyDeletearr=np.array([1,9,6,3,85,96,32,321])
for i in range(0,len(arr)):
for j in range(0,len(arr)):
if arr[i]<arr[j]:
arr[i],arr[j]=arr[j],arr[i]
arr
#Find the Kth largest and Kth smallest number in an array
ReplyDeletearr=np.array([1,9,6,3,7,5,3,4,12,36,98,74,52,20,30,98])
for i in range(0,len(arr)):
for j in range(0,len(arr)):
if arr[i]<arr[j]:
arr[i],arr[j]=arr[j],arr[i]
small=int(input("smallest number"))
print(arr[small-1])
large=int(input("largest number"))
print(arr[-large])
#Find the occurrence of an integer in the array
ReplyDeletel=[]
num=int(input("enter number:-"))
while num>0:
a=num%10
l.append(a)
arr=np.array(l)
num=num//10
for i in range(0,10):
count=0
for j in range(0,len(arr)):
if i==arr[j]:
count+=1
print("number ",i," repeated ",count)
#Sort the array of 0s, 1s, and 2s
ReplyDeletearr=np.array([1,0,2,0,1,0,2,0,1,2,2,2,0,1,0,2,0,1,0,2])
low=0
mid=0
high=len(arr)-1
while mid<=high:
if arr[mid]==0:
arr[low],arr[mid]=arr[mid],arr[low]
mid+=1
low+=1
elif arr[mid]==1:
mid+=1
else:
arr[high],arr[mid]=arr[mid],arr[high]
high-=1
arr
#Range and Coefficient of array
ReplyDeletearr=np.array([5,10,9,6,8,12,15])
mx=arr[0]
mn=arr[0]
for i in range(0,len(arr)):
if arr[i]>mx:
mx=arr[i]
if arr[i]<mn:
mn=arr[i]
r=mx-mn
print("range of array is:-",r)
r1=mx+mn
c=r/r1
print("coefficient of array is:-",c)
#Move all the negative elements to one side of the array
ReplyDeletearr=np.array([9,6,3,-8,-1,-7,-6,-4,10])
j=0
for i in range(0,len(arr)):
if arr[i]<0:
arr[i],arr[j]=arr[j],arr[i]
j+=1
arr
#Find the Union and Intersection of the two sorted arrays
ReplyDeletearr1=np.array([1,3,4,6,8,9])
arr2=np.array([4,6,7,9,10,15])
size=len(arr1)+len(arr2)
arr3=np.array([int]*size)
s=[]
for i in range(0,size):
if i<len(arr1):
arr3[i]=arr1[i]
else:
arr3[i]=arr2[i-len(arr1)]
a=set(arr3)
m=np.array([a])
print("union is:-",m)
for i in range(0,len(arr1)):
for j in range(0,len(arr2)):
if arr1[i]==arr2[j]:
s.append(arr1[i])
print("intersection is:-",np.array(s))
#Write a program to cyclically rotate an array by one
ReplyDeletearr=np.array([1,5,9,3,5,9,6,2,6,8,4,14,5])
j=0
for i in range(0,len(arr)):
arr[i],arr[j]=arr[j],arr[i]
arr
#Find the missing integer
ReplyDeletearr=np.array([1,9,6,3,7,15,5])
mx=arr[0]
mn=arr[0]
for i in range(0,len(arr)):
if arr[i]>mx:
mx=arr[i]
if arr[i]<mn:
mn=arr[i]
for i in range(mn,mx):
if i not in arr:
print(i)
#Count Pairs with given sum
ReplyDeletearr=np.array([1, 5, 7, -1, 5])
sum=6
count=0
for i in range(0,len(arr)):
for j in range(i+1,len(arr)):
if arr[i]+arr[j]==sum:
count+=1
print(count)
#Find duplicates in an array
ReplyDeletearr=np.array([1,1,2,2,5,9,6,6,3,4,7,8,5,-1,-1,-9876,-9876])
for i in range(0,len(arr)):
for j in range(i+1,len(arr)):
if arr[i]==arr[j]:
print(arr[i])
#find max element in numpy array
ReplyDeleteimport numpy as np
m=0
ar=np.array([5,6,4,81,111,7])
for i in ar:
if m<i:
m=i
print("max element is:-",m)
#WAP to sort the elements of the NumPy array?
ReplyDeleteimport numpy as np
ar=np.array([5,9,6,2,8,95,62,32,45])
for i in range(0,len(ar)):
for j in range(i+1,len(ar)):
if ar[i]>ar[j]:
ar[i],ar[j]=ar[j],ar[i]
print(ar)
#WAP to split one array into two different subarrays?
ReplyDeleteimport numpy as np
ar=np.array([1,9,6,5,8,4,6,3,2,8,5,7,4])
a=ar[0:int(len(ar)/2)]
b=ar[int(len(ar)/2):len(ar)]
print(a)
print(b)
#WAP to merge two NumPy arrays into one array
ReplyDeleteimport numpy as np
ar1=np.array([1,7,3,9,3,5,6,7])
ar2=np.array([4,65,4,32,5,6,7])
ar3=np.concatenate((ar1,ar2))
print(ar3)
#WAP to display prime elements in Numpy array
ReplyDeleteimport numpy as np
ar=np.array([4,9,6,3,7,8,5,1,2])
for i in range(0,len(ar)):
if ar[i]>=2:
for j in range(2,ar[i]):
if (ar[i]%j)==0:
break
else:
print(ar[i])
import json
ReplyDeletej = '[{"a":"1", "b":"2", "c":"3", "d":"4"}]'
a = json.loads(j)
for i in range(len(a)):
for j in a[i]:
print(j,":",a[i][j])
import csv
ReplyDeletef= open("P:\\word\\covid_19_india.csv")
c =csv.reader(f)
c1 = 0
for row in c:
print(row)
c1+=1
print("line count",c1)
import docx
ReplyDeletef = docx.Document("P://word//word1.docx")
c=f.paragraphs
for i in c:
print(i.text)
#access word file
ReplyDeleteimport docx
f = docx.Document("P://word//word1.docx")
c=f.paragraphs
for i in c:
print(i.text)
This site is so amazing, This sites gives good knowledge of Data science , This is very helpful for me. Here all content so useful and helpful for beginner and experience both.
ReplyDeleteThis site is so amazing, This sites gives good knowledge of Data science , This is very helpful for me. Here all content so useful and helpful for beginner and experience both.
ReplyDeleteimport numpy as np
ReplyDeletefrom openpyxl import load_workbook
wb = load_workbook("C:\\Users\\91860\\Desktop\\numpy.xlsx")
ws = wb.active
ar =[]
col = ws.max_column
ro = ws.max_row
for i in range(1,(ro+1)):
k=0
ar1=[]#Empty List
for j in range(1,(col+1)):
c1 = ws.cell(row = i, column = j)
ar1.append(c1.value)
k+=1
ar.append(ar1)
print(ar)
tm=0
malemarks=[]
femalemarks=[]
for i in range(2,len(ar)):
if(ar[i][5]=='M'):
malemarks.append(ar[i][4])
else:
femalemarks.append(ar[i][4])
tm += ar[i][4]
print(malemarks)
print(femalemarks)
print(tm)
print("******")
print("Display Record of max obtained marks student, Min obtained marks")
marks=[]
for i in range (2,len(ar)):
if (ar[i][4]):
marks.append(ar[i][4])
print(marks)
print(max(marks))
print(min(marks))
print("******")
print("Display agreegate perecentage of male, female and all")
totalmale=0
for i in malemarks:
totalmale+=i
print(totalmale)
avg_male=totalmale/3
per_male=totalmale/15
print(avg_male)
print(per_male)
print("\n")
totalfemale=0
for i in femalemarks:
totalfemale+=i
print(totalfemale)
avg_female=totalfemale/2
per_female=totalfemale/10
print(avg_female)
print(per_female)
print("\n")
totalmarks=0
for i in marks:
totalmarks+=i
print(totalmarks)
avg_marks=totalmarks/5
per_marks=totalmarks/25
print(avg_marks)
print(per_marks)
print("\n")
print("******")
print('Display agreegate percentage of branch wise and all')
tm=0
CS=[]
IT=[]
for i in range(2,len(ar)):
if(ar[i][2]=='CS'):
CS.append(ar[i][4])
if(ar[i][2]=='IT'):
IT.append(ar[i][4])
tm += ar[i][4]
print(tm)
print(CS)
print(IT)
print("\n")
cs=0
for i in CS:
cs+=i
print(cs)
avg_marks=cs/3
per_marks=cs/15
print(avg_marks)
print(per_marks)
print("\n")
it=0
for i in IT:
it+=i
print(it)
avg_marks=it/2
per_marks=it/10
print(avg_marks)
print(per_marks)
print("\n")
print("******")
print("Display agreegate percetage using branch and semester")
CS=[]
IT=[]
for i in range(2,len(ar)):
if((ar[i][2]=='CS') and ((ar[i][3]=='1st') or (ar[i][3]=='2nd'))):
CS.append(ar[i][4])
if((ar[i][2]=='IT') and ((ar[i][3]=='1st')or (ar[i][3]=='2nd'))):
IT.append(ar[i][4])
print(CS)
print(IT)
print("\n")
cs=0
for i in CS:
cs+=i
avg_cs=cs/3
per_cs=cs/15
print(avg_cs,per_cs)
print("\n")
it=0
for i in IT:
it+=i
avg_it=it/2
per_it=it/10
print(avg_it,per_it)
print("\n")
print("max marks for each branch and semester for male , female and all")
marks=[]
for i in range (2,len(ar)):
if (ar[i][4]):
marks.append(ar[i][4])
print(marks)
print(max(marks))
print("\n")
malemarks=[]
femalemarks=[]
for i in range(2,len(ar)):
if((ar[i][5]=='M') and ((ar[i][2]=='CS')or (ar[i][2]=='IT')) and ((ar[i][3]=='1st') or (ar[i][3]=='2nd'))):
malemarks.append(ar[i][4])
else:
femalemarks.append(ar[i][4])
tm += ar[i][4]
print(malemarks)
print(femalemarks)
print(max(malemarks))
print(max(femalemarks))
#WAP to display prime elements in Numpy array
ReplyDeleteimport numpy as np
x=np.array([1,2,3,4,5,6,7,8])
for i in range(0,len(x)):
if x[i]>=2:
for j in range(2,x[i]):
if (x[i]%j)==0:
break
else:
print(x[i])
#WAP to calculate the sum and multiply two different matrices using NumPy array?
ReplyDeleteimport numpy as np
x=([1,2,3,4],[6,7,8,9])
y=([4,5,6,7],[9,7,5,8])
result=np.array(x)+np.array(y)
print("Addition of Matrices are:",result)
#Now For multiplication
multiplyres=np.array(x)*np.array(y)
print("Multiplication of matrices are:",multiplyres)
#sum of row elements in an matrix using numpy array.
ReplyDeletex=[1,2,3,4,5,6],[9,8,7,6,6,9],[1,2,33,45,67,89]
#with using sum Function:
result=np.sum(x)
print("The Sum of Elements In the Matrices Are:",result)
#Without using sum Function
def array_sum(arr):#Making a Function for sum of elements without using sun() function.
total=0
for num in arr:
total += num
return total
y=[1,2,3,5,6,7]
print(array_sum(y))
#WAP to sort the elements of the NumPy array?.
ReplyDeleteimport numpy as np
x=[10,20,50,100,150,200,120,350]
print("Orginal",x)
for i in range(0,len(x)):
for j in range(i+1, len(x)):
if x[i] >= x[j]:
#creating a Temp Variable.
temp=x[i]
x[i]=x[j]
x[j]=temp
#Now using Sort Function.
print(x)
print("The Sorted Elements Are:",np.sort(x))
#comparing two images using numpy array.
ReplyDeleteimport cv2
import numpy as np
img=cv2.imread("D:\\a.jpg")
numpydata=np.array(img) #converting image into numpy array.
img2=cv2.imread("D:\\b.jpg") #taking 2nd image to compare.
numpydata1=np.array(img2) #converting image to Numpy Array.
if np.all(numpydata==numpydata1):
print("Both are same")
else:
print("The Images Are Different")
cv2.imshow("open",img)
cv2.waitKey(0)
cv2.imshow("open1",img2)
cv2.waitKey(0)
#converting image to Numpy Array.
ReplyDeleteimport cv2
import numpy as np
img=cv2.imread("D:\\a.jpg")
numpydata=np.array(img) #using np.array
print(numpydata)
#json file to numpy array.
ReplyDeleteimport json
import numpy as np
#reading the JSON file.
with open('data.json','r') as file:
data=json.load(file)
#converting it into a numpy array.
numpydata=np.array(data)
print(numpydata)