Task 01数据类型及数组创建

it2023-09-11  72

1 常量

numpy.nan 表示空值。

nan = NaN = NAN

两个numpy.nan是不相等的。

import numpy as np print(np.nan as np) # False print(np.nan !=np.nan) #True

numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.

import numpy as np x = np.array([1, 1, 8, np.nan, 10]) print(x) #[ 1. 1. 8. nan 10.] y = np.isnan(x) print(y) #[False False False True False] z = np.count_nonzero(y) print(z) #1

numpy.inf 表示正无穷大。

Inf = inf = infty =Infinity = PINF import numpy as np print(np.inf) #inf

numpy.pi 表示圆周率。

import numpy as np print(np.pi) #3.141592653589793

numpy.e 表示自然对数。

import numpy as np print(np.e) #2.718281828459045

2 数据类型

numpy支持的数据类型比Python内置的类型要多很多,基本上可以和C语言的数据类型对应上,其中部分类型对应位Python内置的类型。

类型说明bool_bool8布尔类型(8位)int8=byte整型(8位)int16=short整型(16位)int32=intc整型(32位)int_=int64=long=int0=intp整型(64位)uint8=ubyte无符号整型(8位)uint16=ushort无符号整型(16位)uint32=uintc无符号整型(32位)uint64=uintp=uint0=uint无符号整型(64位)float16=half浮点型(16位)float32=single浮点型(32位)float_=float64=double浮点型(64位)str_=unicode_=str0=unicodeUnicode字符串datetime64日期时间类型timedelta64表示两个时间之间的间隔

创建数据类型 numpy的数值类型实际上是dtype对象的实例。

class dtype(object): def _init_(self, obj, align=False, copy=False): pass

object-要转换为的数据类型对象。 align-如果为true,填充字段使其类似C的结构体。 copy-复制dtype对象,如果为false,则是对内置数据类型对象的引用。 每个内建类型都有一个唯一定义它的字符代码,如下:

字符代码对应类型bboolean(b1)isigned integer(i1,i2,i4,i8)uunsigned integer(u1,u2,u4,u8)ffloating-point(f2,f4,f8)mtimedelta64(表示两个时间之间的间隔)Mdatetime64(日期时间类型)OobjectS(byte-)string(S3表示长度为3的字符串)Uunicode(Unicode字符串)Vvoid

实例.创建一个布尔类型的变量:

import numpy as np a = np.dtype('b1') print(a.type) #<class 'numpy.bool_'> print(a.itemsize) #1

数据类型信息 python的浮点数通常是64位浮点数,几乎等同np.float64。 Machine limits for floating point types.

class finfo(object): def _init(self, dtype):

实例:

import numpy as np ff16 = np.finfo(np.float16) print(ff16.bits) #16 print(ff16.min) #-65500.0 print(ff16.max) #65500.0 print(ff16.eps) #0.000977

3 数组的创建

numpy提供的最重要的数据结构是ndarray,它是python中list的拓展。 在array()函数中:

def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0):

实例:

import numpy as np # 创建一维数组 a = np.array)[0,1,2,3,4]) b = np.array([0,1,2,3,4]) print(a, type(a)) #[0 1 2 3 4] <class 'numpy.ndarray'> print(b, type(b)) #[0 1 2 3 4] <class 'numpy.ndarray'> # 创建二维数组 c = np.array([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]]) print(c, type(c)) # [[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]] <class 'numpy.ndarray'>

array() 和 asarray() 都可以将结构数据转化为 ndarray,但是 array() 和 asarray() 主要区别就是当数据源是ndarray 时, array() 仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 asarray() 不会。 在assarray()函数中:

def asarray(a, dtype=None, order=None): return array(a, dtype, copy=False, order=order)

实例:

import numpy as np x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] y = np.array(x) z = np.asarray(x) x[1][2] = 2 print(x,type(x)) # [[1, 1, 1], [1, 1, 2], [1, 1, 1]] <class 'list'> print(y,type(y)) # [[1 1 1] # [1 1 1] # [1 1 1]] <class 'numpy.ndarray'> print(z,type(z)) # [[1 1 1] # [1 1 1] # [1 1 1]] <class 'numpy.ndarray'>

在fromfunction()中:

def fromfunction(function, shape, **kwargs):

实例:

import numpy as np def f(x, y): return 10 * x + y x = np.fromfunction(f, (5, 4), dtype=int) print(x) # [[ 0 1 2 3] # [10 11 12 13] # [20 21 22 23] # [30 31 32 33] # [40 41 42 43]] x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) print(x) # [[ True False False] # [False True False] # [False False True]] x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) print(x) # [[0 1 2] # [1 2 3] # [2 3 4]]

依据ones和zeros填充方式 (a)零数组 1 zeros()函数:返回给定形状和类型的零数组。 2 zeros_like()函数:返回与给定数组形状和类型相同的零数组。

def zeros(shape, dtype=None, order='C'): def zeros_like(a, dtype=None, order='K', subok=Ture, shape=None):

实例:

import numpy as np x = np.zeros(5) print(x) #[0. 0. 0. 0. 0.] x = np.zeros([2, 3]) print(x) # [[0. 0. 0.] # [0. 0. 0.]] x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.zeros_like(x) print(y) # [[0 0 0] # [0 0 0]]

©空数组 1 empty()函数:返回一个空数组,数组元素为随机数。 2 empty_like函数:返回与给定数组具有相同形状和类型的新数组。

def empty(shape, dtype=None, order='C'): def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):

实例:

import numpy as np x = np.empty(6) print(x) # [6.23042070e-307 3.56043053e-307 1.37961641e-306 9.79094970e-307 # 1.78021527e-306 1.60218763e-306] x = np.empty((3,2)) print(x) # [[6.23042070e-307 3.56043053e-307] # [1.37961641e-306 9.79094970e-307] # [1.78021527e-306 1.37962185e-306]] x = np.array([[1,2,3] [4,5,6]]) y = np.empty_like(x) print(y) # [[ 538976288 538976288 661783849] # [1912602624 61 12914]]
最新回复(0)