numpy学习1-1
https://www.runoob.com/numpy/numpy-dtype.html 1.1 numpy.nan
表示空值。【例】两个 numpy.nan 是不相等的。 import numpy as np print(np.nan == np.nan) # False print(np.nan != np.nan) # True. numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array. 对NaN逐个元素进行测试,并以布尔数组返回结果。 Boolean(布尔) 对象 Boolean(布尔)对象用于将非布尔值转换为布尔值(true 或者 false)。 var myBoolean=new Boolean(); 结果不是false就是true;
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) # 1numpy.pi 5. 表示圆周率1.4 numpy.e 6. 表示自然常数1pi = 3.1415926535897932384626433…1e = 2.71828182845904523536028747135266249775724709369995… 7. numpy的数值类型是dtype对象的实例
2.强制指定单位
import numpy as np 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')) #False3.如果单位不统一,则一律转化成其中最小的单位
a = np.array(['2020-03', '2020-03-08', '2020-03-08 20:00'], dtype='datetime64') print(a, a.dtype)4.使用arange()创建 datetime64 数组,用于生成日期范围
a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64) print(a)5.timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的,并且和相减运算中的两个 datetime64 中的较小的单位保持一致。
import numpy as np 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') 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]6.生成 timedelta64时,要注意年(‘Y’)和月(‘M’)这两个单位无法和其它单位进行运算(一年有几天?一个月有几个小时?这些都是不确定的)。 7.将指定的偏移量应用于工作日,单位天(‘D’)。计算下一个工作日,如果当前日期为非工作日,默认报错。可以指定 forward 或 backward 规则来避免报错。(一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日)
#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: 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) # 2020-07-13 print(b) # 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) # 2020-07-14 print(b) # 2020-07-13可以指定偏移量为 0 来获取当前日期向前或向后最近的工作日,当然,如果当前日期本身就是工作日,则直接返回当前日期。
返回指定日期是否是工作日。
import numpy as np
#2020-07-10 星期五 a = np.is_busday(‘2020-07-10’) b = np.is_busday(‘2020-07-11’) print(a) # True print(b) # False 【例】统计一个 datetime64[D] 数组中的工作日天数。
import numpy as np
#2020-07-10 星期五 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) #[‘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’] print(b) # 6 【例】自定义周掩码值,即指定一周中哪些星期是工作日。
import numpy as np
#2020-07-10 星期五 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) # True print(b) # False numpy.busday_count(begindates, enddates, weekmask=‘1111100’, holidays=[], busdaycal=None, out=None)Counts the number of valid days between begindates and enddates, not including the day of enddates. 【例】返回两个日期之间的工作日数量。
import numpy as np
#2020-07-10 星期五 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) # 6 print(b) # -6
array
一、range()函数 复制代码 >>> range(1,10) ——>不包括10 [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>range(1,10,2) ——>1到10,间隔为2(不包括10) [1, 3, 5, 7, 9] >>>range(10) ——>0到10,不包括10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 复制代码 我们在编写python程序时,通过这样就可以直接列出一个序列的数字出来了。方便快捷... 二、array()函数 首先看列List表示内容: Array=[2,3,9,1,4,7,6,8] 这个是一个数字列表,没有顺序的。 [2,3,9,1,4,7,6,8] 从前面开始的顺序列表为{0,1,2,3,4,5,6,7}——>前面序号 从后面开始的顺序列表为{-8,-7,-6,-5,-4,-3,-2,-1}——>后面序号 复制代码 >>>Array[0:] ——>切片从前面序号“0”开始到结尾,包括“0”位 [2, 3, 9, 1, 4, 7, 6, 8] >>>Array[:-1] ——>切片从后面序号“-1”到最前,不包括“-1”位 [2, 3, 9, 1, 4, 7, 6] >>>Array[3:-2] ——>切从前面序号“3”开始(包括)到从后面序号“-2”结束(不包括) [1, 4, 7] >>>Array[3::2] ——>从前面序号“3”(包括)到最后,其中分隔为“2” [1, 7, 8] >>>Array[::2] ——>从整列表中切出,分隔为“2” [2, 9, 4, 6] >>> Array[3::] ——>从前面序号“3”开始到最后,没有分隔 [1, 4, 7, 6, 8] >>> Array[3::-2] ——>从前面序号“3”开始,往回数第二个,因为分隔为“-2” [1, 3] >>> Array[-1] ——>此为切出最后一个 8 >>>Array[::-1] ——>此为倒序 [8, 6, 7, 4, 1, 9, 3, 2] 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'> # 创建三维数组 d = np.array([[(1.5, 2, 3), (4, 5, 6)], [(3, 2, 1), (4, 5, 6)]]) print(d, type(d)) # [[[1.5 2. 3. ] # [4. 5. 6. ]] # # [[3. 2. 1. ] # [4. 5. 6. ]]] <class 'numpy.ndarray'>array()和asarray()都可以将结构数据转化为 ndarray,但是array()和asarray()主要区别就是当数据源是ndarray 时,array()仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 asarray()不会。
import numpy as np 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#**因为是从0开始排列,所以是第二行第三列替换为2** print(x,type(x),x.dtype) #[[1 1 1] #[1 1 2] #[1 1 1]] <class 'numpy.ndarray'> int32 print(y,type(y),y.dtype) #[[1 1 1] #[1 1 1] #[1 1 1]] <class 'numpy.ndarray'> int32 print(z,type(z),z.dtype) #[[1 1 1] #[1 1 2] #[1 1 1]] <class 'numpy.ndarray'> int32 print(w,type(w),w.dtype) #[[1 1 1] #[1 1 2] #[1 1 1]] <class 'numpy.ndarray'> int32 ···