Unit - V
Unit - V
• // Before reshape
• [[1 2 3]
• [4 5 6]]
• //After Reshape
• array([[1, 2],
• [3, 4],
Numpy – min, max, sum, sort, search
• import numpy
• arr = numpy.array([1, 5, 4, 8, 3, 7]) # creating a numpy array of integers
• big= numpy.max(arr) # finding the maximum and
• small = numpy.min(arr) # minimum element in the array
• print(np.sum(arr)) # sum of array elements
• print(np.sort(arr)) # sort the elemnts
• You can search an array for a certain value, and return the indexes that get a match.
• To search an array, use the where() method.
• import numpy as np
• arr = np.array([1, 2, 3, 4, 5, 4, 4])
• x = np.where(arr == 4)
• print(x) # (array([3, 5, 6]),)
Matplot Lib
• Matplotlib is a low level graph plotting library in python that
serves as a visualization utility.
• It is open-source library.
• pip install matplotlib # Installing
• Once Matplotlib is installed, import it in your applications by
adding the import module statement:
• import matplotlib
• print(matplotlib.__version__) # checking the version
Purpose of Matplot library
• Matplotlib is a library for creating static, animated, and interactive
visualizations in Python.
• Matplotlib can be used for publication quality plots.
• it can be used for creating interactive figures that can zoom, pan,
update.
• Pyplot
• Most of the Matplotlib utilities lies under the pyplot submodule, and are
usually imported under the plt alias:
• import matplotlib.pyplot as plt
• Example: Draw a line in a diagram from position (0,0) to position
(6,250):
• import matplotlib.pyplot as plt
• import numpy as np
• x = np.array([0, 6])
• y = np.array([0, 250])
• plt.plot(x, y)
• plt.show()
• Plotting x and y points – using plot() function
• By default, plot() function draws a line from point to point.
• The function takes 2 parameters.
• Example:
• If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8]
and [3, 10] to the plot function.
• Code:
• import matplotlib.pyplot as plt
• import numpy as np
• x = np.array([1, 8])
• y = np.array([3, 10])
• plt.plot(x, y) # plt.plot(x,
y,'o')
• plt.show()
Multiple Points
• Example
• Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to
position (8, 10):
• plt.plot(xpoints, ypoints)
• plt.show()
• Example: Matplotlib Pie Charts - pie() function
• import matplotlib.pyplot as plt
• import numpy as np
• y = np.array([35, 25, 25, 15])
• mylabels = ["GITAM", "VIT", "KL Univ", "SRM"]
• plt.pie(y, labels = mylabels)
• plt.show()
• Creating Bars -bar() function
• import sys
• import matplotlib
• import matplotlib.pyplot as plt
• import numpy as np
• x = np.array(["VIT-AP", "GITAM", "AU", "IITM"])
• y = np.array([10, 8, 6, 9])
• plt.bar(x,y)
• plt.show()
Matplotlib Subplot
• it is used to display Multiple Plots
• provides a way to plot multiple plots on a single figure
• subplot() function you can draw multiple plots in one figure
• Example: Draw 2 plots:
• import matplotlib.pyplot as plt
• import numpy as np
• #plot 1:
• x = np.array([0, 1, 2, 3])
• y = np.array([3, 8, 1, 10])
• plt.subplot(1, 2, 1)
• plt.plot(x,y)
• #plot 2:
• x = np.array([0, 1, 2, 3])
• y = np.array([10, 20, 30, 40])
• plt.subplot(1, 2, 2)
• plt.plot(x,y)
• plt.show()
Package
• We usually organize our files in different folders and subfolders based on
some criteria, so that they can be managed easily and efficiently. For
example, we keep all our games in a Games folder and we can even
subcategorize according to the genre of the game or something like this.
The same analogy is followed by the Python package.
• A Python module may contain several classes, functions, variables, etc.
whereas a Python package can contains several module. In simpler terms a
package is folder that contains various modules as files.
• Creating Package
• Let’s create a package named mypckg that will contain two modules mod1
and mod2. To create this module follow the below steps –
• Create a folder named mypckg.
• Inside this folder create an empty Python file i.e. __init__.py
• Then create two modules mod1 and mod2 in this folder.