task01

it2024-06-26  43

手敲一编代码后,有很多收获,但还没开来得及总结,随后会把总结补上,喜欢的可以点赞哈,稳重如有不对之处,欢迎斧正,谢谢

NAN = NaN = nan

import numpy as np print(np.nan == np.nan) print(np.nan != np.nan) False True 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] #numpy.count_nonzero是用于统计数组中非零元素的个数 z = np.count_nonzero(y) print(z) 1 print(np.pi) 3.141592653589793 print(np.e) 2.718281828459045 class dtype(object): def __init__(self, obj, align = False, copy = False): pass a = np.dtype('b1') print(a.type) print(a.itemsize) <class 'numpy.bool_'> 1 a = np.dtype('i1') print(a.type) print(a.itemsize) <class 'numpy.int8'> 1 a = np.dtype('i2') print(a.type) print(a.itemsize) <class 'numpy.int16'> 2 a = np.dtype('i4') print(a.type) print(a.itemsize) <class 'numpy.int32'> 4 a = np.dtype('i8') print(a.type) print(a.itemsize) <class 'numpy.int64'> 8 a = np.dtype('u1') print(a.type) print(a.itemsize) <class 'numpy.uint8'> 1 a = np.dtype('u2') print(a.type) print(a.itemsize) <class 'numpy.uint16'> 2 a = np.dtype('u4') print(a.type) print(a.itemsize) <class 'numpy.uint32'> 4 a = np.dtype('u8') print(a.type) print(a.itemsize) <class 'numpy.uint64'> 8 a = np.dtype('f2') print(a.type) print(a.itemsize) <class 'numpy.float16'> 2 a = np.dtype('f4') print(a.type) print(a.itemsize) <class 'numpy.float32'> 4 a = np.dtype('f8') print(a.type) print(a.itemsize) <class 'numpy.float64'> 8 a = np.dtype('S') print(a.type) print(a.itemsize) <class 'numpy.bytes_'> 0 a = np.dtype('S3') print(a.type) print(a.itemsize) <class 'numpy.bytes_'> 3 a = np.dtype('U3') print(a.type) print(a.itemsize) <class 'numpy.str_'> 12 class iinfo(object): def __init__(self, int_type): pass def min(self): pass def max(self): pass ii16 = np.iinfo(np.int16) print(ii16.min) print(ii16.max) -32768 32767 ii32 = np.iinfo(np.int32) print(ii32.min) print(ii32.max) -2147483648 2147483647 class finfo(object): def _init(self, dtype): pass ff16 = np.finfo(np.float16) print(ff16.bits) print(ff16.min) print(ff16.max) print(ff16.eps) 16 -65500.0 65500.0 0.000977 ff32 = np.finfo(np.float32) print(ff32.bits) print(ff32.min) print(ff32.max) print(ff32.eps) 32 -3.4028235e+38 3.4028235e+38 1.1920929e-07 a = np.datetime64('2020-03-01') print(a, a.dtype) 2020-03-01 datetime64[D] a = np.datetime64('2020-03') print(a, a.dtype) 2020-03 datetime64[M] a = np.datetime64('2020-03-01 20:00:02') print(a, a.dtype) 2020-03-01T20:00:02 datetime64[s] a = np.datetime64('2020-03-02 20') print(a, a.dtype) 2020-03-02T20 datetime64[h] a = np.datetime64('2020-03', 'D') print(a, a.dtype) 2020-03-01 datetime64[D] a = np.datetime64('2020-03', 'Y') print(a, a.dtype) 2020 datetime64[Y] print(np.datetime64('2020-03') == np.datetime64('2020-03-01')) True print(np.datetime64('2020-03') == np.datetime64('2020-03-02')) False 由上例可以看出,2019-03 和 2019-03-01 所表示的其实是同一个时间 #从字符串创建 datetime64 数组时,如果单位不统一,则一律转化成其中最小的单位。 a = np.array(['2020-03', '2020-03-05', '2020-03-08 20:00'], dtype = 'datetime64') print(a, a.dtype) ['2020-03-01T00:00' '2020-03-05T00:00' '2020-03-08T20:00'] datetime64[m] 使用arange()创建 datetime64 数组 import numpy as np a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64) print(a) #可以看出是一个日期范围 ['2020-08-01' '2020-08-02' '2020-08-03' '2020-08-04' '2020-08-05' '2020-08-06' '2020-08-07' '2020-08-08' '2020-08-09'] print(a.dtype) datetime64[D] a = np.arange('2020-08-01 20:00', '2020-08-10', dtype=np.datetime64) print(a) ['2020-08-01T20:00' '2020-08-01T20:01' '2020-08-01T20:02' ... '2020-08-09T23:57' '2020-08-09T23:58' '2020-08-09T23:59'] print(a.dtype) datetime64[m] a = np.arange('2020-05', '2020-12', dtype=np.datetime64) print(a) ['2020-05' '2020-06' '2020-07' '2020-08' '2020-09' '2020-10' '2020-11'] print(a.dtype) datetime64[M]

由以上结果可以看出,当表示日期范围时,是由第一个时间戳的最小单位来表示公差d的(等差数列)

timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的

a = np.datetime64('2020-03-08') - np.datetime64('2020-03-07') b = np.datetime64('2020-03-08') - np.datetime64('202-03-07 08:00') c = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 23:00', 'D') print(a, a.dtype) 1 days timedelta64[D] print(b, b.dtype) 956178240 minutes timedelta64[m] print(c, c.dtype) 1 days timedelta64[D] a = np.datetime64('2020-03') + np.timedelta64(20, 'D')#20表示天数 b = np.datetime64('2020-06-15 00:00') + np.timedelta64(12, 'h') print(a, a.dtype) 2020-03-21 datetime64[D] print(b, b.dtype) 2020-06-15T12:00 datetime64[m] #生成 timedelta64时,要注意年('Y')和月('M')这两个单位无法和其它单位进行运算 #这与闰年和一些月有31天而一些月仅有30天有关 a = np.timedelta64(1, 'Y') b = np.timedelta64(a, 'M') print(a) print(b) 1 years 12 months c = np.timedelta64(1, 'h') d = np.timedelta64(c, 'm') print(c) print(d) 1 hours 60 minutes print(np.timedelta64(a, 'D')) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-c779ad03d53f> in <module> ----> 1 print(np.timedelta64(a, 'D')) TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind' #关于timedelta64 的运算 a = np.timedelta64(1, 'Y') b = np.timedelta64(6, 'M') c = np.timedelta64(1, 'W') d = np.timedelta64(1, 'D') e = np.timedelta64(10, 'D') print(a) print(b) print(a + b) print(a - b) print(2 * a) print(a / b) print(c / d) print(c % e) 1 years 6 months 18 months 6 months 2 years 2.0 7.0 7 days #numpy.datetime64 与 datetime.datetime 相互转换 dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30) dt64 = np.datetime64(dt, 's')#s代表秒 print(dt64, dt64.dtype) 2020-06-01T20:05:30 datetime64[s] dt2 = dt64.astype(datetime.datetime) print(dt2, type(dt2)) 2020-06-01 20:05:30 <class 'datetime.datetime'> import numpy as np # # 2020-07-10 星期五 a = np.busday_offset('2020-07-10', offsets=1) print(a) 2020-07-13 a = np.busday_offset('2020-07-11', offsets=1) print(a) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-20-72bcd70d2740> in <module> ----> 1 a = np.busday_offset('2020-07-11', offsets=1) 2 print(a) <__array_function__ internals> in busday_offset(*args, **kwargs) ValueError: Non-business day date in busday_offset a = np.busday_offset('2020-07-11', offsets=0, roll='forward') b = np.busday_offset('2020-07-11', offsets=0, roll='backward') print(a) print(b) 2020-07-13 2020-07-10 a = np.busday_offset('2020-07-11', offsets=1, roll='forward') b = np.busday_offset('2020-07-11', offsets=1, roll='backward') print(a) print(b) 2020-07-14 2020-07-13 #返回指定日期是否是工作日。 # 2020-07-10 星期五 a = np.is_busday('2020-07-10') b = np.is_busday('2020-07-11') print(a) # True print(b) # False True False #统计工作日天数 begindates = np.datetime64('2020-07-10') enddates = np.datetime64('2020-07-20') a = np.arange(begindates, enddates, dtype='datetime64') b = np.count_nonzero(np.is_busday(a)) print(a) print(b) ['2020-07-10' '2020-07-11' '2020-07-12' '2020-07-13' '2020-07-14' '2020-07-15' '2020-07-16' '2020-07-17' '2020-07-18' '2020-07-19'] 6 #自定义周掩码值 a = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 1, 0, 0]) b = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 0, 0, 1]) print(a) print(b) True False 返回两个日期之间的工作日数量。 begindates = np.datetime64('2020-07-10') enddates = np.datetime64('2020-07-20') a = np.busday_count(begindates, enddates) b = np.busday_count(enddates, begindates) print(a) print(b) 6 -6

数组的创建

通过array()函数进行创建。 a = np.array([0, 1, 2, 3, 4]) b = np.array((0, 1, 2, 3, 4)) print(a, type(a)) print(b, type(b)) # 创建二维数组 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)) # 创建三维数组 d = np.array([[(1.5, 2, 3), (4, 5, 6)], [(3, 2, 1), (4, 5, 6)]]) print(d, type(d)) [0 1 2 3 4] <class 'numpy.ndarray'> [0 1 2 3 4] <class 'numpy.ndarray'> [[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'> [[[1.5 2. 3. ] [4. 5. 6. ]] [[3. 2. 1. ] [4. 5. 6. ]]] <class 'numpy.ndarray'> 通过asarray()函数进行创建 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)) print(y,type(y)) print(z,type(z)) [[1, 1, 1], [1, 1, 2], [1, 1, 1]] <class 'list'> [[1 1 1] [1 1 1] [1 1 1]] <class 'numpy.ndarray'> [[1 1 1] [1 1 1] [1 1 1]] <class 'numpy.ndarray'> x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) y = np.array(x) z = np.asarray(x) w = np.asarray(x, dtype=np.int) x[1][2] = 2 print(x,type(x),x.dtype) print(y,type(y),y.dtype) print(z,type(z),z.dtype) print(w,type(w),w.dtype) [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32 [[1 1 1] [1 1 1] [1 1 1]] <class 'numpy.ndarray'> int32 [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32 [[1 1 1] [1 1 2] [1 1 1]] <class 'numpy.ndarray'> int32 更改为较大的dtype时,其大小必须是array的最后一个axis的总大小(以字节为单位)的除数 x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) print(x, x.dtype) x.dtype = np.float [[1 1 1] [1 1 1] [1 1 1]] int32 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-32-2f02f2a984df> in <module> 2 print(x, x.dtype) 3 ----> 4 x.dtype = np.float ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array. 通过fromfunction()函数进行创建 def f(x, y): return 10 * x + y x = np.fromfunction(f, (5, 4), dtype=int) print(x) x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) print(x) x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) print(x) [[ 0 1 2 3] [10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43]] [[ True False False] [False True False] [False False True]] [[0 1 2] [1 2 3] [2 3 4]]

