The np.cumsum() is a numpy library function that returns the cumulative sum of the elements along the given axis. The numpy cumsum() function returns an array. The array consists of the cumulative sum of the original array. The cumulative sum is not similar to the sum function. Syntax numpy.cumsum(arr, axis=None, dtype=None, out=None) Parameters The np.cumsum() […]

The post np.cumsum: How to Use numpy cumsum() Function in Python appeared first on AppDividend.

]]>The **np.cumsum()** is a numpy library function that returns the cumulative sum of the elements along the given axis. The numpy cumsum() function returns an array. The array consists of the cumulative sum of the original array. The cumulative sum is not similar to the sum function.

`numpy.cumsum(arr, axis=None, dtype=None, out=None) `

The **np.cumsum()** function has one required argument and three optional arguments as parameters:

**arr**: The array is passed in this argument. This value is the argument needed for returning the cumulative sum of the array. This array is given inside the cumsum() function for finding the cumulative sum of this array.**axis**: The axis along which the cumulative sum is calculated is given as the value of this axis argument. This axis is kept as None by default. However, the axis can be changed.**dtype:**The**dtype**stands for Data type. This specifies the output data type. The output data will be returned in this specified data type.**out**: This is the destination where the output array is stored. This array size specified must match the output obtained from the function.

The **np.cumsum()** function returns an array as output. The **cumsum()** function finds the cumulative sum for the array and returns the cumulative sum in the array format.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array called arr
arr = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])
# Printing the array arr
print("The array arr is: ")
print(arr)
# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)
# Finding the sum from the cumulative sum function for the array
res = np.cumsum(arr)[-1]
# Printing the sum of the array
print("The sum of the array using the cumulative sum function is:")
print(res)
```

```
The array arr is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The sum of the array using the cumulative sum function is:
112
```

We imported the numpy package that provides several array functions in this program. Then, we created a numpy array called arr using the function called **np.array()**. This arr array consists of several integer elements.

For this array, the sum is calculated using the cumulative sum function. To do that, we passed the array into the **cumsum()** function. The **cumsum()** function finds the cumulative sum then we have returned only the last value from the array by the -1 indexing. It returns the last element from the array.

The last element in the array is the sum of all the elements in the array. Hence we have printed the sum of all the elements using the **cumsum()** function.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array called a
a = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])
# printing the array a
print("The array a is: ")
print(a)
# Printing the shape of the array a
print("The shape of the array arr is : ", a.shape)
# Finding the cumulative sum of the single dimensional array
op = np.cumsum(a)
# Printing the sum of the array
print("The cumulative sum of the array is: ")
print(op)
# Creating an numpy array called arr
arr = np.array([[1, 4, 0], [7, 0, 9], [5, 0, 3], [12, 0, 5]])
# printing the array arr
print("The array arr is: ")
print(arr)
# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)
# Finding the cumulative sum for the two dimensional array
res = np.cumsum(arr, axis=1)
# Printing the cumulative sum of the array
print("The cumulative sum of the array is: ")
print(res)
```

```
The array a is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The cumulative sum of the array is:
[ 5 11 18 26 35 45 56 68 87 112]
The array arr is:
[[ 1 4 0]
[ 7 0 9]
[ 5 0 3]
[12 0 5]]
The shape of the array arr is : (4, 3)
The cumulative sum of the array is:
[[ 1 5 5]
[ 7 7 16]
[ 5 5 8]
[12 12 17]]
```

We created two numpy arrays called a and arr using the function called **np.array()**.

The **“a”** array is a single-dimensional array. This array consists of some integer values. We passed this array into the np.cumsum() function.

The **np.cumsum()** function calculates the cumulative sum of the array. The cumulative sum is the sum of the current element plus the cumulative sum of the previous element. The result is displayed.

The **arr** array consists of four rows, each row consisting of 3 columns. Then we passed this array to the **np.cumsum()** function with the axis as 1. The **cumsum()** function calculates the cumulative sum and returns the array.

That’s it for the numpy cumsum() function.

The post np.cumsum: How to Use numpy cumsum() Function in Python appeared first on AppDividend.

]]>The np.histogram() is a numpy library function that returns an array that can be used for plotting in the graph. The array is created based on the parameters passed. The np.histogram() function computes the histogram for the data given inside the function. It can be used for exploring the data. This array can be plotted […]

