The Basics of Numpy
在python语言中,Tensorflow中的tensor返回的是numpy ndarray对象。
Numpy的主要对象是齐次多维数组,即一个元素表(通常是数字),所有的元素具有相同类型,可以通过有序整数列表元组tuple访问其元素。In Numpy, dimensions are called axes. The number of axes is rank.
Numpy的数组类为ndarray,它还有一个名气甚大的别名array。需要注意的是:numpy.array与python标准库中的array.array并不完全相同,后者仅仅处理一维数组而且提供的函数功能较少。
比较重要的一些ndarray数组的属性:
ndarray.ndim: the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.ndarray.shape:the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be(n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.ndarray.size:the total number of elements of the array. This is equal to the product of the elements of shape.ndarray.dtype:an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own.numpy.int32,numpy.int16, andnumpy.float64are some examples.ndarray.itemsize:the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.ndarray.data:the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
An Example
import numpy as np
a = np.arange(15).reshape(3, 5)
print a
print a.ndim
print a.shape
print a.size
print a.dtype
print a.itemsize
# print
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
2
(3, 5)
15
int64
8
Array Creation:
>>> a = np.array(1,2,3,4) # WRONG
>>> a = np.array([1,2,3,4]) # RIGHT
>>> b = np.array([(1.5,2,3), (4,5,6)])
>>> b
array([[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> c
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j]])
>>> np.zeros( (3,4) )
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]],
[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) ) # uninitialized, output may vary
array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260],
[ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
Basic Operations
>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])
>>> a<35
array([ True, True, False, False], dtype=bool)
>>> A = np.array( [[1,1],
... [0,1]] )
>>> B = np.array( [[2,0],
... [3,4]] )
>>> A*B # elementwise product
array([[2, 0],
[0, 4]])
>>> A.dot(B) # matrix product
array([[5, 4],
[3, 4]])
>>> np.dot(A, B) # another matrix product
array([[5, 4],
[3, 4]])
>>> a = np.ones((2,3), dtype=int)
>>> b = np.random.random((2,3))
>>> a *= 3
>>> a
array([[3, 3, 3],
[3, 3, 3]])
>>> b += a
>>> b
array([[ 3.417022 , 3.72032449, 3.00011437],
[ 3.30233257, 3.14675589, 3.09233859]])
>>> a += b # b is not automatically converted to integer type
# Traceback (most recent call last):
# ...
# TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
更多内容请阅读:https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
The Basics of Numpy的更多相关文章
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)
Python Basics with numpy (optional)Welcome to your first (Optional) programming exercise of the deep ...
- Python Basics with numpy (optional)
Python Basics with Numpy (optional assignment) Welcome to your first assignment. This exercise gives ...
- Python Basics with Numpy
Welcome to your first assignment. This exercise gives you a brief introduction to Python. Even if yo ...
- PyTorch(一)Basics
PyTorch Basics import torch import torchvision import torch.nn as nn import numpy as np import torch ...
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 4、Logistic Regression with a Neural Network mindset
Logistic Regression with a Neural Network mindset Welcome to the first (required) programming exerci ...
- 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week2 Neural Networks Basics课堂笔记
Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week2 Neural Networks Basics 2.1 ...
- 【Python】numpy 数组拼接、分割
摘自https://docs.scipy.org 1.The Basics 1.1 numpy 数组基础 NumPy’s array class is called ndarray. ndarray. ...
- numpy基本用法
numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...
- numpy快速指南
Quickstart tutorial 引用https://docs.scipy.org/doc/numpy-dev/user/quickstart.html Prerequisites Before ...
随机推荐
- 2014年辛星解读css第六节
这一节我们就要讲到布局了,事实上布局本身特别简单.可是要合理的布好局就不那么简单了,就像我们写文章一样.写一篇文章非常easy,可是要写一篇名著就非常难了,这须要我们扎实的功底和对文学的理解,可是.千 ...
- 使用imgareaselect 辅助后台进行图片裁剪
由于项目其中用到图片裁剪,本来能够不用到后台进行裁剪的,可是要兼容万恶的IE浏览器,所以不得不使用后台进行裁剪. 这次使用到imgareaselect 插件获取须要裁剪区域的坐标.再由后台进行裁剪操作 ...
- HTML的表单form以及form内部标签
<html> <head> <title> form表单的使用 </title> <!-- 标签名称:form 表单标签 属性:action:提交 ...
- Maven与IDEA结合
转自:https://blog.csdn.net/zzpzheng/article/details/49474671 1. 什么是 Maven,为什么要使用 Maven 而不是 Ant Maven简单 ...
- NOIP 2010 关押罪犯 并查集 二分+二分图染色
题目描述: S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值" ...
- NOIP 2012 D1T1 Vigenère密码
嗯嗯 一道找规律的题.... 真佩服那些把表打出来的人 //By SiriusRen #include <cstdio> #include <cstring> using na ...
- POJ 2299 求逆序对个数 归并排序 Or数据结构
题意: 求逆序对个数 没有重复数字 线段树实现: 离散化. 单点修改,区间求和 // by SiriusRen #include <cstdio> #include <cstring ...
- ueditor和flexpaper的学习。。。。
博客园的博主编辑文本的时候在博客园编辑器上编辑的,最近见到并学习了一点百度一款ueditor的编辑器可供程序猿们二次开发.... 见链接http://fex.baidu.com/ueditor/#st ...
- vue-cli 安装
1 node 下载 http://nodejs.cn/download/ 安装 2 npm install vue-cli -g 3 vue init <template-n ...
- 【技术累积】【点】【java】【8】maven常用命令(持续更新)
建立 mvn archetype:generate -DgroupId=com.andy.test -DartifactId=test-project -Dversion=0.0.1-SNAPSHOT ...