python 日期 random fromiter flags.writeable   np.ndenumerate, np.ndindex put函数,take函数 flat flaten nditer

获得昨天,今天,明天的日期

>>> yesterday = np.datetime64('today','D') - np.timedelta64(1,'D')
>>> print ("Yesterday is " + str(yesterday))
Yesterday is 2020-04-07
>>> today = np.datetime64('today', 'D')
>>> print ("Today is " + str(today))
Today is 2020-04-08
>>> tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
>>> print ("Tomorrow is "+ str(tomorrow))
Tomorrow is 2020-04-09

得到所有与2016年7月对应的日期

>>> Z = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
>>> print(Z)
['2016-07-01' '2016-07-02' '2016-07-03' '2016-07-04' '2016-07-05'
 '2016-07-06' '2016-07-07' '2016-07-08' '2016-07-09' '2016-07-10'
 '2016-07-11' '2016-07-12' '2016-07-13' '2016-07-14' '2016-07-15'
 '2016-07-16' '2016-07-17' '2016-07-18' '2016-07-19' '2016-07-20'
 '2016-07-21' '2016-07-22' '2016-07-23' '2016-07-24' '2016-07-25'
 '2016-07-26' '2016-07-27' '2016-07-28' '2016-07-29' '2016-07-30'
 '2016-07-31']

函数名    功能
numpy.ceil(x,)    向正无穷取整
numpy.floor(x,)    向负无穷取整
numpy.trunc/fix(x,)    截取整数部分
numpy.rint(x,)    四舍五入到最近整数
numpy.around(x,)    四舍五入到给定的小数位
 

>>> Z = np.random.uniform(0,10,10)
>>> print (Z)
[9.0462202  8.14654078 7.70186988 0.17435766 4.3867558  4.07389774
 9.93885826 6.11428093 1.23498679 4.53191938]
>>> print (Z - Z%1)
[9. 8. 7. 0. 4. 4. 9. 6. 1. 4.]
>>> print(np.floor(Z))
[9. 8. 7. 0. 4. 4. 9. 6. 1. 4.]
>>> print (np.ceil(Z))
[10.  9.  8.  1.  5.  5. 10.  7.  2.  5.]
>>> print (np.ceil(Z)-1)
[9. 8. 7. 0. 4. 4. 9. 6. 1. 4.]
>>> print (Z.astype(int))
[9 8 7 0 4 4 9 6 1 4]
>>> print (np.trunc(Z))
[9. 8. 7. 0. 4. 4. 9. 6. 1. 4.]

 fromiter

fromiter(iterable,dtype,count = -1)
从可迭代对象创建新的1维数组。

>>> def generate():
...   for x in range(10):
...     yield x
...
>>> Z = np.fromiter(generate(),dtype=float,count=-1)
>>> print(Z)
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
>>> print(range(10))
range(0, 10)
>>> print(np.arange(10))
[0 1 2 3 4 5 6 7 8 9]

flags.writeable 

创建一个只读数组(read-only)

>>> Z=np.zeros(10)
>>> Z.flags.writeable=False
>>> Z[0] =1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: assignment destination is read-only

打印每个numpy标量类型的最小值和最大值

>>> for dtype in [np.int8, np.int32, np.int64]:
...    print(np.iinfo(dtype).min)
...    print(np.iinfo(dtype).max)
...
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807

>>> for dtype in [np.float32, np.float64]:
...   print(np.finfo(dtype).min)
...   print(np.finfo(dtype).max)
...   print(np.finfo(dtype).eps)
...
-3.4028235e+38
3.4028235e+38
1.1920929e-07
-1.7976931348623157e+308
1.7976931348623157e+308
2.220446049250313e-16

 np.ndenumerate, np.ndindex

>>> Z = np.arange(9).reshape(3,3)
>>> for index, value in np.ndenumerate(Z):
...   print (index, value)
...
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
>>> for index in np.ndindex(Z.shape):
...   print (index, Z[index])
...
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

numpy put函数,take函数

numpy中take函数和put函数有着与神奇索引类似的作用,take函数可以用于获取数组子集,而put函数可以设置数组子集。
话不多说,直接上例子:
#take函数
arr = np.arange(6)*100 #arr初始化为array( [0,100,200,300,400,500])
inds = [4,3,2]
arr.take(inds)
#相当于从arr序列中依次获取索引为4,3,2位置上的元素,因此输出为array([400,300,200])