The post np.histogram: How does numpy historgam() works in Python appeared first on AppDividend.

]]>The **np.histogram()** is a **numpy** **library** **function** that returns an array that can be used for plotting in the graph. The array is created based on the parameters passed. The **np.histogram()** function computes the histogram for the data given inside the function. It can be used for exploring the data. This array can be plotted in a graph to easily understand the data.

`numpy.histogram(arr, bins=10, range=None, normed=None, weights=None, density=None)`

The **np.histogram()** function takes one required argument as a parameter and has five optional arguments:

**arr**: The array is passed in this argument. This array is the required argument for returning the histogram. The array to be given to the histogram function is shown in this argument.**bins**: It is the number of bins. Ten is kept as the default bins. This bin’s value can be a sequence of numbers or int or str. If this is int, then the width of the bins is equally created. If it is a list, then the values for the bins changes.**range**: This is the range within which the values will be considered. If the value exceeds the range then that value will not be considered. This range consists of two values, start, and end. This is enclosed within a tuple. This tuple consists of ( start, end ) start and end values in the range.**normed**: This is similar to the density argument.**weight**: This argument is passed with an array consisting of weights. This weight array’s shape should be equal to the shape of the array a. These weights are normalized if the density argument is set to**True**.**density**: If it is**True,**then it is a value of the probability density function. If it is False, the array will have the number of samples present in each**bin**.

It returns two values. One is an array consisting of histogram values, and the other consists of bin edges.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)
# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.histogram(arr)
print("The Histogram array is: ", res)
```

```
The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
The Histogram array is: (array([1, 1, 1, 0, 0, 1, 0, 1, 1, 1]), array([1. , 1.8, 2.6, 3.4, 4.2, 5. , 5.8, 6.6, 7.4, 8.2, 9. ]))/* Your code... */
```

In this program, we imported numpy to create a numpy array. Then, we printed the array and printed the shape of the array using the np.shape.

We passed this array into the **np**.**histogram()** function in the next step. The **np.histogram()** function returns two values one is an array consisting of histogram values, and the other is the bins edge values.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.random.randint(70, size=(40))
print("The array is : ", arr)
# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.histogram(arr, bins=[0, 10, 20, 30, 40, 50], range=(0, 50))
print("The Histogram array is: ", res)
```

```
The array is : [41 19 64 35 10 60 56 56 6 28 37 61 8 4 39 45 29 3 5 19 24 28 57 10
30 46 51 30 16 40 57 64 54 68 37 30 2 41 41 12]
Shape of the array is : (40,)
The Histogram array is: (array([6, 6, 4, 7, 6]), array([ 0, 10, 20, 30, 40, 50]))
```

In this program, we passed the values bins and range. The **range()** function removes the values greater than and less than the specified range. Only the values that are within the specified range will be considered.

That’s it for this tutorial.

The post np.histogram: How does numpy historgam() works in Python appeared first on AppDividend.

]]>The np.argshort() is the numpy library function that returns an index of the array elements in the sorted order. The numpy argshort() function takes the original array as a parameter and returns an array. This array consists of an index of the elements corresponding to the index of the sorted array elements. This array consists […]

The post np.argsort: How to Use numpy argshort() in Python appeared first on AppDividend.

]]>The **np.argshort()** is the **numpy** **library** function that returns an index of the array elements in the sorted order. The **numpy argshort()** function takes the original array as a parameter and returns an array. This array consists of an index of the elements corresponding to the index of the sorted array elements. This array consists of indices that would sort the array.

`numpy.argsort(arr, axis=- 1, kind=None, order=None)`

The **np.argsort()** function takes one required argument as a parameter and has three optional arguments:

**arr**: The array is passed in this argument. This array is the argument needed for returning the array. The array to be given to the argsort function is given in this argument.**axis**: The axis along which the sorting should be done is given in this argument. This axis is kept as -1 by default. We can also give None in the axis.**kind**: This is the kind of sorting algorithm that is to be used by the**argsort()**function. This is None by default. For example, we can use**quicksort**,**mergesort**,**heapsort**, or**stable**. This algorithm passed in the kind argument is used inside the argsort function for returning the array.**order**: It is used as the precedence order. We can set the value as**str**or a**list**of str as ordered.

