def array(p_object, dtype=None, *args, **kwargs):
a=np.array([1,2,3],ndmin=2)
a=np.array([1,2,3],dtype=complex)
名称 | 描述 |
---|---|
bool_ | 布尔型数据类型(True 或者 False) |
int_ | 默认的整数类型(类似于 C 语言中的 long,int32 或 int64) |
intc | 与 C 的 int 类型一样,一般是 int32 或 int 64 |
intp | 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64) |
int8 | 字节(-128 to 127) |
int16 | 整数(-32768 to 32767) |
int32 | 整数(-2147483648 to 2147483647) |
int64 | 整数(-9223372036854775808 to 9223372036854775807) |
uint8 | 无符号整数(0 to 255) |
uint16 | 无符号整数(0 to 65535) |
uint32 | 无符号整数(0 to 4294967295) |
uint64 | 无符号整数(0 to 18446744073709551615) |
float_ | float64 类型的简写 |
float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位 |
float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位 |
float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位 |
complex_ | complex128 类型的简写,即 128 位复数 |
complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分) |
complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分) |
numpy 的数值类型实际上是 dtype 对象的实例,并对应唯一的字符,包括 np.bool_,np.int32,np.float32,等等。
x=np.empty([3],dtype=int)
print(x)
为随机值,未初始化
x=np.zeros([3,2],dtype=int)
print(x)
x=np.ones([3,2],dtype=int)
print(x)
import numpy as np
x=[[1,2,3],[2,3,2]]
a=np.asarray(x)
print(a.shape)
import numpy as np
str=b'hello'
a=np.frombuffer(str,dtype='S1')
print(a)
import numpy as np
list=range(20)
it=iter(list)
x=np.fromiter(it,dtype=int)
print(x)
x=np.arange(12,dtype=float)
print(x)
import numpy as np
x=np.linspace(0,9,10,dtype=int)
print(x)
import numpy as np
x=np.logspace(0,9,10,base=2)
print(x)
import numpy as np
x=np.arange(15)
a=slice(2,10,3)
print(x[a])
import numpy as np
x=np.arange(15)
a=x[2:12:3]
print(a)
import numpy as np
x=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(x[...,1])#第二列
print(x[1,...])#第二行
print(x[...,1:])#第二列及以下
import numpy as np
#获取(0,0),(1,1),(2,0)
x=np.array([[1,2],[3,4],[5,6]])
y=x[[0,1,2],[0,1,0]]
print(y)
/*
[1 4 5]
*/
import numpy as np
x=np.arange(1,50)
print(x[x>10])
/*
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
*/
import numpy as np
x=np.arange(32).reshape(4,8)
print(x[[0,2,1]])
/*
[[ 0 1 2 3 4 5 6 7]
[16 17 18 19 20 21 22 23]
[ 8 9 10 11 12 13 14 15]]
*/
import numpy as np
x=np.arange(6).reshape(3,2)
for i in np.nditer(x):
print(i,end=",")
import numpy as np
x=np.arange(6).reshape(3,2)
for i in np.nditer(x,order='F'):
print(i,end=",")
/*
0,2,4,1,3,5,
*/
import numpy as np
x=np.arange(6).reshape(3,2)
for i in np.nditer(x,order='C'):
print(i,end=",")
/*
0,1,2,3,4,5,
*/
import numpy as np
x=np.arange(0,60,5).reshape(3,4)
print(x)
for i in np.nditer(x,op_flags=['readwrite']):
i[...]=2*i
print(x)
import numpy as np
x=np.arange(0,60,5).reshape(3,4)
for i in np.nditer(x,flags=['external_loop'],order='F'):
print(i,end=',')
/*
[ 0 20 40],[ 5 25 45],[10 30 50],[15 35 55],
*/
import numpy as np
x=np.arange(0,60,5).reshape(3,4)
b=np.array([1,2,3,4],dtype=int)
for i,j in np.nditer([x,b]):
print(i,j)
import numpy as np
x=np.arange(0,60,5).reshape(3,4)
print(x)
for j in x.flat:
print(j,end=" ")
import numpy as np
x=np.arange(0,60,5).reshape(3,4)
print(np.transpose(x))