Numpy Python library

출처 :  http://chatgptai.mobi/


NumPy is a Python library that stands for “Numerical Python”. It is a powerful package for performing numerical computations and data analysis in Python. NumPy provides an array object, which is used to store and manipulate large amounts of data efficiently. The library contains a number of functions for mathematical operations, linear algebra, random number generation, and more.

NumPy is widely used in scientific computing, data analysis, machine learning, and other domains. Its array object is particularly useful for operations on large sets of data, as it can perform operations much faster than built-in Python lists. Additionally, many other libraries in Python, such as Pandas and Matplotlib, are built on top of NumPy, making it an essential library for many Python data analysis and scientific computing tasks.

The useful functions included in the Numpy library

NumPy provides a wide range of functions for numerical and scientific computing tasks. Here are some of the most commonly used functions in the NumPy library:

  1. np.array() – Creates a NumPy array from a Python list or tuple.
  2. np.arange() – Creates a NumPy array with a range of values.
  3. np.zeros() – Creates a NumPy array of all zeros.
  4. np.ones() – Creates a NumPy array of all ones.
  5. np.linspace() – Creates a NumPy array with a specified number of evenly spaced values within a range.
  6. np.random.rand() – Generates an array of random numbers between 0 and 1.
  7. np.random.randn() – Generates an array of random numbers from a normal distribution with mean 0 and standard deviation 1.
  8. np.min() – Returns the minimum value of an array.
  9. np.max() – Returns the maximum value of an array.
  10. np.mean() – Returns the mean value of an array.
  11. np.median() – Returns the median value of an array.
  12. np.std() – Returns the standard deviation of an array.
  13. np.dot() – Calculates the dot product of two arrays.
  14. np.transpose() – Transposes a matrix or array.
  15. np.linalg.inv() – Calculates the inverse of a matrix.

These are just a few examples of the functions provided by NumPy. There are many more functions available for numerical computations, linear algebra, Fourier analysis, and more.

How to use np.array() included in the Numpy library

The np.array() function in the NumPy library is used to create a NumPy array from a Python list or tuple. Here’s how you can use it:

import numpy as np

# Creating a NumPy array from a Python list
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array)

# Output: [1 2 3 4 5]

# Creating a NumPy array from a Python tuple
my_tuple = (6, 7, 8, 9, 10)
my_array = np.array(my_tuple)
print(my_array)

# Output: [ 6  7  8  9 10]

In the above example, we first import the NumPy library using import numpy as np. Then we create a Python list my_list and a Python tuple my_tuple. We use the np.array() function to create a NumPy array from both the list and tuple.

When we print the arrays using the print() function, we can see that the arrays are printed without commas, and the output is enclosed in square brackets. This is because NumPy arrays are multidimensional arrays, which can be one-dimensional or multi-dimensional.

We can also specify the data type of the array by passing the dtype argument to the np.array() function. For example, to create an array of integers, we can use np.array(my_list, dtype=int). Similarly, to create an array of floats, we can use np.array(my_list, dtype=float). By default, NumPy will try to infer the data type based on the input data.

How to use np.arange() included in the Numpy library

The np.arange() function in the NumPy library is used to create a NumPy array with a range of values. Here’s how you can use it:

import numpy as np

# Creating a NumPy array with a range of values
my_array = np.arange(0, 10)
print(my_array)

# Output: [0 1 2 3 4 5 6 7 8 9]

# Creating a NumPy array with a range of values and a step size
my_array = np.arange(0, 10, 2)
print(my_array)

# Output: [0 2 4 6 8]

In the above example, we first import the NumPy library using import numpy as np. Then we use the np.arange() function to create a NumPy array with a range of values. In the first example, we create an array with values ranging from 0 to 9. In the second example, we create an array with values ranging from 0 to 9 with a step size of 2.