It returns an array. This array consists of the indices that would sort the array.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)
# Printing the shape of the array using shape function
print("Shape of the array is : ", arr.shape)
res = np.argsort(arr)
print("Indices that would sort the array is: ", res)
```

```
The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
Indices that would sort the array is: [4 6 1 0 2 3 5]
```

In this program, we imported numpy for using the numpy functions. Then, we created a numpy array. We printed the array and then printed the shape of the array using the shape function. Then, we have passed this array into the **np.argsort()** function. The **argshort()** function returns an array. The array consists of indices that would sort the elements in the array.

The **argshort()** function sorts the elements in ascending order in this program. Instead of sorting the elements, the corresponding indices are sorted. These indices can be used to sort the elements.

We can see that the first element in the indices list is 4, and the 4th element in the array is 1. Hence 1 is the first element. Similarly, in the next position, the indices are 6, and the element in the 6th position is 2.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)
# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.argsort(arr, kind='mergersort', axis=0)
print("Indices that would sort the array is: ", res)
print(" The sorted array is: ", arr[res])
```

```
The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
Indices that would sort the array is: [4 6 1 0 2 3 5]
```

We passed the values for kind and axis in this program as merge sort and 0. This function then sorts the array using the merge sort algorithm in the axis 0. Then we printed the indices array. Then we printed the sorted array bypassing the indices array as an index to the array.

That’s it for the **numpy argshort()** function tutorial.

How to sort list alphabetically in Python

The post np.argsort: How to Use numpy argshort() in Python appeared first on AppDividend.

]]>The np.char.count() is a numpy library function that counts the total number of occurrences of a character in a string or the array. It comes under the char class. If the np.char.count() function is used in an array, it returns an array containing a count of the character in each array element. Syntax numpy.char.count(arr, sub, start, end=None) […]

The post np.count: How to Count Number of Elements in Python Array appeared first on AppDividend.

]]>The **np.char.count() **is a **numpy** **library** function that counts the total number of occurrences of a character in a string or the array. It comes under the **char **class. If the **np.char.count()** function is used in an array, it returns an array containing a count of the character in each array element.

`numpy.char.count(arr, sub, start, end=None)`

The **np.char.count()** function takes two required arguments as a parameter and has two optional arguments:

**arr**: It is an array or string passed in this argument. This array is the required argument for returning the count. The array in which the count is needed to be found is passed in this argument.**sub**: The substring is given in this argument. This substring needs to be counted in the array. This substring is searched in the array. If the match is found, then the count is increased. This is the required argument.**start**: It is the starting index of the array. It is an optional argument. If this argument is passed with some values, then the count function is applied from the starting index of each element in the array. The count function is not applied to the indices before the starting index of each element in the array.**end**: It is the ending index of the array. It is an optional argument. If this argument is passed with some values, then the count function is applied till the ending index specified in the argument.

It returns an array. This array consists of counting the number of times the substring is present in the array. If the substring is present in the element, then the count is incremented by one.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.array(['Hello How are You', 'an apple for a day', 'aaasskdfj', 'He is aakash'])
print("The array is : ", arr)
count = np.char.count(arr, 'a')
print("Count of 'a' in array", count)
```

```
The array is : ['Hello How are You' 'an apple for a day' 'aaasskdfj' 'He is aakash']
Count of 'a' in array [1 4 3 3]
```

We imported a **numpy** **package** consisting of functions for numerical calculations in this program. Then, we created a numpy array called **arr** using the function called **np.array()**. This array consists of several strings inside the numpy array.

In the next step, we passed this array to the **np.char.count()** function that presents in the char class. The **np.char.count()** function counts the number of occurrences of the substring in the elements present in the string. It first takes the first element and finds the count and stores this in the first index of the output array.

Similarly, the count is found for all the other elements in the array. Hence the count for all the elements is found and returned as an array.

```
# Importing numpy as np
import numpy as np
# Creating a numpy array called arr
arr = np.array(['Hello How are You', 'an apple for a day','aaasskdfjdgfgs', 'He is aakash'])
print("The array is : ", arr)
count = np.char.count(arr, 'a', start=3, end=10)
print("Count of 'a' in array", count)
```

```
The array is : ['Hello How are You' 'an apple for a day' 'aaasskdfjdgfgs' 'He is aakash']
Count of 'a' in array [0 1 0 3]
```

In this program, we passed the values for the start and end index. This finds the count of a number of occurrences of the substring in between starting and ending index.

That’s it for np.count() function tutorial.

The post np.count: How to Count Number of Elements in Python Array appeared first on AppDividend.

]]>The np.vstack() is a numpy library function that returns an array in the vertically stacked order. The vstack stands for a vertical stack. The np.vstack() function returns an array that combines the first array followed by the second array. The np.vstack() function stacks the second array after the first array. To horizontal stack array elements in Python, […]

The post np.vstack: How to Stack Array in Vertical Direction appeared first on AppDividend.

]]>The **np.vstack()** is a **numpy** **library** function that **returns an array in the vertically stacked order. **The **vstack stands **for a** vertical stack. **The **np.vstack() **function returns an array that combines the first array followed by the second array. The **np.vstack()** function stacks the second array after the first array.

To horizontal stack array elements in Python, use the **np.hstack() **function.

Let’s see the syntax of the **numpy vstack()** method.

`numpy.vstack(tup)`

The **np.vstack()** function takes one required arguments as a parameter:

**tup**: The arrays are passed inside the tuple. From these arrays, the **np.vstack()** function returns the new array that contains values from the first array, followed by the values from the second array. It is the required argument for returning the vertically stacked array. The **np.vstack()** function appends the value of the second array to the first array. The arrays should contain the same shape.

It returns the array. This array consists of all the elements from the first array and is followed by all the elements from the second array.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array called arr1
arr1 = np.array([4, 5, 6])
# Creating an numpy array called arr2
arr2 = np.array([7, 8, 9])
# Creating a vertically stacked array from arr1 and arr2
res = np.vstack((arr1, arr2))
print(res)
```

