numpy 学习笔记

导入 numpy 包

import numpy as np

声明 ndarray 的几种方法

方法一,从list中创建

l = [[1,2,3], [4,5,6], [7,8,9]]
matrix = np.array(l)
print(matrix)
[[1 2 3]
[4 5 6]
[7 8 9]]

方法二,指定维度,不赋值

matrix = np.ndarray(shape=(3,4))
print(matrix)
[[9.66308774e-312 2.47032823e-322 0.00000000e+000 0.00000000e+000]
[1.89146896e-307 2.42336543e-057 5.88854416e-091 9.41706373e-047]
[5.44949034e-067 1.46609735e-075 3.99910963e+252 3.43567991e+179]]

由上述的输出可见,矩阵内部的值未初始化,其实这都是原来对应内存地址中的数值

方法三,指定维度,初始化成全零的矩阵

matrix = np.zeros(shape=[3,4])
print(matrix)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

方法四,使用默认参数,赋值成从0至arange的一组数

使用默认参数(arange),生成从0至arange的一组数据

matrix = np.arange(12).reshape(3,4)
print(matrix)
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]

方法五,生成随机数数组

arr = np.random.random((1,5))  # 生成 1 行 5 列的一组数
[[ 2.42219258  0.67773029  5.412364    6.21824333  1.2890334 ]]

数值计算

操作全部元素

乘法

print(matrix)
print("after times 10 on every elements:")
print(matrix * 10)
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
after times 10 on every elements:
[[ 0 10 20 30]
[ 40 50 60 70]
[ 80 90 100 110]]

加法

print(matrix)
print("after plus 10 on every elements:")
print(matrix + 10)
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
after plus 10 on every elements:
[[10 11 12 13]
[14 15 16 17]
[18 19 20 21]]

操作部分元素

print(matrix)
print("after times 10 on every elements:")
print(matrix[1] * 10)
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
after times 10 on every elements:
[40 50 60 70]

计算矩阵的秩

m = np.array([[1,2,3], [0,1,2], [0,0,1]])
np.linalg.matrix_rank(m, tol=None)

output:

3

索引部分元素

取一行数据

print(matrix)
print("a line of a matrix:")
print(matrix[1])
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
a line of a matrix:
[4 5 6 7]

取一列数据

以行的形式返回,得到一个行向量

print(matrix)
print("a column of a matrix:")
print(matrix[:,1])
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
a column of a matrix:
[1 5 9]

以列的形式返回,得到一个列向量

print(matrix)
print("a column of a matrix:")
print(matrix[:,1:2])
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]
a column of a matrix:
[[1]
[5]
[9]]

类型转换

astype 方法可以完成类型转换

>>> import numpy as np
>>> x = np.array([0.1, 0.2, 1.2])
>>> x.astype('int')
array([0, 0, 1])

numpy 转 list

numpy 变量自带 tolist 方法

>>> a = np.array([[1, 2], [3, 4]])
>>> a.tolist()
[[1, 2], [3, 4]]

参考资料

《利用python进行数据分析》. https://book.douban.com/subject/25779298/

Numpy. Quickstart tutorial. https://docs.scipy.org/doc/numpy/user/quickstart.html

numpy 学习笔记的更多相关文章

  1. NumPy学习笔记 三 股票价格

    NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...

  2. NumPy学习笔记 二

    NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  3. NumPy学习笔记 一

    NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  4. Numpy学习笔记(下篇)

    目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...

  5. Numpy学习笔记(上篇)

    目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...

  6. Python数据分析:Numpy学习笔记

    Numpy学习笔记 ndarray多维数组 创建 import numpy as np np.array([1,2,3,4]) np.array([1,2,3,4,],[5,6,7,8]) np.ze ...

  7. 数据分析之Pandas和Numpy学习笔记(持续更新)<1>

    pandas and numpy notebook        最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...

  8. numpy学习笔记Ⅰ

    一直被numpy和matplotlib困扰,打算好好学习一下,也是从自己的观点,学对自己帮助最大的部分 主要参考<https: www.runoob.com="" numpy ...

  9. Python numpy学习笔记(一)

    下边代码是关于numpy的一些基本用法,包括数组和矩阵操作等... import numpy as np print "<== print version ==>" p ...

随机推荐

  1. MySQL-UNIQUE

    什么是UNIQUE约束 可以使用 UNIQUE 约束确保在非主键列中不输入重复的值.尽管 UNIQUE 约束和 PRIMARY KEY 约束都强制唯一性,但想要强制一列或多列组合(不是主键)的唯一性时 ...

  2. “无效数字” ;java.lang.Integer cannot be cast to java.lang.String

    今天页面上突然查询不出数据,大致的sql语句是 select xx ,xxx from table a where a.lrmb in ( 6101060033, 61010503300, 61016 ...

  3. 在运行create_list.sh时候报错:AttributeError: 'module' object has no attribute 'LabelMap'

    Traceback (most recent call last):File "/opt/xuben-project/caffe/data/VOC0712/../../scripts/cre ...

  4. WIN32窗口类风格和窗口风格(备查询)

    一.WNDCLASS typedef struct { UINT cbSize //这个结构体的长度,一般用sizeof(WNDCLASSEX)设置 UINT style //窗口式样 WNDPROC ...

  5. numpy.random随机数生成

    seed 确定随机数生成器的种子 permutation 返回一个序列的随机排列或返回一个随机排列的返回 shuffle 对一个序列就地随机乱序 rand 产生均匀分布的样本值 randint 从给定 ...

  6. Impala 学习

    Impala 基础知识介绍与学习,参考文章: Impala-大数据时代快速SQL引擎 https://blog.csdn.net/kangkangwanwan/article/details/7865 ...

  7. 复习loadRunner参数化

    参数化: 为什么要用参数化? 如果是单一数据,那么会纯测试缓存. 如果是参数化,基本上大部分数据不会被缓存命中. 极端情况:所有的数据都不会被缓存命中,或者少量命中. 在loadrunner中,所有的 ...

  8. Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通

    Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通Selenium接口性能自动化测试基础部分:分层自动化思想Slenium介绍Selenium1.0/2.0/3.0Slenium R ...

  9. javamail发送邮件及错误解决方法javax.mail.AuthenticationFailedException: failed to connect, no password specified?

    javamail发送邮件及错误解决方法javax.mail.AuthenticationFailedException: failed to connect, no password specifie ...

  10. Codeforce 513A - Game

    Two players play a simple game. Each player is provided with a box with balls. First player's box co ...