When we print the arrays using the print() function, we can see that the arrays are printed without commas, and the output is enclosed in square brackets. This is because NumPy arrays are multidimensional arrays, which can be one-dimensional or multi-dimensional.

We can also specify the data type of the array by passing the dtype argument to the np.arange() function. For example, to create an array of integers, we can use np.arange(0, 10, dtype=int). Similarly, to create an array of floats, we can use np.arange(0, 10, dtype=float). By default, NumPy will create an array of integers if the step size is an integer, and an array of floats if the step size is a float.

How to use np.zeros() included in the Numpy library

The np.zeros() function in the NumPy library is used to create a NumPy array of all zeros. Here’s how you can use it:

import numpy as np

# Creating a NumPy array of all zeros
my_array = np.zeros(5)
print(my_array)

# Output: [0. 0. 0. 0. 0.]

# Creating a two-dimensional NumPy array of all zeros
my_array = np.zeros((3, 4))
print(my_array)

# Output: 
# [[0. 0. 0. 0.]
#  [0. 0. 0. 0.]
#  [0. 0. 0. 0.]]

In the above example, we first import the NumPy library using import numpy as np. Then we use the np.zeros() function to create a NumPy array of all zeros. In the first example, we create a one-dimensional array of length 5. In the second example, we create a two-dimensional array of size 3×4, i.e., with 3 rows and 4 columns.

When we print the arrays using the print() function, we can see that the arrays are printed with decimal points, indicating that the array data type is float. By default, NumPy will create an array of float data type. However, we can specify the data type by passing the dtype argument to the np.zeros() function. For example, to create an array of integers, we can use np.zeros(5, dtype=int). Similarly, to create an array of complex numbers, we can use np.zeros(5, dtype=complex).

How to use np.linspace() included in the Numpy library

The np.linspace() function in the NumPy library is used to create a NumPy array of evenly spaced numbers over a specified interval. Here’s how you can use it:

import numpy as np

# Creating a NumPy array of 5 evenly spaced numbers between 0 and 1
my_array = np.linspace(0, 1, 5)
print(my_array)

# Output: [0.   0.25 0.5  0.75 1.  ]

# Creating a NumPy array of 10 evenly spaced numbers between -5 and 5
my_array = np.linspace(-5, 5, 10)
print(my_array)

# Output: [-5.         -3.88888889 -2.77777778 -1.66666667 -0.55555556
#          0.55555556  1.66666667  2.77777778  3.88888889  5.        ]

In the above example, we first import the NumPy library using import numpy as np. Then we use the np.linspace() function to create a NumPy array of evenly spaced numbers over a specified interval. In the first example, we create a one-dimensional array of length 5 with values ranging from 0 to 1, inclusive. In the second example, we create a one-dimensional array of length 10 with values ranging from -5 to 5, inclusive.

When we print the arrays using the print() function, we can see that the arrays are printed with decimal points, indicating that the array data type is float. By default, NumPy will create an array of float data type. However, we can specify the data type by passing the dtype argument to the np.linspace() function. For example, to create an array of integers, we can use np.linspace(0, 10, 5, dtype=int). Similarly, to create an array of complex numbers, we can use np.linspace(0, 1, 5, dtype=complex).

Note that the third argument of np.linspace() specifies the number of samples to generate between the specified interval, inclusive of the start and end points. If not specified, it defaults to 50.

How to use np.random.rand() included in the Numpy library

The np.random.rand() function in the NumPy library is used to generate a NumPy array of random numbers between 0 and 1 with a uniform distribution. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array of 5 random numbers between 0 and 1
my_array = np.random.rand(5)
print(my_array)

# Output: [0.29461723 0.40697375 0.49622607 0.0521665  0.91825415]

# Creating a two-dimensional NumPy array of shape (3, 4) with random numbers between 0 and 1
my_array = np.random.rand(3, 4)
print(my_array)

# Output: 
# [[0.44421364 0.69337664 0.82152286 0.92848914]
#  [0.15782612 0.69702609 0.44547194 0.52579323]
#  [0.86169609 0.65519224 0.57594054 0.01202163]]