```
[[4 5 6]
[7 8 9]]
```

In this program, we imported a numpy package that has functions for numerical calculations. Then, we have created two numpy arrays called **arr1** and **arr2** using the function called **np.array()**. Then, we passed these two arrays into the vstack function. The **np.vstack()** function combines array 1 and array 2 vertically and returns a combined array.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array called arr1
arr1 = np.array([[4], [5], [6]])
# Creating an numpy array called arr2
arr2 = np.array([[7], [8], [9]])
# Creating a vertically stacked array from arr1 and arr2
res = np.vstack((arr1, arr2))
print(res)
```

```
[[4]
[5]
[6]
[7]
[8]
[9]]
```

In this program, two nested arrays are created. These nested arrays are then passed to the **np.vstack(**) function. The **np.vstack()** function vertically stacks the **arr1** with the **arr2**. Hence, the resultant array is formed by appending the **arr2** in the **arr1** array. The **np.vstack()** function is similar to concatenating **arr2** with the **arr1 **using np.concatenate() function.

That’s it for numpy vstack() function.

The post np.vstack: How to Stack Array in Vertical Direction appeared first on AppDividend.

]]>The np.diff() is a numpy array function that finds the difference numbers in an array. The np.diff() function can be applied to a single array and multiple arrays. If a single array is passed then the difference is found by res[i] = arr[i+1] – arr[i]. Syntax numpy.diff(a, n = 1, axis= -1, prepend = < no […]

The post np.diff: How to Find Difference Between Numbers in Array appeared first on AppDividend.

]]>The **np.diff()** is a **numpy** **array** function that **finds the difference **numbers in an array. The **np.diff()** function can be applied to a single array and multiple arrays. If a single array is passed then the difference is found by res[i] = arr[i+1] – arr[i].

`numpy.diff(a, n = 1, axis= -1, prepend = < no value >, append = < no value >)`

The **np.diff()** function takes five arguments as a parameter:

**a**: It is the array for which the difference is found using the**np.diff()**function.**n**: The number of times the array is differenced is passed as the argument to n. This is by default set to 1.**axis**: It is the axis in which the difference is calculated. It means that from right to left are from left to right. By default, the value is set to -1. However, we can also have the axis as 0.**prepend**: The values appended at the beginning before performing the**diff()**function.**append**: The values that are appended at the ending before performing the**diff()**function.

The np.diff() function returns an array. This array consists of values that are the difference between the two numbers in an array.

```
# Importing numpy as np
import numpy as np
# Creating an array arr
arr = np.array([5, 10, 7, 11, 15])
# creating a new array by using the diff function
diff_arr = np.diff(arr)
print(diff_arr)
```

`[ 5 -3 4 4]`

In this program, we imported numpy library and created a numpy array using the **np.array()** function. Then, we passed this array into the **np.diff()** function. Hence, we passed the value for a. The function performs the operations like diff_arr[i] = arr[i+1] – arr[i].

Hence, the first index will store the difference between the first two elements, which is 10 – 5; therefore, the first element is 5. Then in the second index of the **diff_arr**, the value is stored as 7 – 10 is -3. Similarly, this function calculates for all the elements in the array.

```
# Importing numpy as np
import numpy as np
# Creating an array arr
arr = np.array([[5, 10, 7, 11, 15], [5, 20, 5, 15, 10]])
# creating a new array by using the diff function
diff_arr = np.diff(arr, n=1, axis=0)
print(diff_arr)
```

`[[ 0 10 -2 4 -5]]`

In this program, we performed the **np.diff()** function in axis 0. Hence, it does the difference function like subtracting the first array elements from the second array elements. The internal formula is: **diff_arr[0][i] = arr[1][i]-arr[0][i]**. Hence, the first element of the **diff_arr** is equal to 5 – 5 = 0. Similarly, the others are calculated.

That’s it for the **numpy diff()** function in **Python**.

The post np.diff: How to Find Difference Between Numbers in Array appeared first on AppDividend.

]]>The np.unique() is a numpy library function that returns all the unique elements in the array. The returned array does not contain duplicate elements. The np.unique() function can be used to remove duplicates. It returns all the elements only one time. The unique() function has other parameters like the return index. It returns the index […]

The post np.unique: How to Get Unique Values in NumPy Array Python appeared first on AppDividend.

]]>The **np.unique()** is a numpy library function that returns all the unique elements in the array. The returned array does not contain duplicate elements. The **np.unique()** function can be used to remove duplicates. It returns all the elements only one time. The unique() function has other parameters like the return index. It returns the index of the unique elements.

`numpy.unique(arr, return_index=False, return_inverse=False, return_counts=False, axis=None)`

The **np.unique()** function takes one required argument as a parameter and four arguments as optional arguments:

**arr**: The array is passed in this argument. From this array, the function returns the unique elements. This array is the required argument for returning the unique elements. This is because we cannot return the unique elements without an array.**return_index**: This argument takes only Boolean values as parameters. It has two Boolean statements: True and the other is**False**. If the value is**True,**then the**np.unique()**function returns two values: the unique array and the indices of that unique element. If**False**, it returns the unique array alone.**return_inverse**: This argument takes only Boolean values as parameters. This has two Boolean statements: True and the other is False. If the value is True, then this function returns two values: the unique array and the indices of the original array elements in the unique array elements. And if false, it returns the array alone.**return_counts**: This argument takes only Boolean values as parameters. This has two Boolean statements: True and the other is False. If the value is True, this function returns two values: the unique array and the other is the count of each unique element in the original array. If false, it returns the array alone.**axis**: This specifies the axis in which the unique elements are returned. By default, this is set to None.

It returns an array. This array consists of all the unique elements from the original array. If **return_index**, **return_inverse**, or **return_counts** are passed **True** in the arguments, then the **np.unique()** function returns an original array, and the array for the arguments is passed as **True**.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array
arr = np.array([5, 6, 7, 5, 7, 8, 3, 4, 3, 3])
# Finding the unique values using the unique function
res = np.unique(arr)
print(res)
```

