np.split(A, 4, axis=1),np.hsplit(A, 4) 分割

A = np.arange(12).reshape((3, 4))    # 水平方向的长度是4

print(np.split(A, 4, axis=1))        # 参数必须是可均分的,vertically均分成四块,分垂直风向,相当于竖着切举证
print(np.array_split(A, 3, axis=1)) # 可以不必是均分的 2+1+1
print(np.vsplit(A, 3)) # vertical 分垂直方向
print(np.hsplit(A, 4)) # 分水平方向

a.copy() 深度复制

a = np.arange(4)
b = a # a, b, c指向同样的地址内容
c = b
print(c is a) # True b = a.copy() # b是一个新的存储区域
print(b is a) # False

1D one-dimensional

a = np.arange(0, 8, 2)
print(a) # [0 2 4 6]
print(a.shape) # (4,)
print(a[3]) # 6

2D two-dimensional

  • 方括号的深度为2
  • 访问具体数据的两种方法效果相同
b = np.arange(6).reshape(2, 3)
print(b) # [[0 1 2] [3 4 5]]
print(b.shape) # (2, 3) print(b[1][0]) # 3
print(b[1, 0]) # 3

3D three-dimensional

  • 方括号的深度为3
  • 访问具体数据的两种方法效果相同
c = np.arange(24).reshape(2, 3, 4)
print(c)
> [[[ 0 1 2 3]
> [ 4 5 6 7]
> [ 8 9 10 11]]
> [[12 13 14 15]
> [16 17 18 19]
> [20 21 22 23]]] print(c.shape) # (2, 3, 4)
print(c[0, 1, 2]) # 6
print(c[0][1][2]) # 6

构造数组

  • np.ones((2, 3)), np.zeros((3, 4))

print(np.ones(2))
print(np.ones((2)))
print(np.ones((2,))) # 一维时括号和逗号都可以省略 print(np.ones((2, 3)))
print(np.ones((2, 3, 4))) # 全1
  • np.full((2, 2), 3)
print(np.full((2, 2), 3))    # 全3矩阵,[[3 3]  [3 3]]
  • np.eye(2)
print(np.eye(2))
print(np.eye((2))) # 单位矩阵,只有对角非零且全为1的方正矩阵,括号可以省略
  • np.empty((2,3))
print(np.empty((2,3)))
print(np.empty_like(c)) # 空矩阵的内容由缓存状态决定
  • np.linespace(0, 10, num=5)
print(np.linspace(0, 10, num=5))  # [ 0.   2.5  5.   7.5 10. ]
  • np.random.random((2, 4, 5))
my_random_array = np.random.random(5)
my_random_array = np.random.random((5))
my_random_array = np.random.random((5,)) # 一维时括号和逗号都可以省略 print(my_random_array) # [0.26326548 0.92779013 0.81319706 0.62325733 0.61494552] my_random_array = np.random.random((1, 2))
print(my_random_array) # [[0.73278507 0.12801832]]

维度对比 type() 和 np.shape()

  • print() python数组与numpy数组的输出形式不同,前者有逗号,后者没有逗号
print([[0, 1], [2, 3]][0][0])  # 0
print([[0, 1], [2, 3]][0]) # [0, 1]
print([[0, 1], [2, 3]]) # [[0, 1], [2, 3]] print(np.array([[0, 1], [2, 3]][0][0])) # 0
print(np.array([[0, 1], [2, 3]][0])) # [0 1]
print(np.array([[0, 1], [2, 3]])) # [[0 1] [2 3]]
  • type() python数组与numpy数组的类型不同
print(type([[0, 1, 2], [3, 4, 5]][0][0]))  # <class 'int'>
print(type([[0, 1, 2], [3, 4, 5]][0])) # <class 'list'>
print(type([[0, 1, 2], [3, 4, 5]])) # <class 'list'> print(type(np.array([[0, 1, 2], [3, 4, 5]])[0][0])) # <class 'numpy.int64'>
print(type(np.array([[0, 1, 2], [3, 4, 5]])[0])) # <class 'numpy.ndarray'>
print(type(np.array([[0, 1, 2], [3, 4, 5]]))) # <class 'numpy.ndarray'>
  • np.shape()
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0][0]))   # ()
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0])) # (3,)
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]]))) # (2, 3) print(np.random.random((2, 2))) # [[0.09242512 0.31837721] [0.13707168 0.31265585]]

slice 切片

my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]])