In the above example, we first import the NumPy library using import numpy as np. Then we use the np.random.rand() function to create a NumPy array of random numbers between 0 and 1 with a uniform distribution. In the first example, we create a one-dimensional array of length 5. In the second example, we create a two-dimensional array of shape (3, 4), i.e., with 3 rows and 4 columns.

When we print the arrays using the print() function, we can see that the arrays contain random numbers between 0 and 1. The specific numbers generated will be different each time the code is run, as they are generated randomly.

We can also generate arrays of random numbers with other distributions, such as the normal distribution, by using other functions in the np.random module, such as np.random.randn() or np.random.normal().

How to use np.random.randn() included in the Numpy library

The np.random.randn() function in the NumPy library is used to generate a NumPy array of random numbers from a standard normal distribution (i.e., with mean 0 and variance 1). Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array of 5 random numbers from a standard normal distribution
my_array = np.random.randn(5)
print(my_array)

# Output: [-0.18655349 -1.16030916 -1.19593106  0.63083647 -0.16328998]

# Creating a two-dimensional NumPy array of shape (3, 4) with random numbers from a standard normal distribution
my_array = np.random.randn(3, 4)
print(my_array)

# Output: 
# [[ 0.5685866  -0.65194766 -0.70227899  1.17296314]
#  [ 0.78590669 -0.23558005 -0.4055979   0.38966559]
#  [ 1.12203434  0.1733774  -1.67767811 -0.68302184]]
import numpy as np

# Creating a one-dimensional NumPy array of 5 random numbers from a standard normal distribution
my_array = np.random.randn(5)
print(my_array)

# Output: [-0.18655349 -1.16030916 -1.19593106  0.63083647 -0.16328998]

# Creating a two-dimensional NumPy array of shape (3, 4) with random numbers from a standard normal distribution
my_array = np.random.randn(3, 4)
print(my_array)

# Output: 
# [[ 0.5685866  -0.65194766 -0.70227899  1.17296314]
#  [ 0.78590669 -0.23558005 -0.4055979   0.38966559]
#  [ 1.12203434  0.1733774  -1.67767811 -0.68302184]]

In the above example, we first import the NumPy library using import numpy as np. Then we use the np.random.randn() function to create a NumPy array of random numbers from a standard normal distribution. In the first example, we create a one-dimensional array of length 5. In the second example, we create a two-dimensional array of shape (3, 4), i.e., with 3 rows and 4 columns.

When we print the arrays using the print() function, we can see that the arrays contain random numbers from a standard normal distribution. The specific numbers generated will be different each time the code is run, as they are generated randomly.

We can also generate arrays of random numbers from other normal distributions, with different means and variances, by using the np.random.normal() function. We can specify the mean and standard deviation of the normal distribution using the loc and scale parameters, respectively.

How to use np.min() included in the Numpy library

The np.min() function in the NumPy library is used to find the minimum value of an array. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array
my_array = np.array([5, 2, 9, 1, 7])

# Finding the minimum value of the array using np.min()
min_value = np.min(my_array)
print(min_value)

# Output: 1

In the above example, we first import the NumPy library using import numpy as np. Then we create a one-dimensional NumPy array my_array with 5 elements.

To find the minimum value of the array, we use the np.min() function and pass the array as the argument. The function returns the minimum value of the array, which we store in the variable min_value.

When we print min_value using the print() function, we can see that it contains the minimum value of the array, which is 1 in this case.

We can also find the minimum value of a multi-dimensional array by specifying the axis parameter of the np.min() function. The axis parameter specifies the axis along which to perform the operation. For example, to find the minimum value of each column of a 2D array, we can set axis=0. To find the minimum value of each row, we can set axis=1. If axis is not specified, the function returns the minimum value of the flattened array.

How to use np.max() included in the Numpy library