`[3 4 5 6 7 8]`

We imported a **numpy** **package** consisting of functions for numerical calculations into this program.

We created a **numpy array** using the **np.array()** function in the next step. This created array consists of duplicate values. Then, we passed an array into the **np.unique()** function.

The **np.unique()** function returns all the unique elements from the array. It returns all the elements from the array only once. In this example, 5 is present in two places, but the **np.array()** function returns only once.

The np.unique() function returns the unique values in the ascending order. We have passed the array as the argument to the function.

```
# Importing numpy as np
import numpy as np
# Creating an numpy array
arr = np.array([5, 6, 7, 5, 7, 8, 3, 4, 3, 3])
# Finding the unique values using the unique function
uni, index = np.unique(arr, return_index=True)
print(" The unique array is: ", uni, " and the indices are ", index)
uni, inver = np.unique(arr, return_inverse=True)
print(" The unique array is: ", uni, " and the inverses are ", inver)
uni, index, inver, count = np.unique(arr, return_index=True, return_inverse=True, return_counts=True)
print(" The unique array is: ", uni, " and the indices are ", index, " and the inverses are ", inver, " counts are: ", count)
```

```
The unique array is: [3 4 5 6 7 8] and the indices are [6 7 0 1 2 5]
The unique array is: [3 4 5 6 7 8] and the inverses are [2 3 4 2 4 5 0 1 0 0]
The unique array is: [3 4 5 6 7 8] and the indices are [6 7 0 1 2 5] and the inverses are [2 3 4 2 4 5 0 1 0 0] counts are: [3 1 2 1 2 1]
```

