首页 > 其他分享 >02Array以及创建方法和操作函数

02Array以及创建方法和操作函数

时间:2023-01-20 18:01:54浏览次数:54  
标签:10 02Array 函数 ------------------------------------------ 创建 np ones print like

image-20230120142038256

image-20230120142115797

image-20230120142133419

import numpy as np

x1 = np.array([1,2,3,4,5,6,7,8])
x2 = np.array(
    [
        [1,2,3,4],
        [5,6,7,8]
    ])
#返回元组,表维度
print(x1.shape)
print(x2.shape)
#返回数字,表维度
print(x1.ndim)
print(x2.ndim)
#返回数字,表元素个数
print(x1.size)
print(x2.size)
#返回数据类型
print(x1.dtype)
print(x2.dtype)
print('------------------------------------------')


#创建一维数组(eg:从1开始,10结束,步长为2---包括1但不包括10)
x3 = np.arange(1,10,2)
print(x3)
print('------------------------------------------')


#创建一个全是1的数组
x4 = np.ones(10)#长度为10
x5 = np.ones((2,3))#维度为2*3
print(x4)
print(x5)
#拷贝ones创造的数组
x6 = np.ones_like(x4)
x7 = np.ones_like(x5)
print(x6)
print(x7)
print('------------------------------------------')


#同ones,但全为0
x8 = np.zeros((2,3))
x9 = np.zeros_like(x8)
print(x8)
print(x9)
print('------------------------------------------')


#同ones,但未初始化
y1 = np.empty((2,3))
y2 = np.empty_like(y1)
print(y1)
print(y2)
print('------------------------------------------')


#同ones,但可指定值
y3 = np.full((2,3),666)
y4 = np.full_like(y3,666)#注:此为创建一个形状相同的数组,需自行设置初始值
print(y3)
print(y4)
print('------------------------------------------')


#生成随机数组,案例如下
a1 = np.random.randn()
a2 = np.random.randn(3)
a3 = np.random.randn(3,2)
a4 = np.random.randn(3,2,4)
print(a1)
print(a2)
print(a3)
print(a4)
print('------------------------------------------')


A=np.arange(10)
print(A.reshape(2,5))
print(A+1)
print(A*3)
print(np.sin(A))
print(np.exp(A))
B=np.full_like(A,6)
print(A+B)
print(A-B)
print(A/B)
print(A*B)

标签:10,02Array,函数,------------------------------------------,创建,np,ones,print,like
From: https://www.cnblogs.com/lyc2001/p/17062962.html

相关文章