The np.max() function in the NumPy library is used to find the maximum value of an array. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array
my_array = np.array([5, 2, 9, 1, 7])

# Finding the maximum value of the array using np.max()
max_value = np.max(my_array)
print(max_value)

# Output: 9

In the above example, we first import the NumPy library using import numpy as np. Then we create a one-dimensional NumPy array my_array with 5 elements.

To find the maximum value of the array, we use the np.max() function and pass the array as the argument. The function returns the maximum value of the array, which we store in the variable max_value.

When we print max_value using the print() function, we can see that it contains the maximum value of the array, which is 9 in this case.

We can also find the maximum value of a multi-dimensional array by specifying the axis parameter of the np.max() function. The axis parameter specifies the axis along which to perform the operation. For example, to find the maximum value of each column of a 2D array, we can set axis=0. To find the maximum value of each row, we can set axis=1. If axis is not specified, the function returns the maximum value of the flattened array.

How to use np.mean() included in the Numpy library

The np.mean() function in the NumPy library is used to find the arithmetic mean of an array along a specified axis. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array
my_array = np.array([5, 2, 9, 1, 7])

# Finding the mean value of the array using np.mean()
mean_value = np.mean(my_array)
print(mean_value)

# Output: 4.8

In the above example, we first import the NumPy library using import numpy as np. Then we create a one-dimensional NumPy array my_array with 5 elements.

To find the mean value of the array, we use the np.mean() function and pass the array as the argument. The function returns the mean value of the array, which we store in the variable mean_value.

When we print mean_value using the print() function, we can see that it contains the mean value of the array, which is 4.8 in this case.

We can also find the mean value of a multi-dimensional array by specifying the axis parameter of the np.mean() function. The axis parameter specifies the axis along which to perform the operation. For example, to find the mean value of each column of a 2D array, we can set axis=0. To find the mean value of each row, we can set axis=1. If axis is not specified, the function returns the mean value of the flattened array.

How to use np.median() included in the Numpy library

The np.median() function in the NumPy library is used to find the median value of an array along a specified axis. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array
my_array = np.array([5, 2, 9, 1, 7])

# Finding the median value of the array using np.median()
median_value = np.median(my_array)
print(median_value)

# Output: 5.0

In the above example, we first import the NumPy library using import numpy as np. Then we create a one-dimensional NumPy array my_array with 5 elements.

To find the median value of the array, we use the np.median() function and pass the array as the argument. The function returns the median value of the array, which we store in the variable median_value.

When we print median_value using the print() function, we can see that it contains the median value of the array, which is 5.0 in this case.

We can also find the median value of a multi-dimensional array by specifying the axis parameter of the np.median() function. The axis parameter specifies the axis along which to perform the operation. For example, to find the median value of each column of a 2D array, we can set axis=0. To find the median value of each row, we can set axis=1. If axis is not specified, the function returns the median value of the flattened array.

How to use np.std() included in the Numpy library

The np.std() function in the NumPy library is used to find the standard deviation of an array along a specified axis. Here’s how you can use it:

import numpy as np

# Creating a one-dimensional NumPy array
my_array = np.array([5, 2, 9, 1, 7])

# Finding the standard deviation of the array using np.std()
std_value = np.std(my_array)
print(std_value)

# Output: 2.9154759474226504

How to use np.dot() included in the Numpy library

The np.dot() function in the NumPy library is used to compute the dot product of two arrays. It takes two arrays as input and returns a new array that is the dot product of the input arrays.

Here’s an example of how to use np.dot() function:

import numpy as np

# create two arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# compute the dot product of a and b
c = np.dot(a, b)

print(c)

In this example, we create two 2-dimensional arrays a and b using NumPy’s np.array() function. We then use the np.dot() function to compute the dot product of a and b, and store the result in c. Finally, we print the result.

The output of this program will be:

[[19 22]
 [43 50]]

