Find Steps to Change One Word to Another in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:08:44

251 Views

Suppose we have a list of words called dictionary and we have another two strings start and end. We want to reach from start to end by changing one character at a time and each resulting word should also be in the dictionary. Words are case-sensitive. So we have to find the minimum number of steps it would take to reach at the end. If it is not possible then return -1.So, if the input is like dictionary = ["may", "ray", "rat"] start = "rat" end = "may", then the output will be 3, as we can select this path: ... Read More

Check Odd Length Cycle in a Graph using Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:02:11

469 Views

Suppose we have an undirected graph we have to check whether we can find an odd length cycle inside it or not.So, if the input is like adj_list = [[1, 2], [0, 3, 4], [0, 3, 4], [1, 2, 4], [1, 2, 3]]then the output will be True as there are odd length cycles like [0, 1, 3, 4, 2], [1, 3, 4], [2, 3, 4].To solve this, we will follow these steps −Define a function dfs() . This will take node, iif node is in path, thenreturn true when (i - path[node]) is oddif node is visited, thenreturn Falsemark ... Read More

Get Indices of List After Deleting Elements in Ascending Order in Python

Arnab Chakraborty
Updated on 12-Dec-2020 08:58:42

192 Views

Suppose we have a list of distinct values and we want to remove each number in non-decreasing order. We have to find the indices of numbers in order of their deletion.So, if the input is like nums = [4, 6, 2, 5, 3, 1], then the output will be [5, 2, 3, 0, 1, 0] as we delete 1, so array is [4, 6, 2, 5, 3], then remove 2, array is [4, 6, 5, 3], then remove 3 we get [4, 6, 5], then remove 4 we get [6, 5], remove 5, [6] and finally remove 6.To solve this, we will follow these steps −Define a function my_sort() . This will take indsif size of inds

Show Decimal Number from Rational Number Representation in C++

Arnab Chakraborty
Updated on 12-Dec-2020 08:54:06

418 Views

Suppose we have two numbers called numerator and denominator representing a rational number in the form (numerator / denominator). We have to find its decimal representation as a string. If there are some recurring numbers, then surround them with brackets.So, if the input is like numerator = 164 denominator = 3, then the output will be "54.(6)".To solve this, we will follow these steps −if numerator is same as 0, then −return "0"Define an array ansif numerator < 0 and denominator > 0 or numerator > 0 and denominator < 0, then −insert '-' at the end of ansdivisor := ... Read More

Broadcasting in NumPy for Python

AmitDiwan
Updated on 11-Dec-2020 11:34:09

176 Views

NumPy refers to ‘Numerical’ ‘Python’. It is a library that contains multidimensional array objects and multiple methods that help in processing the arrays.NumPy can be used to perform a wide variety of operations on arrays. It is used in conjunction with packages like SciPy, Matplotlib and so on. NumPy+Matplotlib can be understood as an alternative to MatLab. It is an open-source package, which means it can be used by anyone. Standard Python distribution doesn’t include NumPy package by default. The package has to be separately installed using the installer ‘pip’.For Windows, it has been shown below −pip install numpyOnce this ... Read More

Add Specific Tint to Grayscale Images in Scikit-Learn Python

AmitDiwan
Updated on 11-Dec-2020 11:33:00

363 Views

The values of ‘R’, ‘G’, and ‘B’ are changed and applied to the original image to get the required tint.Below is a Python program that uses scikit-learn to implement the same. Scikit-learn, commonly known as sklearn is a library in Python that is used for the purpose of implementing machine learning algorithms −Exampleimport matplotlib.pyplot as plt from skimage import data from skimage import color path = "path to puppy_1.jpg" orig_img = io.imread(path) grayscale_img = rgb2gray(orig_img) image = color.gray2rgb(grayscale_img) red_multiplier = [0.7, 0, 0] yellow_multiplier = [1, 0.9, 0] fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True) ax1.imshow(red_multiplier * image) ... Read More

Hysteresis Thresholding in Python using Scikit-Learn

AmitDiwan
Updated on 11-Dec-2020 11:31:58

2K+ Views

Hysteresis refers to the lagging effect of a result. With respect to threshold, hysteresis refers to the areas that are above a specific low threshold value or above high threshold values. It refers to areas that are highly-confident in nature.With the help of hysteresis, the noise outside the edges of the object in the image can be ignored.Let us see how hysteresis threshold can be achieved using scikit-learn library:Exampleimport matplotlib.pyplot as plt from skimage import data, filters fig, ax = plt.subplots(nrows=2, ncols=2) orig_img = data.coins() edges = filters.sobel(orig_img) low = 0.1 high = 0.4 lowt = (edges > low).astype(int) hight ... Read More

Upload and View an Image in Python Using Scikit-Learn

AmitDiwan
Updated on 11-Dec-2020 11:30:33

364 Views

Pre-processing data refers to cleaning of data, removing invalid data, noise, replacing data with relevant values and so on. This doesn’t always mean text data; it could also be images or video processing as well.Data pre-processing basically refers to the task of gathering all the data (which is collected from various resources or a single resource) into a common format or into uniform datasets (depending on the type of data). Since real-world data is never ideal, there is a possibility that the data would have missing cells, errors, outliers, discrepancies in columns, and much more.Sometimes, images may not be correctly ... Read More

Different Kinds of Gradient Descent Algorithms in Machine Learning

AmitDiwan
Updated on 11-Dec-2020 11:29:06

751 Views

The idea behind using gradient descent is to minimize the loss when in various machine learning algorithms. Mathematically speaking, the local minimum of a function is obtained.To implement this, a set of parameters are defined, and they need to be minimized. Once the parameters are assigned coefficients, the error or loss is calculated. Next, the weights are updated to ensure that the error is minimized. Instead of parameters, weak learners can be users, such as decision trees.Once the loss is calculated, gradient descent is performed, and tree is added to the algorithm step wise, so that loss is minimal.Some examples ... Read More

Fit Polynomial Regression Model for Non-Linear Trends in Python

AmitDiwan
Updated on 11-Dec-2020 11:01:35

179 Views

When regression models are being built, multicollinearity is checked for. This is because we need to understand the correlation present between all different combinations of continuous variables. If multicollinearity exists between the variables, we have to make sure that it is removed from the data.The data in real world is usually non-linear. We need to find mechanisms to fit such non-linear data to the model. We will be using Anscombe’s dataset to visualize this data.The ‘implot’ function is used with non-linear data −Exampleimport pandas as pd import seaborn as sb from matplotlib import pyplot as plt my_df = sb.load_dataset('anscombe') sb.lmplot(x ... Read More

Advertisements