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的更多相关文章

  1. Numpy | 01 简介

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...

  2. 01. Numpy模块

    1.科学计算工具-Numpy基础数据结构 1.1.数组ndarray的属性 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成:① 实际的数据② 描述这些数据的元数据 注意数组格式, ...

  3. [Pandas] 01 - A guy based on NumPy

    主要搞明白NumPy“为什么快”. 学习资源 Panda 中文 易百教程 远程登录Jupyter笔记本 效率进化 四步效率优化 NumPy 底层进行了不错的优化. %timeit 对于任意语句,它会自 ...

  4. 数据分析01 /numpy模块

    数据分析01 /数据分析之numpy模块 目录 数据分析01 /数据分析之numpy模块 1. numpy简介 2. numpy的创建 3. numpy的方法 4. numpy的常用属性 5. num ...

  5. 18-09-21 numpy 的基础学习01

    # 1关于numpy 的学习import numpy as np # 一 如何创建数组****# 1 有规律的一维数据的创建======# 1 range() 和arange() 区别 貌似没有区别l ...

  6. 01 numpy库(一)

    01-numpy NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行 ...

  7. 01.Numpy数组的基本应用

    数组的创建 数组的访问 数组的合并 数组的分割 数组创建 >>> import numpy as np 创建一维数组 >>> x = np.arange(10) & ...

  8. numpy学习笔记 01

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...

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

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

随机推荐

  1. python调用chrome打开指定网址

    #!/usr/bin/python # -*- coding:utf-8 -*- import os f = open("chrome_cmd_path.txt") chrome ...

  2. php红包生成随机算法

    一.适用场景 红包总金额X,分配成Y个红包,每个红包随机金额. 二.生成算法 /** * 红包生成算法 * @param $money 总金额 * @param $number 红包数量 * @par ...

  3. backtrack渗透测试中常用的命令总结

    ping 域名/ip 测试本机到远端主机是否联通. dig 域名/ip 查看域名解析的详细信息. host -l 域名 dns服务器 传输zone. 扫描 nmap: -sS 半开扫描TCP和SYN扫 ...

  4. c++ auto_ptr超简易版实现

    namespace wang{ template<class T> class shared_ptr{ public: explicit shared_ptr(T *p) : count( ...

  5. Java虚拟机5:常用JVM命令参数

    这里汇总一些平时用到的.看到的一些虚拟机参数: (1)-Xms20M 表示设置堆容量的最小值为20M,必须以M为单位 (2)-Xmx20M 表示设置堆容量的最大值为20M,必须以M为单位.将-Xmx和 ...

  6. canvas的两个方法说明

    今天在用canvas的时候,发现有两个方法比较陌生,在此记录详细说明一下. (1)文本绘制的一个方法 canvas.drawTextOnPath(text, path, hOffset, vOffse ...

  7. Python解析配置文件模块:ConfigPhaser

    算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...

  8. BZOJ1879:[SDOI2009]Bill的挑战(状压DP)

    Description Input 本题包含多组数据.  第一行:一个整数T,表示数据的个数.  对于每组数据:  第一行:两个整数,N和K(含义如题目表述).  接下来N行:每行一个字符串. T ≤ ...

  9. wampserver 最新版本 mysql修改数据库密码

    由于wampsever版本更新就导致以前版本的密码修改造成失败,主要是密码字段改变造成的! 第一步 进入MySQL 控制台  wamp安装,数据库是没有密码 进入控制台直接回车就可以了 第二步 使用 ...

  10. shiro集成spring&工作流程&DelegatingFilterProxy

    1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...