To compute the dot product of two matrices using np.dot(), the number of columns in the first matrix must be equal to the number of rows in the second matrix. In the example above, a is a 2×2 matrix and b is also a 2×2 matrix, so the dot product of a and b can be computed using np.dot(a, b). If the dimensions of a and b were not compatible for matrix multiplication, np.dot() would raise a ValueError exception.

How to use np.transpose() included in the Numpy library

The np.transpose() function in the NumPy library is used to reverse or permute the axes of an array. It takes an array as input and returns a new array with the axes transposed.

Here’s an example of how to use np.transpose() function:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# transpose the array
arr_transpose = np.transpose(arr)

print(arr_transpose)

In this example, we create a 2D array arr using NumPy’s np.array() function. We then use the np.transpose() function to transpose the array and store the result in arr_transpose. Finally, we print the result.

The output of this program will be:

[[1 4]
 [2 5]
 [3 6]]

In this case, the original array arr had two rows and three columns. The transpose of arr has three rows and two columns, and the values of each row and column have been swapped.

We can also use the T attribute of an array to transpose it. For example:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# transpose the array using T attribute
arr_transpose = arr.T

print(arr_transpose)

This will produce the same output as the previous example.

By default, np.transpose() reverses the order of the dimensions of the array. However, we can also specify the order of the new dimensions using the axes parameter. For example, to swap the first and second dimensions of an array, we can use np.transpose(arr, axes=(1, 0)).

How to use np.linalg.inv() included in the Numpy library

The np.linalg.inv() function in the NumPy library is used to compute the inverse of a square matrix. It takes a square matrix as input and returns the inverse of that matrix.

Here’s an example of how to use np.linalg.inv() function:

import numpy as np

# create a 2x2 matrix
A = np.array([[1, 2], [3, 4]])

# compute the inverse of A
A_inv = np.linalg.inv(A)

print(A_inv)

In this example, we create a 2×2 matrix A using NumPy’s np.array() function. We then use the np.linalg.inv() function to compute the inverse of A and store the result in A_inv. Finally, we print the result.

The output of this program will be:

[[-2.   1. ]
 [ 1.5 -0.5]]

In this case, the inverse of the matrix A is another 2×2 matrix. We can use the np.dot() function to verify that the inverse was computed correctly:

I = np.dot(A, A_inv)
print(I)

This will produce the 2×2 identity matrix:

[[1. 0.]
 [0. 1.]]

If the matrix is not invertible, np.linalg.inv() will raise a LinAlgError exception.

It is worth noting that np.linalg.inv() should be used with caution, especially for matrices with large dimensions, as the computation of the inverse can be computationally expensive and numerically unstable. In many cases, it may be better to use other methods for solving linear systems, such as matrix decompositions or iterative methods.

How to use shape included in the Numpy library

The shape attribute in the NumPy library is used to determine the shape of an array. It returns a tuple representing the dimensions of the array along each axis.

Here’s an example of how to use the shape attribute:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# print the shape of the array
print(arr.shape)

In this example, we create a 2D array arr using NumPy’s np.array() function. We then use the shape attribute to print the shape of the array.

The output of this program will be:

(2, 3)

In this case, the shape of the arr array is (2, 3), which means it has two rows and three columns.

We can also use the reshape() function to change the shape of an array. For example:

import numpy as np

# create a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])

# reshape the array to a 2D array
arr_reshaped = arr.reshape(2, 3)

# print the new shape of the array
print(arr_reshaped.shape)

This will produce the output:

(2, 3)

In this case, we start with a 1D array arr with six elements, and use the reshape() function to convert it to a 2D array with two rows and three columns. We then use the shape attribute to print the new shape of the array.

How to use size included in the Numpy library

The size attribute in the NumPy library is used to determine the number of elements in an array. It returns an integer representing the total number of elements in the array.

Here’s an example of how to use the size attribute:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# print the size of the array
print(arr.size)

