Found 10476 Articles for Python

Program to find number of steps required 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

Program to check whether odd length cycle is in a graph or not in 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

Program to get indices of a 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

Discuss broadcasting in Numpy in 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

How can a specific tint be added to grayscale images in scikit-learn in 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

What is hysteresis thresholding? How can it be achieved using scikit-learn in Python?

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

How can scikit learn library be used to upload and view an image in Python?

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

How can a polynomial regression model be fit to understand non-linear trends in data 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

How can SciPy be used to calculate the permutations and combination values in Python?

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

667 Views

SciPy can be used to determine the permutation and combination with respect to two values.A function named ‘perm’ present in the class ‘special’ in ‘SciPy’ is used.Syntax of ‘perm’ functionscipy.special.perm(N, k)Performing permutation on a set of values has been shown belowExample Live Demofrom scipy.special import perm my_permute = perm(6, 2, exact = True) print("The permutation of 6 and 2 is ") print(my_permute)OutputThe permutation of 6 and 2 is 30ExplanationThe required libraries are imported.Parameters are passed to the ‘perm’ function that computes the value.The value is assigned to a variable.This variable is displayed on the console.Computing combination of two values in SciPyA ... Read More

How can ‘implot’ function be used to fit values to data if one of the variables is a discrete value in Python?

AmitDiwan
Updated on 11-Dec-2020 10:59:15

220 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.This is where functions ‘regpot’ and ‘implot’ come into play. They help visualize a linear relationship between variables in linear regression.The ‘regplot’ function accepts values for variables ‘x’ and ‘y’ in a variety of formats, and this includes numpy arrays, pandas series objects, references to variables or values from a pandas dataframe.On the other hand, the ... Read More

Advertisements