print(my_array)        # [[1 2 3 4 5] [6 7 8 9 0]]
print(my_array.shape) # (2, 5) print(my_array[0]) # [1 2 3 4 5]
print(my_array[1]) # [6 7 8 9 0]
print(my_array[0][3]) # 4 print(my_array[0, :]) # [1 2 3 4 5] 上面的方法和这种表示方法效果相同
print(my_array[1, :]) # [6 7 8 9 0]
print(my_array[0, 3]) # 4
  • 注意:

    1. 冒号和逗号在一起时起作用,单独的 [ : ] 需要忽略,不影响输出结果的判断
    2. 以后使用上面这种只用一对方括号的表示方法 [ , ]
    3. :可以用 ... 替代,一般使用 :
print(my_array[:][0])  # [1 2 3 4 5]   [:]不起作用,需要忽略
print(my_array[:][1]) # [6 7 8 9 0]
print(my_array[0][:]) # [1 2 3 4 5] [:]不起作用,需要忽略
print(my_array[1][:]) # [6 7 8 9 0]
  • 下面三种表示方法的的输出结果相同
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])

print(my_array[0])
print(my_array[1]) print(my_array[0, ])
print(my_array[1, ]) print(my_array[0, ...]) # [1 2 3 4 5]
print(my_array[1, ...]) # [6 7 8 9 0]
  • 下面三种表示方法的的输出结果相同
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])
print(my_array[[0], ])
print(my_array[[1], ]) print(my_array[[0], :])
print(my_array[[1], :]) print(my_array[[0], ...]) # [[1 2 3 4 5]]
print(my_array[[1], ...]) # [[6 7 8 9 0]]
print(my_array[0, 0])            # 1
print(my_array[[0], [0]]) # [1]
print(my_array[[0], [0, 2]]) # [1 3]
print(my_array[[0, 1], [2, 0]]) # [3 6]

+ - * / .dot()

a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]]) sum = a + b # [[ 6. 8.] [10. 12.]] 对应相加
difference = a - b # [[-4. -4.] [-4. -4.]]
product = a * b # [[ 5. 12.] [21. 32.]] 对应相乘
quotient = a / b # [[0.2 0.33333333] [0.42857143 0.5 ]]
matrix_product = a.dot(b) # [[19. 22.] [43. 50.]] 矩阵的乘法

1D Array 一位数组

  • 数组表示时,圆括号和方括号的效果相同,一般是用方括号,数据方括号之间都用逗号间隔开
a = np.array([0, 1, 2, 3, 4])     # [0 1 2 3 4]
b = np.array((0, 1, 2, 3, 4)) # [0 1 2 3 4] c = np.arange(5) # [0 1 2 3 4]
d = np.linspace(0, 2*np.pi, 5) # [0. 1.57079633 3.14159265 4.71238898 6.28318531]

MD Array M维数组

print(np.arange(11, 36).reshape(5, 5))
a = 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(a) # [[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(a[2, 4]) # 25
print(a[0, 1:4]) # [12 13 14]
print(a[1:4, 0]) # [16 21 26]
print(a[::2, ::2]) # [[11 13 15]
[21 23 25]
[31 33 35]] print(a[:, 1]) # [12 17 22 27 32]

Array properties 数组属性

  • .dtype .size .shape .itemsize .ndim .nbytes
a = 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(type(a)) # <class 'numpy.ndarray'> 类型
print(a.dtype) # int64 一个元素64位
print(a.size) # 25 一共25个元素
print(a.shape) # (5, 5)
print(a.itemsize) # 8 一个元素大小为8个字节
print(a.ndim) # 2 2维数组
print(a.nbytes) # 200 数组大小为200字节=25个*8字节

Basic Operators 基本操作符

  • np.arange(4) .reshape(2, 2)
a = np.arange(4)             # [0 1 2 3]
a = a.reshape(2, 2) # [[0 1] [2 3]]
b = np.array([3, 2, 1, 0]) # [3 2 1 0]
b = b.reshape((2, 2)) # [[3 2] [1 0]]
print(b+1) # [[4 3] [2 1]]
print(a + b) # [[3 3] [3 3]]
print(a - b) # [[-3 -1][ 1 3]]
print(a * b) # [[0 2] [2 0]]
print(a / (b+1)) # [[0. 0.33333333] [1. 3.]]
print(a ** 2) # [[0 1] [4 9]]
print(a <= b) # [[ True True] [False False]]
print(a.dot(b)) # [[1 0] [9 4]]

Special operator 特殊运算符

  • .sum() .min() .max() .cumsum()
a = np.arange(10)
print(a) # [0 1 2 3 4 5 6 7 8 9]
print(a.sum()) # 45
print(a.min()) # 0
print(a.max()) # 9
print(a.cumsum()) # [ 0 1 3 6 10 15 21 28 36 45]