In this example, we create a 2D array arr using NumPy’s np.array() function. We then use the size attribute to print the number of elements in the array.

The output of this program will be:

6

In this case, the arr array contains 6 elements, so the size attribute returns the value 6.

We can also use the reshape() function to change the size of an array. For example:

import numpy as np

# create a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])

# reshape the array to a 2D array
arr_reshaped = arr.reshape(2, 3)

# print the new size of the array
print(arr_reshaped.size)

This will produce the output:

6

In this case, we start with a 1D array arr with six elements, and use the reshape() function to convert it to a 2D array with two rows and three columns. We then use the size attribute to print the new size of the array, which is still 6 since the total number of elements has not changed.

How to use argmax() included in the Numpy library

The argmax() function in NumPy is used to return the indices of the maximum values along an axis. It is useful for finding the index of the maximum value in a NumPy array.

Here’s an example of how to use argmax():

import numpy as np

# create a 2D array
arr = np.array([[4, 2, 6], [1, 5, 3]])

# find the index of the maximum value
max_index = np.argmax(arr)

print(max_index)   # Output: 2

In this example, we create a 2D NumPy array arr and use the argmax() function to find the index of the maximum value in the array. The output of the argmax() function is the index of the maximum value along the flattened array.

Alternatively, you can use the axis parameter to find the index of the maximum value along a specific axis. For example:

import numpy as np

# create a 2D array
arr = np.array([[4, 2, 6], [1, 5, 3]])

# find the index of the maximum value along axis=0
max_index = np.argmax(arr, axis=0)

print(max_index)   # Output: [0 1 0]

In this example, we use the axis parameter to find the index of the maximum value along the columns of the array. The output of the argmax() function is an array with the index of the maximum value for each column.

Note that if there are multiple occurrences of the maximum value in the array, argmax() will return the index of the first occurrence.

How to use T included in the Numpy library

In NumPy, the T attribute of an array can be used to get the transpose of a matrix. The transpose of a matrix is obtained by interchanging the rows and columns of the original matrix.

Here’s an example of how to use T:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# get the transpose of the array
arr_T = arr.T

print(arr_T)

In this example, we create a 2D NumPy array arr and use the T attribute to get its transpose. The output of the T attribute is a new array with the rows and columns of the original array interchanged.

You can also use the transpose() function in NumPy to get the transpose of an array. For example:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# get the transpose of the array
arr_T = np.transpose(arr)

print(arr_T)

This code produces the same output as the previous example. Note that the transpose() function can also be used to permute the axes of an array in a specific order, using the axes parameter.

How to use reshape included in the Numpy library

The reshape() function in NumPy is used to change the shape of an array without changing its data. It returns a new array with the same data but with a new shape.

Here’s an example of how to use reshape():

import numpy as np

# create a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])

# reshape the array to a 2D array with 3 rows and 2 columns
arr_2d = arr.reshape((3, 2))

print(arr_2d)

In this example, we create a 1D NumPy array arr and use the reshape() function to convert it to a 2D array with 3 rows and 2 columns. The output of the reshape() function is a new array with the same data as arr but with a new shape.

Note that the new shape should have the same number of elements as the original shape. In this example, the original shape had 6 elements, so we could reshape it to a 2D array with 3 rows and 2 columns, but we couldn’t reshape it to a 2D array with 2 rows and 3 columns, because that would have a different number of elements.

You can also use the reshape() function to change the shape of a multi-dimensional array. For example:

import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# reshape the array to a 1D array
arr_1d = arr.reshape((-1,))

print(arr_1d)

This code reshapes the 2D array arr to a 1D array by passing -1 as the new shape parameter. This tells NumPy to infer the new shape based on the original shape and the number of elements. In this case, the new shape is (6,), which is a 1D array with 6 elements.

How to use np.sort included in the Numpy library

The np.sort() function in NumPy is used to return a sorted copy of an array. By default, it sorts the array in ascending order along the last axis.

