Numpy入门

一些基础知识

import numpy as np
# ndarray.ndim # 轴的个数,即维度的个数
def basic_knowledge(arraY):
    print("ndim:",arraY.ndim)
    print("shape:",arraY.shape)
    print("size:",arraY.size)
    print("dtype.name:",type(arraY.dtype))
    print("dtype:",type(arraY.dtype))
    print("itemsize:",arraY.itemsize)
    print("dtype.itemsize:",arraY.dtype.itemsize)
    print("data:",arraY.data)
    print("-"*20)
basic_knowledge(np.array([[1,2],[3,4]]))  # 二维数组
basic_knowledge(np.array([[1.,2.],[3.,4.]]))  # 二维数组
basic_knowledge(np.array([1,2]))    # 一维向量
basic_knowledge(np.array([[1],[2]]))  # 二维数组
ndim: 2
shape: (2, 2)
size: 4
dtype.name: <class 'numpy.dtype'>
dtype: <class 'numpy.dtype'>
itemsize: 4
dtype.itemsize: 4
data: <memory at 0x0000017796F38708>
--------------------
ndim: 2
shape: (2, 2)
size: 4
dtype.name: <class 'numpy.dtype'>
dtype: <class 'numpy.dtype'>
itemsize: 8
dtype.itemsize: 8
data: <memory at 0x0000017796F38708>
--------------------
ndim: 1
shape: (2,)
size: 2
dtype.name: <class 'numpy.dtype'>
dtype: <class 'numpy.dtype'>
itemsize: 4
dtype.itemsize: 4
data: <memory at 0x0000017796F0E648>
--------------------
ndim: 2
shape: (2, 1)
size: 2
dtype.name: <class 'numpy.dtype'>
dtype: <class 'numpy.dtype'>
itemsize: 4
dtype.itemsize: 4
data: <memory at 0x0000017796F38708>
--------------------

数组创建

np.array(([1,2],[3,4]),dtype = np.complex)  # np.int16  float64...
array([[1.+0.j, 2.+0.j],
       [3.+0.j, 4.+0.j]])

1、序列的序列或者序列的序列的序列都可以转化成array
2、可以在创建的时候指定数据类型

a = np.ones((2,2))
print(a,a.dtype)
print(np.zeros((2,2)))
print(np.empty((2,2)))
print(np.empty((1,2)))
[[1. 1.]
 [1. 1.]] float64
[[0. 0.]
 [0. 0.]]
[[0. 0.]
 [0. 0.]]
[[4.24399158e-314 8.48798317e-314]]

通常,数组的元素最初是未知的,但它的大小是已知的。因此,NumPy提供了几个函数来创建具有初始占位符内容的数组。这就减少了数组增长的必要,因为数组增长的操作花费很大。 ----numpy中文网
初始化类型都是float64
感觉np.empty((2,2))有什么特殊含义,不去深究了

a = np.array([[1,2],[2,3],[3,3]])
print(np.ones_like(a))
print(np.zeros_like(a))
print(np.empty_like(a))
a = [[1,2],[2,3]]
print(np.full_like(a,fill_value = 100))
[[1 1]
 [1 1]
 [1 1]]
[[0 0]
 [0 0]
 [0 0]]
[[0 0]
 [0 0]
 [0 0]]
[[100 100]
 [100 100]]

xxx_like 既可以以array为参数,也可以以序列、序列的序列、序列的序列的序列为参数

print(np.arange(0,10,2))
print(np.arange(0,5,0.7))
[0 2 4 6 8]
[0.  0.7 1.4 2.1 2.8 3.5 4.2 4.9]

只要确定上界、下界、步长
arange和range一样,下界不可取

