A module is a file in Python which organizes code in a systematic manner in order to increase our understanding and accessibility of the code. This code mostly consists of definitions of various functions, classes and variables and sometimes, even runnable code. Python modules usually have a .py extension and contain code written in Python. Over the years, Python has had an increasing set of in-built modules and as of now, it has well over 200. The Python Standard Library contains built-in modules for us to use. We can even
install other modules using the pip command. A single program can have multiple import statements.
Necessity of modules in machine learning
The necessity of modules is derived from the idea of reuse of code. Modules save programmers a lot of time by allowing us to use pre-written code and help us save time. This also means that we can avoid writing bulky code and can concentrate on the main idea of the program. For example, in case of a system administrator, it is much easier to import the ‘os’ module instead of writing down code to rename a folder full of files.
The following is some code written for rename files in bulk using the ‘os’ module:
import os #importing the os module to use functions that interact with the operating system currentPath="C:/Users/Arya/Desktop/sample/" imgNo=0 for myFile in os.listdir(currentPath): #looping through each file os.rename((currentPath+myFile),(currentPath+"image"+ str(imgNo)+".jpg")) imgNo+=1
- import statement
Syntax: import [module]
In spite of there existing a plethora of modules, we can even use our own code as a module with the help of the import statement written in another Python source file. The Python interpreter uses the search path specified to go through the files and then find the
correct file to import the correct module. Even if we try to import the same module multiple times, it is loaded only once in order to prevent the same module from executing repeatedly. Import statements are usually written at the top of the file to make code look
cleaner. After importing a module, we are free to access its individual functions in our program.
- from import statement
Syntax: from [module] import [function or value] This statement is usually used when we want to import specific attributes and not the entire module. For example the following line of code is used to import the degrees method which is used to convert angles in radians to degrees: from math import degrees
- from import * statement
Syntax: from [module] import *
This statement imports all the names in a module to the current namespace. It should be used sparingly and only in cases when we aware of exactly what we require from the specified module. The following line of code gives us information about all the names
inside the math module:
commonly used built-in modules in Machine learning
The time module is used to perform time related tasks. For example, some of the functions it contains are as follows:
- time()-returns the number of seconds passed till epoch
- sleep()-delays the execution of the current thread for a specified number of seconds
The random module is used to generate pseudo-random numbers. For example, some of
the functions it contains are as follows:
- randint()-returns a random integer between two specified integers
- choice()-selects a random element from a sequence such as a list and raises an
IndexError in case of an empty sequence
The os module contains functions which help us interact with the operating system to perform various tasks. For example, some of the functions it contains are as follows:
- mkdir()-creates a directory named path according to the specific numeric mode
- makedirs()-uses recursion to create directories
In this article we saw which is best python modules for machine learning so about this article you have any query then free to ask me.
Credit: Arya Gupta