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.
Table of Contents
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
There are several popular Python modules that are commonly used for machine learning, including:
NumPy: A numerical computing library that provides support for large, multi-dimensional arrays and matrices of numerical data.
Pandas: A library that provides fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive.
Matplotlib: A library for creating static, animated, and interactive visualizations in Python.
Scikit-learn: A library that provides a wide range of machine learning algorithms and related utilities.
TensorFlow: An end-to-end open source machine learning platform that allows users to build and deploy machine learning models.
It is worth noting that these are just a few of the many Python modules that are available for machine learning, and the best one for a particular task will depend on the specific needs of the project..
There are many different frameworks available in Python that can be used for a variety of purposes. Some of the most popular Python frameworks include:
Django: A high-level web framework that encourages rapid development and clean, pragmatic design.
Flask: A lightweight web framework that provides useful tools and features for building web applications.
Pyramid: A web framework that is designed to be flexible and easy to extend.
TurboGears: A web development framework that combines the simplicity of TurboGears 1 with the stability and scalability of the WebOb WSGI stack.
Bottle: A fast, simple, and lightweight web framework for Python.
CherryPy: A web framework that combines the flexibility of Python with the stability of a mature, production-quality web server.
These are just a few examples of the many frameworks that are available for Python. There are many others to choose from, depending on the specific needs of your project.