Mastering Machine Learning Design Patterns in Python (Part Four : Final Part of KNN Series)

Dr. GP Pulipaka
5 min readJan 6, 2017

In Computer Science Engineering, design patterns are implemented to architect a software design solution. Implementing industrial best practices for object-oriented programming and code implementation require design patterns at a higher-level as an abstract solution. Most of the functions in Python are first-class citizens. The biological cognitive models based on the way the complex computations work in the brain inspire the architecture for neural networks for machine learning and deep learning. The connectionism in the human brain is implemented through artificial neural networks and massively parallel distributed processing with a wide range of capabilities for the models of memory, attention span, semantic representation, language, inception formation, and cognitive reasoning.

Following are some important design patterns in Python:

The factory pattern

The design patterns that deal with creation of objects in Python is to provide alternative solutions for a direct object creation through __init__() function. In the factory design pattern, the factories generate from the factory function, which returns a different object for each input parameter based on the request generated by the client without knowing the origin or the source of the class of the object generation. The fundamental concept behind factory pattern is to simplify the object creation. In order to track the objects that were created from the class, a central function would be crucial than the client creating the objects through the instantiation of the direct class. Therefore, the factory design patterns aid simplifying the code and reduce the footprint of the code by eliminating the complexity by dechaining the code from the object that produces the code and the object that consumes the code.

The building pattern

An object in object-oriented programming language such as Python requires building blocks that consist of multiple chunks of code portions that require composition as a step-by-step procedure. The object can reach the overall complete status, once all the blocks are built for the object. Builder design pattern aids in decoupling the construction of a complex object from its mere representation.

The prototype pattern

The prototype pattern in Python aids extending the functionality of an object in its entirety. However, the time factor has key value for the object and its prototype pattern as the extended object was created at a particular point of time. The extended object works as a reference of the original object and the changes applied to each reference object, does not impact the original object, as they’re independent of each other.

The adapter pattern

The adapter pattern deals with the relationships among multiple entities such as objects and the classes within the system. The structural design pattern aims at composing the functionality of the object in a simple way that creates the end-to-end new functionality. The adapter pattern also bolsters in making multiple interfaces as compatible, through they are incompatible by functionality. Several eCommerce systems leverage old components and new components. Leveraging old components from the existing system into the new system can be achieved through adapter patterns by simply enabling the communication between the components without having to modify the code. Especially, if the code is located in external library, it’s not possible to have access to modify the code and requires an effective way of implementing the code without having to access the old component.

The decorator pattern

There are few alternatives available to add new functionality to an existing object either by adding the functionality to the object, which is part of a class, or by composition or through inheritance. The first preferred option available for pattern implementation would be through composition over the inheritance, because the reusability factor for inheritance has high complexity as opposed to the flexibility from the composition. This is due to the fact that the inheritance is static. This is where the decorator pattern aids in implementing the object dynamically from a transparent perspective.

The façade pattern

As the input increases to the software engineering systems, the complexity increases significantly leading to the evolution of the systems with large-scale collection of interactions, interfaces, and classes. However, exposing such complexities to the client does not aid in simplifying the solution. The façade design pattern reduces the complexity and exposes the necessary components through the simplified interface.

The flyweight pattern

The overhead created by the object creation can create potential performance bottlenecks through the embedded systems with the resources racing for the execution of the objects. This is more apparent in large-scale big data ecosystems with complex systems. Every time a new object is created, it requires allocation of additional memory. In reality, when all the physical memory on the system has been consumed, the program swaps pages to the secondary storage of hard disk creating potential performance bottlenecks as it is slugging to process the data from disk-based system as opposed to processing from the in-memory computing of the main memory of the system. Though, SSD drives have better performance when contrasted with HDD drives, not all corporations leverage SSD drives. The flyweight design pattern aids in implementing efficient in-memory computing techniques by implementing shared object memory among multiple objects, which is immutable and state-independent.

I’ve written code in Python and uploaded my code and dataset into Github repository. Check it out at https://github.com/GPSingularity/Machine-Learning-in-Python.

References

Kasampalis, S. (2015). Mastering Python Design Patterns. Birmingham, England: Packt Publishing.

Sun, R. (2008). The Cambridge Handbook of Computational Psychology (1 ed.). Cambridge, England: Cambridge University Press.

--

--

Dr. GP Pulipaka

Ganapathi Pulipaka | Founder and CEO @deepsingularity | Bestselling Author | Big data | IoT | Startups | SAP | MachineLearning | DeepLearning | DataScience