#put函数
arr = np.arange(6)*100 #arr初始化为array( [0,100,200,300,400,500])
inds = [4,3,2]
arr.put(inds, 11)
#相当于将arr序列中索引为4,3,2位置上的元素用11来替换,因此输出为array([0,100,11,11,11,500])

 

np.random.choice()

#numpy.random.choice(a, size=None, replace=True, p=None)
#从a(只要是ndarray都可以,但必须是一维的)中随机抽取数字,并组成指定大小(size)的数组
#replace:True表示可以取相同数字,False表示不可以取相同数字
#数组p:与数组a相对应,表示取数组a中每个元素的概率,默认为选取每个元素的概率相同。 

np.random

https://www.jianshu.com/p/85ec63259c4e 

 

numpy.ndarray.flat/flatten


ndarray.flat

A 1-D iterator over the array. /将数组转换为1-D的迭代器 /
flat返回的是一个迭代器,可以用for访问数组每一个元素

import numpy as np
a = np.arange(4).reshape(2,2)
print(a)
for i in a.flat:
    print(i)
#迭代器可以用list进行输出
print(list(a.flat))
print(type(a.flat))#返回类型为 numpy.flatiter
#可以用索引对迭代器进行引号
a.flat[3]
[[0 1]
 [2 3]]
0
1
2
3
[0, 1, 2, 3]
<class 'numpy.flatiter'>
3

ndarray.flatten(order=’C’)

Return a copy of the array collapsed into one dimension.
将数组的副本转换为一个维度,并返回

可选参数,order:{‘C’,‘F’,‘A’,‘K’}

  • ‘C’:C-style,行序优先
  • ‘F’:Fortran-style,列序优先
  • ‘A’:if a is Fortran contiguous in memory ,flatten in column_major order
  • ‘K’:按照元素在内存出现的顺序进行排序
    默认为’C’

举例如下:

a = np.array([[4,5],[4,9]])
#默认按行转换
b= a.flatten()
print(b)
#换成列来划分
c = a.flatten('F')
print(c)
[4 5 4 9]
[4 4 5 9]

NumPy 迭代数组 nditer

 https://www.runoob.com/numpy/numpy-terating-over-array.html

 

numpy.bincount详解

 

python实现flatmap flat python_迭代器

 大致说bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数。

# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([0, 1, 1, 3, 2, 1, 7])
# 索引0出现了1次,索引1出现了3次......索引5出现了0次......
np.bincount(x)
#因此,输出结果为:array([1, 3, 1, 1, 0, 0, 0, 1])

# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([7, 6, 2, 1, 4])
# 索引0出现了0次,索引1出现了1次......索引5出现了0次......
np.bincount(x)
#输出结果为:array([0, 1, 1, 0, 1, 0, 1, 1])

weights这个参数。文档说,如果weights参数被指定,那么x会被它加权,也就是说,如果值n发现在位置i,那么out[n] += weight[i]而不是out[n] += 1.**因此,我们weights的大小必须与x相同,否则报错。

w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6])
# 我们可以看到x中最大的数为4,因此bin的数量为5,那么它的索引值为0->4
x = np.array([2, 1, 3, 4, 4, 3])
# 索引0 -> 0
# 索引1 -> w[1] = 0.5
# 索引2 -> w[0] = 0.3
# 索引3 -> w[2] + w[5] = 0.2 - 0.6 = -0.4
# 索引4 -> w[3] + w[4] = 0.7 + 1 = 1.7
np.bincount(x,  weights=w)
# 因此,输出结果为:array([ 0. ,  0.5,  0.3, -0.4,  1.7])

最后,我们来看一下minlength这个参数。文档说,如果minlength被指定,那么输出数组中bin的数量至少为它指定的数(如果必要的话,bin的数量会更大,这取决于x)

# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# 本来bin的数量为4,现在我们指定了参数为7,因此现在bin的数量为7,所以现在它的索引值为0->6
np.bincount(x, minlength=7)
# 因此,输出结果为:array([0, 2, 1, 2, 0, 0, 0])

# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# 本来bin的数量为4,现在我们指定了参数为1,那么它指定的数量小于原本的数量,因此这个参数失去了作用,索引值还是0->3
np.bincount(x, minlength=1)
# 因此,输出结果为:array([0, 2, 1, 2])