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 ** x
1.
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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-03-21 Visual Studio, Nuget package sources, Order doesn't stick
2019-03-21 cat查看文件以及sed查看指定行数
2019-03-21 sql server database mail
2019-03-21 X-Pack for the Elastic Stack [6.2] » Securing the Elastic Stack »Setting Up User Authentication
2018-03-21 wox 快速搜索程序
2018-03-21 QTTabBar http://qttabbar.wikidot.com/
2018-03-21 git出错调试