Numpy 01
Infi-chu:
http://www.cnblogs.com/Infi-chu/
import numpy as np # 创建的数组
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]]) # 基本属性
count = stus_score.size
print('该数组的元素有 --> ',count)
shape = stus_score.shape
print('该数组的形状是 --> ',shape) # shape结果的第一个元素是行,第二个元素是列
ndim = stus_score.ndim
print('该数组的维度 --> ',ndim)
type = stus_score.dtype
print('该数组元素类型是 --> ',type) # 快速创建n维数组的API函数
# 创建10行10列的数值为浮点1的矩阵
array_one = np.ones([10,10])
print('array_one --> ',array_one)
# 创建10行10列的数值为浮点1的矩阵
array_zero = np.zeros([10,10])
print('array_zero --> ',array_zero) # Numpy创建随机数组
# 均值分布
'''
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) # 改变数组形状(要求前后元素个数匹配)
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) # 数组的计算
# 比较
res = stus_score > 80
print(res)
res = np.where(stus_score > 80)
print(res)
res = np.where(stus_score > 80,'Yes','No') # 大于80的重写为Yes,否则为No
print(res)
# 求最大值
print('数组是:\n',stus_score)
# 求每一列的最大值(0表示列)
result = np.amax(stus_score, axis=0)
print("每一列的最大值为:\n",result)
# 求每一行的最大值(1表示列)
result = np.amax(stus_score, axis=1)
print("每一行的最大值为:\n",result)
# 求最小值
# 求每一行的最小值(0表示列)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result)
# 求每一行的最小值(1表示行)
print("每一行的最小值为:")
result = np.amin(stus_score, axis=1)
print(result)
# 求平均值
# 求每一行的平均值(0表示列)
print("每一列的平均值:")
result = np.mean(stus_score, axis=0)
print(result)
# 求每一行的平均值(1表示行)
print("每一行的平均值:")
result = np.mean(stus_score, axis=1)
print(result)
# 求方差
# 求每一行的方差(0表示列)
print("每一列的方差:")
result = np.std(stus_score, axis=0)
print(result)
# 求每一行的方差(1表示行)
print("每一行的方差:")
result = np.std(stus_score, axis=1)
print(result) # 数组的运算
# 加法
print("加分前:")
print(stus_score)
# 为所第一列成绩都加5分
stus_score[:, 0] = stus_score[:, 0]+5
stus_score_new = stus_score[:, 0]+5
print("加分后:")
print(stus_score)
print('')
print(stus_score_new)
# 乘法
print("减半前:")
print(stus_score)
# 平时成绩减半
stus_score[:, 0] = stus_score[:, 0]*0.5
print("减半后:")
print(stus_score)
# 数组间运算
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a + b
d = a - b
e = a * b
f = a / b
print("a+b为", c)
print("a-b为", d)
print("a*b为", e)
print("a/b为", f) # np.dot()
# (M行, N列) * (N行, Z列) = (M行, Z列)
# 平时成绩占40% 期末成绩占60%, 计算结果
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("最终结果为:")
print(result) # 矩阵拼接
# 垂直拼接
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)
# 水平拼接
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)
Numpy 01的更多相关文章
- Numpy | 01 简介
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...
- 01. Numpy模块
1.科学计算工具-Numpy基础数据结构 1.1.数组ndarray的属性 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成:① 实际的数据② 描述这些数据的元数据 注意数组格式, ...
- [Pandas] 01 - A guy based on NumPy
主要搞明白NumPy“为什么快”. 学习资源 Panda 中文 易百教程 远程登录Jupyter笔记本 效率进化 四步效率优化 NumPy 底层进行了不错的优化. %timeit 对于任意语句,它会自 ...
- 数据分析01 /numpy模块
数据分析01 /数据分析之numpy模块 目录 数据分析01 /数据分析之numpy模块 1. numpy简介 2. numpy的创建 3. numpy的方法 4. numpy的常用属性 5. num ...
- 18-09-21 numpy 的基础学习01
# 1关于numpy 的学习import numpy as np # 一 如何创建数组****# 1 有规律的一维数据的创建======# 1 range() 和arange() 区别 貌似没有区别l ...
- 01 numpy库(一)
01-numpy NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行 ...
- 01.Numpy数组的基本应用
数组的创建 数组的访问 数组的合并 数组的分割 数组创建 >>> import numpy as np 创建一维数组 >>> x = np.arange(10) & ...
- numpy学习笔记 01
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...
- 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算
http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...
随机推荐
- Ubuntu下python的第三方module无法在pycharm中导入
换了台笔记本,新安装的requests module无法在pycharm导入: Traceback (most recent call last): File "/home/winsterc ...
- July 11th 2017 Week 28th Tuesday
No possession, but use, in the only riches. 真正的财富不是占有,而是使用. These days I have bought tens of books a ...
- SAP CRM One order里user status和system status的mapping逻辑
Below example show: How the mapping relationship between User status and System status maintained in ...
- Promise里捕捉错误的最佳实践
Promise里的同步部分不需要try catch new Promise((resolve, reject) => { throw new Error('error'); setTimeout ...
- 洛谷p1064 金明的预算方法
有附带条件的01背包 要那附件必须拿主件 因为一个主件最多有两个附件,所以每次遇到主件可能有四种选择 1.只拿主件 2.拿主件和一号附件 3.拿主件和二号附件 4.都拿 #include <cs ...
- WMIC_2
- 显示mac电脑中隐藏的文件和文件夹
显示mac电脑中隐藏的文件和文件夹的办法:打开电脑,cd到相应的文件夹,输入以下命令,为显示隐藏的文件和文件夹 defaults write com.apple.finder AppleShowAll ...
- 【luogu P1073 最优贸易】 题解
题目链接:https://www.luogu.org/problemnew/show/P1073 对于状态量相互影响的题目,分层图是个不错的想法. 考虑在题目中分为: 不交易: 直接从1到n出去,为0 ...
- RabbitMQ + topic发送消息+python
接口使用两个queue监听信息,且有两个测试环境,所以需要向mq中发送测试数据: python使用pika包:Pika is a RabbitMQ (AMQP-0-9-1) client librar ...
- HDU 1284 钱币兑换问题(普通型 数量无限的母函数)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1284 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) ...