numpy

作者: 稍安勿躁zzh | 来源:发表于2018-03-08 19:14 被阅读0次
  • rand(d0,d1....dn)
    参数:维度设置、int整型、可选;给定的维度必须是正数,如果没有给任何参数,则返回一个python浮点数.
    输出:ndarray;随机数值,数值大小服从均匀分布[0, 1).
#例子
np.random.rand(3,2)
#输出
[[ 0.91018835  0.81058281]
 [ 0.79837365  0.45386407]
 [ 0.45797705  0.45170906]]
  • randn(d0,d1....dn)
    使用方法、参数和输出同上,区别是返回的数值服从标准正太分布.
#例子
np.random.randn()
#输出
1.3833194717008752
#例子
np.random.randn(2, 3) + 3
#输出
[[ 1.59315748  3.60762008  2.02047004]
 [ 2.69538253  2.65981673  4.2338875 ]]
  • randint(low, high=None, size=None, dtype='l')
    参数:分别是范围值设置参数[low, high)(high是可选的,如果为None,则范围是[0, low))、维度设置(可选)、输出结果的类型参数设置.
    返回值:随机整数
#例子
np.random.randint(2, size=10)
#输出
[1, 0, 0, 0, 1, 1, 0, 0, 1, 0]
#例子
np.random.randint(5, size=(2, 4))
#输出
[[4, 0, 2, 1],
 [3, 2, 2, 0]]
  • random(size=None)
#Return random floats in the half-open interval [0.0, 1.0).
#例子
print(np.random.random())
print(np.random.random((2, 3)))
#输出
0.668554697988308
[[ 0.67318801  0.29553275  0.48014267]
 [ 0.09472903  0.55096364  0.05223546]]
  • choice(a, size=None, replace=True, p=None)
    choice() 方法返回一个随机项(Generates a random sample from a given 1-D array)
    参数:a为1-D array-like or int、维度、replace是抽取的样本项是否重复、p为每一项被采样到的概率
    返回:single item or ndarray
#例子
np.random.choice(5, 3)
#输出、如果a为单个整数,则从np.arange(a)中采样得到
[0, 3, 4]
#例子
np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
np.random.choice(5, 3, replace=False)
#输出、注意到第一个例子重复的项被采样出、而第二个例子则不会(不信可以去多打印几次。。。。。)
[3, 3, 0]
[3, 1, 0]
#例子
aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
#输出
['pooh', 'pooh', 'pooh', 'Christopher', 'piglet']
  • .expand_dims(a, axis)
    对给定array插入一个维度,用于扩展数组的shape
#例子
x = np.array([1,2])
x.shape
#输出
(2,)
#1维的例子
>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
#
>>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,newaxis]
>>> y
array([[1],
       [2]])
>>> y.shape
(2, 1)
#二维
x = np.array([[1,2,3],[4,5,6]])
print x
print x.shape
>>>[[1 2 3]
    [4 5 6]]
>>>(2, 3)
----------------
y = np.expand_dims(x,axis=0)
print y
print "y.shape: ",y.shape
print "y[0][1]: ",y[0][1]
>>>[[[1 2 3]
      [4 5 6]]]
>>>y.shape:  (1, 2, 3)
>>>y[0][1]:  [4 5 6]
----------------
y = np.expand_dims(x,axis=1)
print y
print "y.shape: ",y.shape
print "y[1][0]: ",y[1][0]
>>>[[[1 2 3]]
     [[4 5 6]]]
>>>y.shape:  (2, 1, 3)
>>>y[1][0]:  [4 5 6]
  • 还有这种操作~~
#例子
import numpy as np
a = np.array([1,2,3,4,5,6])
b = (a<5)
print(b)
print(type(b))
print(a[a < 5])
#out
[ True  True  True  True False False]
<class 'numpy.ndarray'>
[1 2 3 4]
#例子
a = [1, 2, 3]
b = [4, 5, 6]
print(a+b)
#out
[1, 2, 3, 4, 5, 6]

暂时这么多,笔记~~~~~~

相关文章

  • 科学计算库numpy的执行示例

    numpy1 numpy2 numpy3 numpy4

  • numpy中的常量

    Constants 正无穷 numpy.inf numpy.Inf numpy.Infinity numpy.in...

  • NumPy学习资料

    Numpy 中文资料 NumPy 中文文档 NumPy 中文用户指南 NumPy 中文参考手册

  • Numpy基础

    安装Numpy Numpy Numpy属性 ndim:纬度 shape:行数和列数 size:元素个数 Numpy...

  • Numpy和Pandas基本操作速查

    """ numpy 基本操作 """'''安装 Numpy 的方法:pip install numpy''''''...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • 学习:biopython的安装

    安装Numpy 因为使用biopython需要numpy的支持,所以需要先安装numpy。安装numpy过程如下:...

  • Numpy

    Numpy中文文档 # 基本语法 ``` import numpy myText = numpy.genfromt...

  • numpy运算

    numpy的与运算 numpy 中 argsort() numpy 中的布尔索引

网友评论

    本文标题:numpy

    本文链接:https://www.haomeiwen.com/subject/yojyfftx.html