依据 ones 和 zeros 填充方式

#零数组 x = np.zeros(5) print(x) x = np.zeros([2, 3]) print(x) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.zeros_like(x) print(y) [0. 0. 0. 0. 0.] [[0. 0. 0.] [0. 0. 0.]] [[0 0 0] [0 0 0]] #1数组 x = np.ones(5) print(x) x = np.ones([2, 3]) print(x) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.ones_like(x) print(y) [1. 1. 1. 1. 1.] [[1. 1. 1.] [1. 1. 1.]] [[1 1 1] [1 1 1]] #空数组 x = np.empty(5) print(x) x = np.empty((3, 2)) print(x) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.empty_like(x) print(y) [1. 1. 1. 1. 1.] [[1. 1.] [1. 1.] [1. 1.]] [[0 0 0] [0 0 0]] #单位数组 x = np.eye(4) print(x) x = np.eye(2, 3) print(x) x = np.identity(4) print(x) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] [[1. 0. 0.] [0. 1. 0.]] [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] #对角数组 x = np.arange(9).reshape((3, 3)) print(x) print(np.diag(x)) print(np.diag(x, k=1)) print(np.diag(x, k=-1)) v = [1, 3, 5, 7] x = np.diag(v) print(x) [[0 1 2] [3 4 5] [6 7 8]] [0 4 8] [1 5] [3 7] [[1 0 0 0] [0 3 0 0] [0 0 5 0] [0 0 0 7]] #常数数组 x = np.full((2,), 7) print(x) x = np.full(2, 7) print(x) x = np.full((2, 7), 7) print(x) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.full_like(x, 7) print(y) [7 7] [7 7] [[7 7 7 7 7 7 7] [7 7 7 7 7 7 7]] [[7 7 7] [7 7 7]]

利用数值范围来创建ndarray

x = np.arange(5) print(x) x = np.arange(3, 7, 2) print(x) x = np.linspace(start=0, stop=2, num=9) print(x) x = np.logspace(0, 1, 5) print(np.around(x, 2)) x = np.linspace(start=0, stop=1, num=5) x = [10 ** i for i in x] print(np.around(x, 2)) x = np.random.random(5) print(x) x = np.random.random([2, 3]) print(x) [0 1 2 3 4] [3 5] [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] [ 1. 1.78 3.16 5.62 10. ] [ 1. 1.78 3.16 5.62 10. ] [0.66025066 0.82810975 0.10492026 0.80965395 0.20104655] [[0.33858738 0.446174 0.27837237] [0.15558581 0.77515961 0.9544448 ]]

结构数组的创建

#利用字典来定义结构 personType = np.dtype({ 'names': ['name', 'age', 'weight'], 'formats': ['U30', 'i8', 'f8']}) a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)], dtype=personType) print(a, type(a)) [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)] <class 'numpy.ndarray'> #利用包含多个元组的列表来定义结构 personType = np.dtype([('name', 'U30'), ('age', 'i8'), ('weight', 'f8')]) a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)], dtype=personType) print(a, type(a)) print(a[0]) print(a[-2:]) print(a['name']) print(a['age']) print(a['weight']) [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)] <class 'numpy.ndarray'> ('Liming', 24, 63.9) [('Mike', 15, 67. ) ('Jan', 34, 45.8)] ['Liming' 'Mike' 'Jan'] [24 15 34] [63.9 67. 45.8]

数组的属性

a = np.array([1, 2, 3, 4, 5]) print(a.shape) print(a.dtype) print(a.size) print(a.ndim) print(a.itemsize) b = np.array([[1, 2, 3], [4, 5, 6.0]]) print(b.shape) print(b.dtype) print(b.size) print(b.ndim) print(b.itemsize) (5,) int32 5 1 4 (2, 3) float64 6 2 8 a = np.array([1, 2, 3, 4, 5]) print(a) b = np.array([1, 2, 3, 4, '5']) print(b) c = np.array([1, 2, 3, 4, 5.0]) print(c) [1 2 3 4 5] ['1' '2' '3' '4' '5'] [1. 2. 3. 4. 5.]
最新回复(0)