Server Side Programming Articles - Page 1409 of 2650

Explain how the minimum of a scalar function can be found in SciPy using Python?

AmitDiwan
Updated on 10-Dec-2020 13:45:53

188 Views

Finding the minimum of a scalar function is an optimization problem. Optimization problems help improve the quality of the solution, thereby yielding better results with higher performances. Optimization problems are also used for curve fitting, root fitting, and so on.Let us see an example −Exampleimport matplotlib.pyplot as plt from scipy import optimize import numpy as np print("The function is defined") def my_func(a):    return a*2 + 20 * np.sin(a) plt.plot(a, my_func(a)) print("Plotting the graph") plt.show() print(optimize.fmin_bfgs(my_func, 0))OutputOptimization terminated successfully.    Current function value: -23.241676    Iterations: 4    Function evaluations: 18    Gradient evaluations: 6 [-1.67096375]ExplanationThe required packages are imported.A ... Read More

How can discrete Fourier transform be performed in SciPy Python?

AmitDiwan
Updated on 10-Dec-2020 13:44:39

583 Views

Discrete Fourier Transform, or DFT is a mathematical technique that helps in the conversion of spatial data into frequency data.Fast Fourier Transformation, or FTT is an algorithm that has been designed to compute the Discrete Fourier Transformation of spatial data.The spatial data is usually in the form of a multidimensional array. Frequency data refers to data that contains information about the number of signals or wavelengths in a specific period of time.Let us see how this DFT can be achieved using the ‘SciPy’ library.The graph is created using the matplotlib library and data is generated using the Numpy library −ExampleFrom ... Read More

How can SciPy be used to calculate the eigen values and eigen vectors of a matrix in Python?

AmitDiwan
Updated on 10-Dec-2020 13:42:50

2K+ Views

Eigen vectors and Eigen values find their uses in many situations. The word ‘Eigen’ in German means ‘own’ or ‘typical’. An Eigen vector is also known as a ‘characteristic vector’. Suppose we need to perform some transformation on a dataset but the given condition is that the direction of data in the dataset shouldn’t change. This is when Eigen vectors and Eigen values can be used.Given a square matrix (a matrix where the number of rows is equal to the number of columns), an Eigen value and an Eigen vector fulfil the below equation.Eigen vectors are computed after finding the ... Read More

Explain how the top ‘n’ elements can be accessed from series data structure in Python?

AmitDiwan
Updated on 10-Dec-2020 13:40:49

133 Views

We have previously used slicing with the help of operator ‘:’, which is used in the case of extracting top ‘n’ elements from series structure. It helps assign a range to the series elements that will later be displayed.Let us see an example −Example Live Demoimport pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh', 'kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) n = 3 print("Top 3 elements are :") print(my_series[:n])OutputThe series contains following elements ab  34 mn  56 gh  78 kl  90 wq  123 az ... Read More

Explain the different ways in which data from a series data structure can be accessed in Python?

AmitDiwan
Updated on 10-Dec-2020 13:39:25

135 Views

The ability to index elements and access them using their positional index values serves a great purpose when we need to access specific values.Let us see how series data structure can be index to get value from a specific index.Example Live Demoimport pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh', 'kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) print("The second element (zero-based indexing)") print(my_series[2]) print("Elements from 2 to the last element are") print(my_series[2:])OutputThe series contains following elements ab  34 mn  56 gh  78 kl  90 wq ... Read More

Explain how series data structure in Python can be created using dictionary and explicit index values?

AmitDiwan
Updated on 10-Dec-2020 13:37:38

186 Views

Let us understand how series data structure can be created using dictionary, as well as specifying the index values, i.e., customized index values to the series.Dictionary is a Python data structure that has a mapping kind of structure- a key, value pair.Example Live Demoimport pandas as pd my_data = {'ab' : 11., 'mn' : 15., 'gh' : 28., 'kl' : 45.} my_index = ['ab', 'mn' ,'gh', 'kl'] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using dictionary and specifying index values") print(my_series)OutputThis is series data structure created using dictionary and specifying index values ab  11.0 mn  15.0 ... Read More

How can SciPy be used to calculate the cube root of values and exponential values in Python?

AmitDiwan
Updated on 10-Dec-2020 13:36:14

580 Views

When it is required to find the cube root of values, a function present in SciPy library can be used.Syntax of ‘cbrt’ functionscipy.special.cbrt(x)The ‘x’ is the parameter that is passed to the function ‘cbrt’ that is present in ‘special’ class of ‘SciPy’ library. Here’s an example −Example Live Demofrom scipy.special import cbrt my_cb = cbrt([27, 89]) print("The cube roots are :") print(my_cb)OutputThe cube roots are : [3. 4.4647451]ExplanationThe required packages are imported.The ‘cbrt’ function is called on the list of values whose cube root needs to be computed.The output is displayed on the console.When it is required to find the 10**x ... Read More

How can scikit learn library be used to preprocess data in Python?

AmitDiwan
Updated on 10-Dec-2020 13:34:59

331 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. It is an important step in the machine learning pipeline.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).This is done so that the learning algorithm can learn from this dataset and give relevant results with high accuracy. Since real-world ... Read More

How to apply functions element-wise in a dataframe in Python?

AmitDiwan
Updated on 10-Dec-2020 13:30:21

1K+ Views

It may sometimes be required to apply certain functions along the elements of the dataframe. All the functions can’t be vectorised. This is where the function ‘applymap’ comes into picture.This takes a single value as input and returns a single value as output.Example Live Demoimport pandas as pd import numpy as np my_df = pd.DataFrame(np.random.randn(5, 5), columns=['col_1', 'col_2', 'col_3', 'col_4', 'col_5']) print("The dataframe generated is ") print(my_df) my_df.applymap(lambda x:x*11.45) print("Using the applymap function") print(my_df.apply(np.mean))OutputThe dataframe generated is     col_1       col_2      col_3      col_4     col_5 0  -0.671510  -0.860741   0.886484   0.842158   ... Read More

How can a specific operation be applied row wise or column wise in Pandas Python?

AmitDiwan
Updated on 10-Dec-2020 13:28:19

406 Views

It may sometimes be required to apply certain functions along the axes of a dataframe. The axis can be specified, otherwise the default axis is considered as column-wise, where every column is considered as an array.If the axis is specified, then the operations are performed row-wise on the data.The ‘apply’ function can be used in conjunction with the dot operator on the dataframe. Let us see an example −Example Live Demoimport pandas as pd import numpy as np my_data = {'Age':pd.Series([45, 67, 89, 12, 23]), 'value':pd.Series([8.79, 23.24, 31.98, 78.56, 90.20])} print("The dataframe is :") my_df = pd.DataFrame(my_data) print(my_df) print("The description of ... Read More

Advertisements