Found 10476 Articles for Python

Return a boolean array where the string element in array ends with a given suffix but test begin and end in Python

AmitDiwan
Updated on 24-Feb-2022 07:45:04

179 Views

To return a boolean array which is True where the string element in array ends with suffix, use the numpy.char.endswith() method in Python Numpy. The method outputs an array of bools. The first parameter is the input array. The second parameter is the suffix. With optional start parameter, test beginning at that position. With optional end parameter, stop comparing at that position.StepsAt first, import the required library −import numpy as npCreate a One-Dimensional array of strings −arr = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'CRADlE'])Displaying our array −print("Array...", arr)Get the datatype −print("Array datatype...", arr.dtype) Get the dimensions of the Array −print("Array Dimensions...", ... Read More

Test whether similar int type of different sizes are subdtypes of integer class in Python

AmitDiwan
Updated on 24-Feb-2022 07:41:39

91 Views

To test whether similar int type of different sizes are subdtypes of integer class, use the numpy.issubdtype() method in Python Numpy. The parameters are the dtype or object coercible to one.StepsAt first, import the required library −import numpy as npUsing the issubdtype() method in Numpy. Checking for integer datatype with different sizes −print("Result...", np.issubdtype(np.int16, np.signedinteger)) print("Result...", np.issubdtype(np.int32, np.signedinteger)) print("Result...", np.issubdtype(np.int64, np.signedinteger)) print("Result...", np.issubdtype(np.int16, np.integer)) print("Result...", np.issubdtype(np.int32, np.integer)) print("Result...", np.issubdtype(np.int64, np.integer))Exampleimport numpy as np # To test whether similar int type of different sizes are subdtypes of integer class, use the numpy.issubdtype() method in Python Numpy. # The parameters are ... Read More

Return the gradient of an N-dimensional array over axis 0 in Python

AmitDiwan
Updated on 24-Feb-2022 07:39:18

308 Views

The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array. The 1st parameter, f is an Ndimensional array containing samples of a scalar function. The 2nd parameter is the varargs i.e. the spacing between f values. Default unitary spacing for all dimensions.The 3rd parameter is the edge_order{1, 2} i.e. the Gradient is calculated using N-th order accurate differences at the boundaries. Default: 1. The 4th parameter is the Gradient, ... Read More

Calculate the n-th discrete difference over axis 0 in Python

AmitDiwan
Updated on 24-Feb-2022 07:35:15

184 Views

To calculate the n-th discrete difference, use the numpy.diff() method. The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. The diff() method returns the n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is datetime64, which results in ... Read More

Compute the Natural Logarithm in Python

AmitDiwan
Updated on 24-Feb-2022 07:28:53

340 Views

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e. The method returns the natural logarithm of x, elementwise. This is a scalar if x is a scalar. The 1st parameter is the input value, array-like. The 2nd parameter is out, a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number ... Read More

Convert angles from degrees to radians in Python

AmitDiwan
Updated on 24-Feb-2022 07:31:44

3K+ Views

To convert angles from degrees to radians, use the numpy.radians() method in Python Numpy. The method returns the corresponding radian values. This is a scalar if x is a scalar. The 1st parameter is an input array in degrees. The 2nd and 3rd parameters are optional.The 2nd parameter is an ndarray, A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.The 3rd parameter ... Read More

How to change the attributes of a networkx / matplotlib graph drawing?

Rishikesh Kumar Rishi
Updated on 02-Feb-2022 11:56:11

2K+ Views

To change the attributes of a netwrokx/matplotlib graph drawing, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Initialize a graph with edges, name, or graph attributes.Add the graph's attributes. Add an edge between u and v.Get the edge attributes from the graph.Position the nodes with circles.Draw the graph G with Matplotlib.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True G = nx.Graph() G.add_edge(0, 1, color='r', weight=2) G.add_edge(1, 2, color='g', weight=4) G.add_edge(2, 3, color='b', weight=6) G.add_edge(3, 4, ... Read More

How to fill an area within a polygon in Python using matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Feb-2022 11:47:09

2K+ Views

To fill an area within a polygon in Python using matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Get an instance of a polygon.Get the generic collection of patches with iterable polygons.Add a 'collection' to the axes' collections; return the collection.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots(1) polygon = Polygon(np.random.rand(6, 2), closed=True, alpha=1) ... Read More

How to get data labels on a Seaborn pointplot?

Rishikesh Kumar Rishi
Updated on 02-Feb-2022 11:37:50

3K+ Views

To get data labels on a Seaborn pointplot, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Create a pointplot.Get the axes patches and label; annotate with respective labels.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 3, 1, 2, 3, 1]}) ax = sns.pointplot(df["a"],    order=df["a"].value_counts().index) for p, label in zip(ax.patches, df["a"].value_counts().index):    ax.annotate(label, ... Read More

How to draw a precision-recall curve with interpolation in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Feb-2022 11:33:00

849 Views

To draw a precision-recall curve with interpolation in Python, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create r, p and duplicate recall, i data points using numpy.Create a figure and a set of subplots.Plot the recall matrix in the range of r.shape.Plot the r and dup_r data points using plot() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True r = np.linspace(0.0, 1.0, num=10) p = np.random.rand(10) * (1. - r) dup_p = p.copy() i ... Read More

Advertisements