Numpy 基础

参考https://www.jianshu.com/p/83c8ef18a1e8

import numpy as np

简单创建数组

# 创建简单列表
a = [1, 2, 3, 4]
# 将列表转换为数组
b = np.array(a) print(a, "\t", b) print("\n数组元素个数:\t",b.size)
print("数组形状:\t", b.shape)
print("数组维度:\t", b.ndim)
print("数组元素类型:\t", b.dtype)

[1, 2, 3, 4] [1 2 3 4]

数组元素个数: 4

数组形状: (4,)

数组维度: 1

数组元素类型: int32

快速创建N维数组的api函数

# 创建10行10列的数值为浮点1的矩阵
array_one = np.ones([5, 5])
# 创建10行10列的数值为浮点0的矩阵
array_zero = np.zeros([5, 5])

从现有的数据创建数组:

  • array(深拷贝)
  • asarray(浅拷贝)

创建随机数组

  • 均匀分布

    • np.random.rand(10, 10)创建指定形状(示例为10行10列)的数组(范围在0至1之间)
    • np.random.uniform(0, 100)创建指定范围内的一个数
    • np.random.randint(0, 100) 创建指定范围内的一个整数
  • 正态分布

    给定均值/标准差/维度的正态分布 np.random.normal(1.75, 0.1, (2, 3))

数组的索引、切片

# 正态生成4行5列的二维数组
arr = np.random.normal(1.75, 0.1, (4, 5))
print(arr)
# 截取第1至2行的第2至3列(从第0行算起)
after_arr = arr[1:3, 2:4]
print(after_arr)
[[1.70402304 1.6514506  1.63572257 1.81760737 1.91454784]
[1.65294415 1.82911738 1.78747379 1.82805398 1.76698167]
[1.91056499 1.86022006 1.89047126 1.62899365 1.88996188]
[1.77414812 1.630505 1.81022207 1.78061747 1.65964094]]
[[1.78747379 1.82805398]
[1.89047126 1.62899365]]

改变数组形状(要求前后元素个数匹配)

print("reshape函数的使用!")
one_20 = np.ones([20])
print("-->1行20列<--")
print (one_20) one_4_5 = one_20.reshape([4, 5])
print("-->4行5列<--")
print (one_4_5)
reshape函数的使用!
-->1行20列<--
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
-->4行5列<--
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]

Numpy 计算

条件运算

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
stus_score > 80
array([[False,  True],
[ True, True],
[ True, False],
[ True, True],
[False, True]])
# 三目运算。 小于80的替换为0,不小于80的替换为90
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
np.where(stus_score < 80, 0, 90)
array([[90, 90],
[90, 90],
[90, 0],
[90, 90],
[ 0, 90]])

统计运算

指定轴最大值amax(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一列的最大值(0表示列)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result) print("每一行的最大值为:")
result = np.amax(stus_score, axis=1)
print(result)
每一列的最大值为:
[86 88]
每一行的最大值为:
[88 82 84 86 81]

指定轴最小值amin

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的最小值(0表示列)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result) # 求每一行的最小值(1表示行)
print("每一行的最小值为:")
result = np.amin(stus_score, axis=1)
print(result)
每一列的最小值为:
[75 75]
每一行的最小值为:
[80 81 75 83 75]

指定轴平均值mean

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的平均值(0表示列)
print("每一列的平均值:")
result = np.mean(stus_score, axis=0)
print(result) # 求每一行的平均值(1表示行)
print("每一行的平均值:")
result = np.mean(stus_score, axis=1)
print(result)
每一列的平均值:
[81.4 81.6]
每一行的平均值:
[84. 81.5 79.5 84.5 78. ]

方差std

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的方差(0表示列)
print("每一列的方差:")
result = np.std(stus_score, axis=0)
print(result) # 求每一行的方差(1表示行)
print("每一行的方差:")
result = np.std(stus_score, axis=1)
print(result)
每一列的方差:
[3.77359245 4.1761226 ]
每一行的方差:
[4. 0.5 4.5 1.5 3. ]

数组运算

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("加分前:")
print(stus_score) # 为所有平时成绩都加5分
stus_score[:, 0] = stus_score[:, 0]+5
print("加分后:")
print(stus_score) # 加减乘除类似
加分前:
[[80 88]
[82 81]
[84 75]
[86 83]
[75 81]]
加分后:
[[85 88]
[87 81]
[89 75]
[91 83]
[80 81]]

