NumPy

np.arange  vs np.linspace

Both np.arange and np.linspace are used to create arrays with evenly spaced values, but they have different ways of specifying the intervals and the number of elements.

np.arange takes start, stop, and step as arguments:

import numpy as np
array = np.arange(start, stop, step)

 

start: The starting value of the range.
stop: The end value of the range (exclusive).
step: The difference between each consecutive value in the array.


np.linspace takes start, stop, and num as arguments:

import numpy as np
array = np.linspace(start, stop, num)

start: The starting value of the range.
stop: The end value of the range (inclusive).
num: The number of evenly spaced values to generate.

Here's a comparison of the two functions:

import numpy as np

# Using np.arange
array1 = np.arange(0, 10, 2)
print(array1)  # Output: [0 2 4 6 8]

# Using np.linspace
array2 = np.linspace(0, 10, 5)
print(array2)  # Output: [ 0.  2.5  5.  7.5 10. ]

In summary, np.arange is useful when you want to specify the step size between values, while np.linspace is useful when you want to specify the total number of values in the array.

np.exp vs math.exp

math.exp() and np.exp() are both functions used to calculate the exponential value of a number, but they belong to different libraries and have some differences in their usage:

math.exp(): This function is part of the built-in math module in Python. It takes a single number as input and returns the exponential value of that number. It works only with scalar values.

import math

x = 2
result = math.exp(x)
print(result)


np.exp(): This function is part of the NumPy library, which is widely used for numerical operations in Python. It can work with both scalar values and arrays. When given an array as input, it calculates the exponential value element-wise.

import numpy as np

x = np.array([1, 2, 3])
result = np.exp(x)
print(result)

In summary, if you're working with scalar values, you can use either math.exp() or np.exp(). However, if you're working with arrays or need more advanced numerical operations, it's better to use np.exp() from the NumPy library.

另外,也有纯数学的处理,

ou can use math.e with pow function in Python. math.e is a mathematical constant that represents Euler’s number, approximately equal to 2.71828. pow function is used to calculate the power of a number.

For example, if you want to calculate e raised to the power of x, you can use pow(math.e, x) or math.e ** x1.

 

np.maxium

import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([3, 2, 1])
result = np.maximum(array1, array2)
print(result)

This is a good example of how np.maximum works.

In this example, array1 is an array of [1, 2, 3] and array2 is an array of [3, 2, 1]. When we apply np.maximum(array1, array2), it returns an array of [3, 2, 3] which is the element-wise maximum of the two arrays.

The print(result) statement prints the resulting array to the console.

 

posted @ 2023-03-21 18:34  ChuckLu  阅读(18)  评论(0)    收藏  举报