Skip to main content

Posts

Showing posts with the label Languages,Python

Mahotas – Template Matching

In this article we will see how we can do template matching in mahotas. Template is basically a part or structure of image. In this tutorial we will use “lena” image, below is the command to load it.   mahotas.demos.load('lena') Below is the lena image      In order to do this we will use mahotas.template_match method Syntax : mahotas.template_match(img, template) Argument : It takes image object and template as argument Return : It returns image object    Note : Input image should be filtered or should be loaded as grey In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this   image = image[:, :, 0] Below is the implementation    Python3 # importing required libraries import mahotas import mahotas.demos from pylab import gray, imshow, show import numpy as np import matplotlib.pyplot as plt      # loading image ...

K Nearest Neighbors with Python | ML

K-Nearest Neighbors is one of the most basic yet essential classification algorithms in Machine Learning. It belongs to the supervised learning domain and finds intense application in pattern recognition, data mining, and intrusion detection. The K-Nearest Neighbors (KNN) algorithm is a simple, easy-to-implement supervised machine learning algorithm that can be used to solve both classification and regression problems. The KNN algorithm assumes that similar things exist in close proximity. In other words, similar things are near to each other. KNN captures the idea of similarity (sometimes called distance, proximity, or closeness) with some mathematics we might have learned in our childhood— calculating the distance between points on a graph. There are other ways of calculating distance, which might be preferable depending on the problem we are solving. However, the straight-line distance (also called the Euclidean distance ) is a popular and familiar choice. It is widely disposabl...

Difference Between ‘+’ and ‘append’ in Python

Using ‘+’ operator to add an element in the list in Python: The use of the ‘+’ operator causes Python to access each element of that first list. When ‘+’ is used a new list is created with space for one more element. Then all the elements from the old list must be copied to the new list and the new element is added at the end of this list. Example: sample_list = [] n = 10     for i in range (n):              # i refers to new element      sample_list = sample_list + [i]     print (sample_list) Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] The ‘+’ operator refers to the accessor method and does not modify the original list. In this, sample_list doesn’t change itself. This type of addition of element in sample_list creates a new list from the elements in the two lists. The assignment of sample_list to this new list updates PythonList object so it now refers to the new list. Complexity to add n elements Have you wonder...

id() function in Python

In Python, the id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Syntax: id(object) Return: a unique integer for a given object Example: Python3 x = 42 y = x z = 42   print ( id (x))  print ( id (y))  # (same as x) print ( id (z))  # (same as x and y) Output : 10731304 10731304 10731304 As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.  Py...

Get current date using Python

Prerequisite: DateTime module In Python , Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions to get the current date as well as time. Get the current date using date.today() The today() method of date class under DateTime module returns a date object which contains the value of Today’s date. Syntax: date.today()  Returns: Return the current local date. Example:   Python3 # Import date class from datetime module from datetime import date   # Returns the current local date today = date.today() print ( "Today date is: " , today) Output: Today date is: 2019-12-11 Get the current date using datetime.now() Python library defines a function that can be primarily used to get the current time and dat...