矩阵运算 np.dot()(非常重要)

  • 计算规则

    (M行, N列) * (N行, Z列) = (M行, Z列)
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 平时成绩占40% 期末成绩占60%, 计算结果
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("最终结果为:")
print(result)
最终结果为:
[[84.8]
[81.4]
[78.6]
[84.2]
[78.6]]
  • 矩阵拼接
  • 矩阵垂直拼接
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.vstack((v1, v2))
print("v1和v2垂直拼接的结果为")
print(result)
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2垂直拼接的结果为
[[ 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("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.hstack((v1, v2))
print("v1和v2水平拼接的结果为")
print(result)
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2水平拼接的结果为
[[ 0 1 2 3 4 5 12 13 14 15 16 17]
[ 6 7 8 9 10 11 18 19 20 21 22 23]]

Numpy 基础的更多相关文章

  1. 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片

    概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...

  2. 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算

    http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...

  3. 利用Python进行数据分析——Numpy基础:数组和矢量计算

    利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...

  4. numpy 基础操作

    Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...

  5. [转]python与numpy基础

    来源于:https://github.com/HanXiaoyang/python-and-numpy-tutorial/blob/master/python-numpy-tutorial.ipynb ...

  6. python学习笔记(三):numpy基础

    Counter函数可以对列表中数据进行统计每一个有多少种 most_common(10)可以提取前十位 from collections import Counter a = ['q','q','w' ...

  7. Numpy基础数据结构 python

    Numpy基础数据结构 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成: 实际的数据 描述这些数据的元数据 1.一维数组 import numpy as np ar = np.a ...

  8. Python Numpy基础教程

    Python Numpy基础教程 本文是一个关于Python numpy的基础学习教程,其中,Python版本为Python 3.x 什么是Numpy Numpy = Numerical + Pyth ...

  9. NumPy基础操作

    NumPy基础操作(1) (注:记得在文件开头导入import numpy as np) 目录: 数组的创建 强制类型转换与切片 布尔型索引 结语 数组的创建 相关函数 np.array(), np. ...

随机推荐

  1. k8s-No.3-pod进阶

    本章目录 pod环境变量env pod的资源限制resources pod的健康检查-探针 pod的imagepullsecrets 一   pod-env 环境变量就是系统或者程序运行时的预定义的参 ...

  2. DateTimeFormat

    中文:星期一,星期二 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.Da ...

  3. MySQL的GTID复制与传统复制的相互转换

    主库:192.168.225.128:3307从库1:192.168.225.129:3307 Gtid作为5.6版本以来的杀手级特性,却因为不支持拓扑结构内开关而饱受诟病.如果你需要从未开启GTID ...

  4. 001-JUnit之断言assert

    一.简介以及pom JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活: 使用全新的断言语法:assertThat, ...

  5. bootstrap 自适应和响应式布局的区别

    自适应:  不管屏幕多大,都尽量不换行,而只是横向缩放. 响应式: 屏幕变小了之后,属于同一行的元素受到挤压后,行的右边元素自动换行显式: 屏幕大了后,本属于同一行的元素尽可能的排在同一行内: boo ...

  6. [js]d3.js绘制拓扑树

    echart也支持拓扑树了 所需的json数据格式: children嵌套 vis.js也支持绘制拓扑树 数据格式: nodes: {id, label, title} edges: {from, t ...

  7. Hadoop生态集群YARN详解

    一,前言 Hadoop 2.0由三个子系统组成,分别是HDFS.YARN和MapReduce,其中,YARN是一个崭新的资源管理系统,而MapReduce则只是运行在YARN上的一个应用,如果把YAR ...

  8. 马永亮(马哥) linux视频2018年版

    视频试看地址: 百度云链接: https://pan.baidu.com/s/1q4DV62DiFbBT8t63CapLJA  提取码: h5e4 资料收集于网络,如有侵权,告知后删除

  9. 使用python写的一个代码统计程序

    # encoding="utf-8" """ 统计代码行数 """ import sys import os def c ...

  10. 使用 notify.js 桌面提醒

    //var iN = new iNotify({ // effect: 'flash', // interval: 500, // message: "有消息拉!", // aud ...