Here’s an example of how to use np.sort():

import numpy as np

# create an array
arr = np.array([3, 2, 1, 5, 4])

# sort the array in ascending order
sorted_arr = np.sort(arr)

print(sorted_arr)

In this example, we create a NumPy array arr and use the np.sort() function to return a sorted copy of the array. The sorted copy is stored in the variable sorted_arr. The output of the program is the sorted array [1 2 3 4 5].

By default, np.sort() sorts the array in ascending order along the last axis. This means that if the array is multi-dimensional, it will sort each sub-array along its last axis. For example:

import numpy as np

# create a 2D array
arr = np.array([[3, 2, 1], [6, 5, 4]])

# sort the array along the last axis (axis=-1) in ascending order
sorted_arr = np.sort(arr, axis=-1)

print(sorted_arr)

In this example, we create a 2D NumPy array arr and use the np.sort() function to sort each sub-array along its last axis (i.e. each row) in ascending order. The sorted copy is stored in the variable sorted_arr. The output of the program is the sorted array:

[[1 2 3]
 [4 5 6]]

You can also use the np.sort() function to sort the array in descending order by passing the kind='mergesort' parameter. For example:

import numpy as np

# create an array
arr = np.array([3, 2, 1, 5, 4])

# sort the array in descending order
sorted_arr_desc = np.sort(arr)[::-1]

print(sorted_arr_desc)

In this example, we create a NumPy array arr and use the np.sort() function to return a sorted copy of the array in ascending order. We then use the slice notation [::-1] to reverse the order of the sorted copy and obtain a copy of the array sorted in descending order. The output of the program is the sorted array [5 4 3 2 1].

How to use sin, cos, tan, arcsin, arccos, arctan included in the Numpy library

NumPy library provides trigonometric functions such as sin, cos, and tan, as well as their inverse counterparts, arcsin, arccos, and arctan.

Here’s an example of how to use these functions:

import numpy as np

# create an array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2])

# calculate the sin of each angle
sine = np.sin(angles)
print("sine of angles:", sine)

# calculate the cos of each angle
cosine = np.cos(angles)
print("cosine of angles:", cosine)

# calculate the tan of each angle
tangent = np.tan(angles)
print("tangent of angles:", tangent)

# calculate the arcsin of each angle
arcsine = np.arcsin(sine)
print("arcsine of sine:", arcsine)

# calculate the arccos of each angle
arccosine = np.arccos(cosine)
print("arccosine of cosine:", arccosine)

# calculate the arctan of each angle
arctangent = np.arctan(tangent)
print("arctangent of tangent:", arctangent)

In this example, we first create an array of angles in radians. We then use the np.sin()np.cos(), and np.tan() functions to calculate the sin, cos, and tan of each angle, respectively. We also use the np.arcsin()np.arccos(), and np.arctan() functions to calculate the arcsin, arccos, and arctan of the sin, cos, and tan values, respectively. The output of the program is:

sine of angles: [0.         0.70710678 1.        ]
cosine of angles: [1.000000e+00 7.071068e-01 6.123234e-17]
tangent of angles: [0.00000000e+00 1.00000000e+00 1.63312394e+16]
arcsine of sine: [0.         0.78539816 1.57079633]
arccosine of cosine: [0.         0.78539816 1.57079633]
arctangent of tangent: [0.         0.78539816 1.57079633]

Note that the np.arcsin()np.arccos(), and np.arctan() functions return values in radians. If you want the output in degrees, you can use the np.degrees() function to convert the output. For example:

# convert arcsine values to degrees
arcsine_degrees = np.degrees(arcsine)
print("arcsine of sine (in degrees):", arcsine_degrees)

The output of this code will be:

arcsine of sine (in degrees): [ 0. 45. 90.]

댓글

이 블로그의 인기 게시물

이클립스 플러그인으로 기능확장!

Neuralink: Connecting the Brain and Computer

Simulation Universe Theory