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:一种多维数组对象 实话说, ...
随机推荐
- posix进程间的通信
1.无名管道 1.1管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管道: 只能用于父子进程或者兄弟进程之间( ...
- HDU 6206 Apple (高精确度+JAVA BigDecimal)
Problem Description Apple is Taotao's favourite fruit. In his backyard, there are three apple trees ...
- @SuppressWarnings注解用法详解
@SuppressWarnings注解用法详解 今天来谈谈@SuppressWarnings注解的作用. J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条 ...
- linux-资料汇集
1.http://www.debian.org/doc/ 2.鸟哥的私房菜 3.The Linux Command Line by William E. Shotts, Jr. 4.https://d ...
- [转]基于WorldWind平台的建筑信息模型在GIS中的应用
1 引言 随着BIM(Building Information Modeling)的不断发展,建筑信息建模的理念贯穿着建筑.结构.施工.运行维护以及拆迁再规划的整个建筑的生命周期,这种理念不仅使得 ...
- 关闭layer当前弹窗
一. layer关闭弹出层方法1-1) 先获取某个弹出层的 index var index = layer.open(); var index = layer.alert(); var i ...
- Tag It 一款 Jquery控件,当你在文本框中输入逗号时,自动帮你分隔开相关内容
Demo地址:http://webspirited.com/tagit/ 使用方法: 除了JQuery脚本外,下面的脚本也是必须的,这些脚本你都可以去GitHub下载:https://github.c ...
- LeetCode28.实现strStr() JavaScript
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- Java日期类题目
每类题都有各种各样解决的方式,大家随意发散 分析以下需求,并用代码实现 1.已知日期字符串:"2015-10-20",将该日期字符串转换为日期对象 2.将(1)中的日期对象转换为日 ...
- Struts2+hibernate 结合,实现登陆校验
完整的项目在github中,数据库使用postgresql,建表语句见项目文档. 下面我分块介绍一下struts2.hibernate.与页面部分的代码. Struts2 UserAction.jav ...