This program passed the parameters for **return_index**, **return_inverse**, and **return_counts**. The **return_index** argument returns the index of the unique elements.

In the **return_inverse** argument, the elements from the original array are compared with the unique array index. For example, the first element of the original array is 5. This 5 is present in the 2 indexes of the unique elements; hence 2 is returned as the index.

That’s it for this **np.unique() **function.

The post np.unique: How to Get Unique Values in NumPy Array Python appeared first on AppDividend.

]]>The np.sin() is a numpy library function that generates the sine value for the angle passed inside the function. Syntax numpy.sin(x, /,out=None,*,where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'sin'> Arguments The np.sin() function takes one required arguments as a parameter: x: This is the angle to be passed to the function. This is […]

The post np.sin: How to Use numpy sin() Function in Python appeared first on AppDividend.

]]>The **np.sin()** is a **numpy** **library** function that **generates the sine value for the angle passed inside the function.**

`numpy.sin(x, /,out=None,*,where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'sin'>`

The **np.sin()** function takes one required arguments as a parameter:

**x**: This is the angle to be passed to the function. This is given in radians. This is the required parameter for calculating the sine value.**out**: We can specify the location where we want to store the output value. By default, None is kept as the value for this argument.**where**: If this condition is True, then the ufunc result is stored; if the condition is False, then the original value is retained.

It returns a single value. It returns the sine value for the angle passed inside the function.

```
# importing numpy as np
import numpy as np
# creating an numpy element
ele = np.pi / 4
# storing the sine value to the variable.
sine = np.sin(ele)
print(sine)
```

`0.7071067811865475`

In this program, we imported the numpy library and created an element for storing the value of **pi** by **2 and using** the numpy function called **np.pi** for using **pi**. Hence, the **pi** / **2** is stored in the variable named ele. This angle is passed inside the function called **sin()**. This sin function finds the appropriate sine value from the angle. Finally, we have printed the sine value.

```
# Importing numpy as np
import numpy as np
# Storing the sine value to the variable.
sine = np.sin(np.pi / 3)
print(sine)
```

`0.8660254037844386`

In this program, we found the sine value for the angle of pi / 3. Then, we passed the angle inside the sin function to get the sine value.

```
# Importing numpy as np
import numpy as np
# Creating an array for storing angles
arr = np.array((0, 30, 45, 60, 90))
np.pi/180
# Finding the sine values for the angles
sine = np.sin(arr)
print(sine)
```

`[ 0. -0.98803162 0.85090352 -0.30481062 0.89399666]`

In this program, we imported numpy and created a numpy array for storing the angles. The numpy array consists of angles of 0, 30, 45, 60, and 90. We found the sine values of these angles using the sin function. We passed an array inside the sin() function in this program.

That’s it for **np.sin()** function in **Python**.

The post np.sin: How to Use numpy sin() Function in Python appeared first on AppDividend.

]]>The np.hstack() is a numpy library function that stacks the arrays horizontally. Hstack stands for the horizontal stack. The resulting array will be single-dimensional if two single-dimensional arrays are horizontally stacked using a hstack() function. The resulting array has the elements from array 1 first and then has the elements from the second array appended at […]

The post np.hstack: How to Stack Array in Horizontal Direction appeared first on AppDividend.

]]>The **np.hstack()** is a **numpy** **library function** that stacks the arrays horizontally. **Hstack** stands for the **horizontal stack**. The resulting array will be single-dimensional if two single-dimensional arrays are horizontally stacked using a hstack() function. The resulting array has the elements from array 1 first and then has the elements from the second array appended at the end. It is similar to concatenating arrays with the axis is 1. This both results in the same result.

`numpy.hstack(tup)`

**tup**: It is the tuple that consists of arrays in which we want to perform the hstack function. In a single-dimensional array, the length of the arrays can be different, but in other cases, both arrays should have the same shape.

