"""
np.minimum(x1,x2) 逐元素比较两个数组 x1 和 x2,返回对应位置上较小的值。
"""
a = np.array([1,4,3])
b = np.array([2,2,5])
result = np.minimum(a, b)print(result)# 输出: [1 2 3]"""
np.maximum(x1, x2) 逐元素比较两个数组 x1 和 x2,返回对应位置上较大的值。
"""
a = np.array([1,4,3])
b = np.array([2,2,5])
result = np.minimum(a, b)print(result)# 输出: [2 4 5]"""
np.where(condition,x,y) 逐元素地根据条件 condition 判断,从 x 或 y 中选取元素构成结果数组。若条件为 True,则选择 x 中的值,否则选择 y 中的值。
"""
a = np.array([1,4,3])
b = np.array([2,2,5])
condition = a > b
result = np.where(condition, a, b)print(result)# 输出: [2 2 5]
numpy和pandas,torch的转换
"""
Pandas的DataFrame或者Series转numpy数组
"""
df = pd.DataFrame(data=[[1,2,3],[4,5,6]], columns=['a','b','c'])print(df)# a b c# 0 1 2 3# 1 4 5 6
a = df.to_numpy()print(a)# [[1 2 3]# [4 5 6]]"""
numpy转Pandas的Dataframe和Series直接当做多维数组即可
"""#numpy转Series
a = np.arange(4)print(a)# [0 1 2 3]
s = pd.Series(a)print(s)# 0 0# 1 1# 2 2# 3 3# dtype: int64#numpy转Dataframe
index =['A','B','C','D']
name ='sample'
s = pd.Series(data=a, index=index, name=name, dtype='float')print(s)# A 0.0# B 1.0# C 2.0# D 3.0# Name: sample, dtype: float64print(type(a))# <class 'numpy.ndarray'>"""
torch的tensor转numpy
"""import torch
x = torch.ones(5)# 创建张量x# tensor([1., 1., 1., 1., 1.])
x_ = x.detach().numpy()# 转换,使用detach()不计算张量# array([1., 1., 1., 1., 1.], dtype=float32)"""
numpy转tensor
"""
x=np.ones((3,4))type(x)#numpy.ndarray
x=torch.tensor(x)# 深拷贝
x=torch.from_numpy(x)#浅拷贝,共享numpy数组type(x)#torch.Tensor