Create NumPy Array Within a Given Range

Prasad Naik
Updated on 16-Mar-2021 10:21:08

1K+ Views

We have to create a numpy array in the range provided by the user. We will use the arange() function in the numpy library to get our output.AlgorithmStep1: Import numpy. Step 2: Take start_value, end_value and Step from the user. Step 3: Print the array using arange() function in numpy.Example Codeimport numpy as np start_val = int(input("Enter starting value: ")) end_val = int(input("Enter ending value: ")) Step_val = int(input("Enter Step value: ")) print(np.arange(start_val, end_val, Step_val))OutputEnter starting value: 5 Enter ending value: 50 Enter Step value: 5 [ 5 10 15 20 25 30 35 40 45]

Convert Number to Words in R Data Frame Column

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:21:07

4K+ Views

To convert number to words in an R data frame column, we can use english function from english package. For example, if we have a data frame called df that contains a number column x then we can convert the numbers into words by using the command as.character(english(df$x)).ExampleConsider the below data frame − Live Demox

Create Identity Matrix Using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:20:45

4K+ Views

In this program, we will print an identity matrix of size nxn where n will be taken as an input from the user. We shall use the identity() function in the numpy library which takes in the dimension and the data type of the elements as parametersAlgorithmStep 1: Import numpy. Step 2: Take dimensions as input from the user. Step 3: Print the identity matrix using numpy.identity() function.Example Codeimport numpy as np dimension = int(input("Enter the dimension of identitiy matrix: ")) identity_matrix = np.identity(dimension, dtype="int") print(identity_matrix)OutputEnter the dimension of identitiy matrix: 5 [[1 0 0 0 0]  [0 1 0 0 0]  [0 0 1 0 0]  [0 0 0 1 0]  [0 0 0 0 1]]

Find Number of Rows and Columns in a Matrix Using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:20:19

3K+ Views

We will first create a numpy matrix and then find out the number of rows and columns in that matrixAlgorithmStep 1: Create a numpy matrix of random numbers. Step 2: Find the rows and columns of the matrix using numpy.shape function. Step 3: Print the number of rows and columns.Example Codeimport numpy as np matrix = np.random.rand(2,3) print(matrix) print("Total number of rows and columns in the given matrix are: ", matrix.shape)Output[[0.23226052 0.89690884 0.19813164]  [0.85170808 0.97725669 0.72454096]] Total number of rows and columns in the given matrix are:  (2, 3)

Custom Colors in Pandas Matplotlib Bar Graph

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:19:16

4K+ Views

To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass them into the scatter method to get the desired output.Using the set_color method, we could set the color of the bar.StepsTake user input for the number of bars.Add bar using plt.bar() method.Create colors from hexadecimal alphabets by choosing random characters.Set the color for every bar, using set_color() method.To show the figure we can use plt.show() method.Examplefrom matplotlib import pyplot as plt import random bar_count = int(input("Enter number of bars: ")) bars = plt.bar([i for ... Read More

Change Backends in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:18:47

2K+ Views

We can override the backend value using atplotlib.rcParams['backend'] variable.StepsUsing get_backend() method, return the name of the current backend, i.e., default name.Now override the backend name.Using get_backend() method, return the name of the current backend, i.e., updated name.Exampleimport matplotlib print("Before, Backend used by matplotlib is: ", matplotlib.get_backend()) matplotlib.rcParams['backend'] = 'TkAgg' print("After, Backend used by matplotlib is: ", matplotlib.get_backend())OutputBefore, Backend used by matplotlib is: GTK3Agg After, Backend used by matplotlib is: TkAgg Enter number of bars: 5

Row and Column Headers in Matplotlib's Subplots

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:18:23

552 Views

Using the subplot method, we can configure the number of rows and columns. nrows*nclos will create number positions to draw a diagram.StepsNumber of rows = 2, Number of columns = 1, so total locations are: 2*1 = 2.Add a subplot to the current figure, nrow = 2, ncols = 1, index = 1.Add a subplot to the current figure, nrow = 2, ncols = 1, index = 2.Using plt.show(), we can show the figure.Examplefrom matplotlib import pyplot as plt row_count = 2 col_count = 1 index1 = 1 # no. of subplots are: row*col, index is the position ... Read More

Remove Substring of Certain Length from a Given String in Python

Prasad Naik
Updated on 16-Mar-2021 10:17:50

183 Views

We need to write a Python program which will remove a certain substring from a given stringAlgorithmStep 1: Define a string. Step 2: Use the replace function to remove the substring from the given string.Example CodeLive Demooriginal_string = "C++ is a object oriented programming language"modified_string = original_string.replace("object oriented", "")print(modified_string)OutputC++ is a  programming languageExplanationThe in-build Python replace() function takes the following parameters:Oldstring: The string which you want to removeNewstring: The new string you want to replace in place of the oldstringCount: Optional. Number of times you want to replace the oldstring with the newstringRead More

Find Row Product of a Matrix in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:16:29

1K+ Views

To find the row product of a matrix in R, we can use apply function along with prod function. For example, if we have a matrix called M then to find the row product of a matrix we can use the command apply(M,1,prod). We need to remember that the output will be a vector not matrix. Check out the below examples to understand how to perform the row product of the matrix.ExampleConsider the below matrix − Live DemoM1

Plot Two Dotted Lines and Set Marker Using Matplotlib

Prasad Naik
Updated on 16-Mar-2021 10:15:42

2K+ Views

In this program, we will plot two lines using the matplot library. Before starting to code, we need to first import the matplotlib library using the following command −Import matplotlib.pyplot as pltPyplot is a collection of command style functions that make matplotlib work like MATLAB.AlgorithmStep 1: Import matplotlib.pyplot Step 2: Define line1 and line2 points. Step 3: Plot the lines using the plot() function in pyplot. Step 4: Define the title, X-axis, Y-axis. Step 5: Display the plots using the show() function.Example Codeimport matplotlib.pyplot as plt line1_x = [10, 20, 30] line1_y = [20, 40, 10] line2_x = ... Read More

Advertisements