numpy库应用04

7.Numpy基本的组合运算

  1. 数学函数应用(exp-e底数,sqrt-平方根函数)

     import numpy as np//设置宏定义
     B=np.arange(3)
     print (B)//结果[0 1 2]
     print (np.exp(B))//结果:[1. exp数据]
     print (np.sqrt(B))//结果:[0. sqrt数据]
    
  2. 数据取整,维数转换

    import numpy as np//设置宏定义
    a=np.floor(10*np.random.random((3,4)))//np.random(3,4)的数据,矩阵3X4
                                         //numpy.floor-向下取整(数据取整数处理)
                                        //numpy.ceil-向上取整
    print (a)//结果:[6. 7. 2. 9]
    [6. 0. 5. 2]
    [9. 0. 9. 6]
    print(“------------”)
    print (a.ravel)//结果:[6. 7. 2. 9. 6. 0. 5. 2. 9. 0. 9. 6]
                   //.ravel-将多维数组降位一维
                  //.flattenh-将多维数组降位一维(优先级高于ravel,并保持原有矩阵)
    print(“------------”)
    a.shape=(6,2)//a:矩阵定义名称 shape:矩阵打印函数
    print (a)//结果:[6. 7]
                    [2. 9]
                    [6. 0]
                    [5. 2]
                    [9. 0]
                    [9. 6]
    print(“------------”)
    print (a,T)//结果:[[6. 2. 6. 5. 9. 9.][7. 9. 0. 2. 0. 6.]]
    

numpy.floor

numpy.floor(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘floor’>
Return the floor of the input, element-wise.

The floor of the scalar x is the largest integer i, such that i <= x. It is often denoted as \lfloor x \rfloor.

Parameters:
x : array_like
Input data.
out : ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
**kwargs
For other keyword-only arguments, see the ufunc docs.
Returns:
y : ndarray or scalar
The floor of each element in x. This is a scalar if x is a scalar.
See also
ceil, trunc, rint

Notes

Some spreadsheet programs calculate the “floor-towards-zero”, in other words floor(-2.5) == -2. NumPy instead uses the definition of floor where floor(-2.5) == -3.

Examples

a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.floor(a)
array([-2., -2., -1., 0., 1., 1., 2.])

8.矩阵数据组合

import numpy as np//设置宏定义
a = np.floor(10*np.random.random((2,2)))//数据内容10*(2-2)的随机;数据矩阵2X2
b = np.floor(10*np.random.random((2,2)))//数据内容10*(2-2)的随机;数据矩阵2X2
print (a)//结果:[3. 1]
       //      [0. 1]
print(“------------”)
print (b)//结果:[4. 2]
[9. 2]
print(“------------”)
print (np.hstack((a,b)))//结果:[3. 1. 4. 2]
                       // [0. 1. 9. 2]

.hstack(a,b)-stack针对数据(a,b)叠加。hstack叠加方向行向(h)
.vstack(a,b)-stack针对数据(a,b)叠加。vstack叠加方向纵向(v)

9.矩阵数据拆分组合

import numpy as np//设置宏定义
a = np.floor(10*np.random.random((2,12)))//数据内容10*(2-12)的随机;数据矩阵2X12
print (a)//结果:数据内容10*(2-12)的随机;数据矩阵2X12
print(“------------”)
print (np.hsplit(a,3))//矩阵数据a拆分,拆分数量为3(3等份的矩阵),方向为行
print(“------------”)
print (np.hsplit(a,(3,4)))//矩阵数据a拆分,拆分数量为(3,4)(矩阵第3,4行),方向为行
a = np.floor(10*np.random.random((12,2)))//数据内容10*(12-2)的随机;数据矩阵12X2
print(“------------”)
print (a)//结果:数据内容10*(2-12)的随机;数据矩阵12X2
np.vsplit(a,3)//矩阵数据a拆分,拆分数量为3(3等份的矩阵),方向为纵

.hsplit(a,b)-split针对矩阵数据a拆分,拆分数量为b(b等份的矩阵)。hspli拆方向行向(h)
.hsplit(a,(b,c))-split针对矩阵数据a拆分,拆分数量为(b,c)(矩阵第b,c行)。hspli拆方向行向(h)
.vsplit(a,b)-split针对矩阵数据a拆分,拆分数量为b(b等份的矩阵)。vspli拆方向纵向(v)
.vsplit(a,(b,c))-split针对矩阵数据a拆分,拆分数量为(b,c)(矩阵第b,c列)。hspli拆方向纵向(v)

—未完待续(2018.10.16.21点11分)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初上花样年华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值