Fancy indexing 花式索引

a = np.arange(0, 100, 10)
indices = [1, 2, 5, -1]
b = a[indices]
print(a) # [ 0 10 20 30 40 50 60 70 80 90]
print(b) # [10 20 50 90]

Boolean masking 布尔屏蔽

  • 代码:
import matplotlib.pyplot as plt
import numpy as np a = np.linspace(0, 2 * np.pi, 11)
b = np.sin(a)
plt.plot(a, b)
mask = b >= 0 print(mask) # [ True True ..... False False ]
print(a[mask]) # [0. 0.12822827 ...... 2.94925025 3.07747852]
print(b[mask]) # [0. 0.12787716 ...... 0.19115863 0.06407022] plt.plot(a[mask], b[mask], 'bo')
mask = (b >= 0) & (a <= np.pi / 2)
plt.plot(a[mask], b[mask], 'go') plt.show()
  • 显示输出:

Incomplete Indexing 不完整的索引

a = np.arange(0, 100, 10)  # [ 0 10 20 30 40 50 60 70 80 90]
b = a[:5] # [ 0 10 20 30 40]
c = a[a >= 50] # [50 60 70 80 90]

np.where(arr < 50)函数

a = np.arange(0, 100, 10)
b = np.where(a < 50)
c = np.where(a < 50)[0] print(a) # [ 0 10 20 30 40 50 60 70 80 90]
print(b) # (array([0, 1, 2, 3, 4]),)
print(b[0]) # [0 1 2 3 4]
print(c) # [0 1 2 3 4]

END

numpy基础篇-简单入门教程1的更多相关文章

  1. numpy基础篇-简单入门教程4

    np.set_printoptions(precision=3),只显示小数点后三位 np.random.seed(100) rand_arr = np.random.random([2, 2]) n ...

  2. numpy基础篇-简单入门教程3

    np import numpy as np np.__version__ print(np.__version__) # 1.15.2 numpy.arange(start, stop, step, ...

  3. numpy基础篇-简单入门教程2

    import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...

  4. NumPy简单入门教程

    # NumPy简单入门教程 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体 ...

  5. 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...

  6. GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

  7. Flyway 简单入门教程

    原文地址:Flyway 简单入门教程 博客地址:http://www.extlight.com 一.前言 Flyway 是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway 可以 ...

  8. .net 开源模板引擎jntemplate 实战演习:基础篇之入门

    一.简介 模板引擎是Web开发中非常重要的一环,它负责将页面上的动态内容呈现出最终的结果展现给前端用户,在asp.net mvc中,我们最熟悉的就是Razor了,作为官方的视图引擎(视图引擎不等同于模 ...

  9. 【ASP.NET 基础】WCF入门教程一(什么是WCF)?

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

随机推荐

  1. P1776 宝物筛选_NOI导刊2010提高(02)(背包的二进制优化)

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

  2. node.js连接数据库基本操作、封装数据库操作,输出到网页

    声明:以下代码测试通过,不同于直接的复制粘贴乱七八糟未测试的代码,完全可以用,最后会附上所有的代码和sql文件 首先建立表,建表语句如下: /* SQLyog Ultimate v12.08 (64 ...

  3. uva 11584 - 字符串 dp

    题目链接 一个长度1000的字符串最少划分为几个回文字符串 ---------------------------------------------------------------------- ...

  4. 使用maven插件dockerfile-maven-plugin生成Docker镜像并推送到镜像仓库

    1.引入maven插件 <build> <plugins> <plugin> <groupId>com.spotify</groupId> ...

  5. NodeJS学习笔记 (2)文件系统操作-fs(ok)

    原文:https://github.com/chyingp/nodejs-learning-guide/blob/master/%E6%A8%A1%E5%9D%97/fs.md#%E9%80%9A%E ...

  6. Django综合基础知识

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...

  7. JavaIO 总结-装饰者模式

    另外参考文章:http://www.ibm.com/developerworks/cn/java/j-lo-javaio/ 一. File类 file.createNewFile();file.del ...

  8. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  9. 批量修改文件的编码格式至UTF-8

    批量修改文件的编码格式至UTF-8 学习了: https://jingyan.baidu.com/article/e8cdb32b47a1ea37042bad11.html http://blog.c ...

  10. arcmap 设置线段的不同颜色(及其它转化)

    一: shp 转化为 mxd或导出地图  当时做的第一个shp文件,应该是研一的第二个学期了,都不记得是怎么操作的了. 通过file另存为mxd就可以生成各个shp的arcmap能够直接打开的mxd文 ...