print(np.linspace(1,10,100))
[ 1.          1.09090909  1.18181818  1.27272727  1.36363636  1.45454545
  1.54545455  1.63636364  1.72727273  1.81818182  1.90909091  2.
  2.09090909  2.18181818  2.27272727  2.36363636  2.45454545  2.54545455
  2.63636364  2.72727273  2.81818182  2.90909091  3.          3.09090909
  3.18181818  3.27272727  3.36363636  3.45454545  3.54545455  3.63636364
  3.72727273  3.81818182  3.90909091  4.          4.09090909  4.18181818
  4.27272727  4.36363636  4.45454545  4.54545455  4.63636364  4.72727273
  4.81818182  4.90909091  5.          5.09090909  5.18181818  5.27272727
  5.36363636  5.45454545  5.54545455  5.63636364  5.72727273  5.81818182
  5.90909091  6.          6.09090909  6.18181818  6.27272727  6.36363636
  6.45454545  6.54545455  6.63636364  6.72727273  6.81818182  6.90909091
  7.          7.09090909  7.18181818  7.27272727  7.36363636  7.45454545
  7.54545455  7.63636364  7.72727273  7.81818182  7.90909091  8.
  8.09090909  8.18181818  8.27272727  8.36363636  8.45454545  8.54545455
  8.63636364  8.72727273  8.81818182  8.90909091  9.          9.09090909
  9.18181818  9.27272727  9.36363636  9.45454545  9.54545455  9.63636364
  9.72727273  9.81818182  9.90909091 10.        ]

只要确定上界、下界、个数
linspace下界可取

打印数组

a = np.arange(6)
print(a)  
a = a.reshape((6,1))
print(a)  
a = a.reshape((2,3))
print(a)  
a = a.reshape((3,2))
print(a)  
[0 1 2 3 4 5]
[[0]
 [1]
 [2]
 [3]
 [4]
 [5]]
[[0 1 2]
 [3 4 5]]
[[0 1]
 [2 3]
 [4 5]]
import sys
np.set_printoptions(threshold = sys.maxsize)

如果数组太大而无法打印,NumPy会自动跳过数组的中心部分并仅打印角点
使用上述,便可全部打出

基本运算

加减跳过

乘法:

a = np.arange(6).reshape(2,3)
b = np.arange(6).reshape(3,2)
print("*:",a*a) # 按元素运算
print("multiply:",np.multiply(a,a)) # 按元素运算
print("@:",a@b) 
print("dot:",a.dot(b))
*: [[ 0  1  4]
 [ 9 16 25]]
multiply: [[ 0  1  4]
 [ 9 16 25]]
@: [[10 13]
 [28 40]]
dot: [[10 13]
 [28 40]]

除法:

a = np.array([2,3,4])
b = np.array([4,5,6])
print(np.divide(a,b)) # 与true_divide相同
print(np.true_divide(a,b))
print(np.floor_divide(b,a))  # 向下取整
[0.5        0.6        0.66666667]
[0.5        0.6        0.66666667]
[2 1 1]

取模运算:

a = np.array([2,-3,4])
print("mod:",np.mod(a,2))
print("remainder:",np.remainder(a,2))
print("%:",a%2)
print("fmod:",np.fmod(a,2))
mod: [0 1 0]
remainder: [0 1 0]
%: [0 1 0]
fmod: [ 0 -1  0]

1、fmod正负取自于除数
2、可以试试多维数组

+=、*=跳过,重点关注:当使用不同类型的数组进行操作时,结果数组的类型对应于更一般或更精确的数组(称为向上转换的行为):

# float += int  (int转化成float) —>float ;    int += float (float不会转化为int) —> 报错
a = np.arange(12).reshape(3,4) # [0,1,2,3],[4,5,6,7],[8,9,10,11]
print(a.sum())
print(a.sum(axis = 0))
print(a.sum(axis = 1))
print(a.cumsum())
66
[12 15 18 21]
[ 6 22 38]
[ 0  1  3  6 10 15 21 28 36 45 55 66]

1、axis = 0:按列;axis = 1:按行
2、min()、max()同
3、cumulative sum along each row

通函数

tobecontinued

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值