```
import numpy as np
# Creating an array named arr1
arr1 = np.array((1, 2, 3))
# Creating an array named arr2
arr2 = np.array((4, 5, 6))
# Printing the shape of the arr1
print(arr1.shape)
# Printing the shape of the arr2
print(arr2.shape)
# Creating the horizontally stacked array
res = np.hstack((arr1, arr2))
print(res)
```

```
(3,)
(3,)
[1 2 3 4 5 6]
```

In this program, we imported numpy for performing numerical operations. First, we created two numpy arrays using **np.array()**. It creates a numpy array. Then we printed the shape of the two numpy arrays using np.shape. This prints the dimensions of the two arrays.

Then, we horizontally stacked arrays **arr1** ans **arr2** using the hstack() function. It concatenates **arr1** and **arr2** horizontally.

```
# Importing Numpy as np
import numpy as np
# Creating an array named arr1
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
# Creating an array named arr2
arr2 = np.array([[7, 8, 9], [10, 11, 12]])
# Printing the shape of the arr1
print(arr1.shape)
# Printing the shape of the arr2
print(arr2.shape)
# Creating the horizontally stacked array
res = np.hstack((arr1, arr2))
print(res)
```

```
(2, 3)
(2, 3)
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
```

In this program, we created a multi-dimensional array. We have printed the shape of the two numpy arrays. Finally, we have horizontally stacked these multi-dimensional arrays.

That’s it for np.hstack() tutorial.

The post np.hstack: How to Stack Array in Horizontal Direction appeared first on AppDividend.

]]>The np.concatenate() is a numpy library function that joins two numpy arrays into a single numpy array. There are two types of concatenating the arrays. One is by concatenating using the axis 0, that is, by adding the second array at the end of the first array. Then another one is by concatenating the first array and […]

The post np.concatenate: How to Join Numpy Array in Python appeared first on AppDividend.

]]>The **np.concatenate() **is a numpy library function that joins two numpy arrays into a single numpy array. There are two types of concatenating the arrays. One is by concatenating using the axis 0, that is, by adding the second array at the end of the first array. Then another one is by concatenating the first array and second array by columns.

The columns of the second array will be joined at the end of the columns of the first array.

`numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")`

( a1, a2, …): This is the sequence of the arrays that we want to concatenate.

**axis**: It is the axis along which the arrays will be joined. By default, it is 0. It concatenates row-wise. If the axis is 1, then it is concatenated column-wise.

**out**: If this out is used, the shape must be correct.

**dtype**: The **dtype** stands for the data type. We can specify the data type in the dtype argument. The output array will have the data type provided in this argument. If nothing is provided, then it takes the data type as None.

**casting**: The **casting** consists of several castings – no, equiv, safe, same_kind, unsafe. Same_kind is set as the default.

```
# import Numpy as np
import numpy as np
# creating an numpy array named arr1
arr1 = np.array([1, 2, 3])
# creating an numpy array named arr2
arr2 = np.array([4, 5, 6])
# concatenating arr1 and arr2
res = np.concatenate((arr1, arr2))
print(res)
```

`[1 2 3 4 5 6]`

In this program, we imported numpy for working with numerical data. Then, we created two numpy arrays named **arr1** and **arr2**. Then we concatenated using a np.concatenate() function.

The **np.concatenate()** function concatenates **arr1** with **arr2**. Since no other arguments are passed, it will consider the default arguments.

Axis will be considered as 0. Both axis results in the same output in a single dimensional array, but in multi-dimensional arrays, the axis changes its result. In this program, the elements are added by rows.

Let’s see another example.

```
# Import Numpy as np
import numpy as np
# creating an numpy array named arr1
arr1 = np.array([[8, 7], [6, 5]])
# creating an numpy array named arr2
arr2 = np.array([[4, 3], [2, 1]])
# concatenating arr1 and arr2
res = np.concatenate((arr1, arr2), axis=1)
print(res)
```

```
[[8 7 4 3]
[6 5 2 1]]
```

In this program, we used the axis as 1. The program will concatenate the first row of the second array with the first row of the first array. Hence the columns from the first array are combined with the columns of the second array.

That’s it for this tutorial.

The post np.concatenate: How to Join Numpy Array in Python appeared first on AppDividend.

]]>