4 Arrays and Matrices

4.1 Arrays

this chapter is about homogeneous arrays containing numeric data( an array where all elements have the same numeric type)

1-dimensional arrays:

>>> x = [0.0, 1, 2, 3, 4]
>>> y = array(x)
>>> y
array([ 0.,  1.,  2.,  3.,  4.])
>>> type(y)
numpy.ndarray

2-dimensional arrays are initialized by nested lists:

>>> y = array([[0.0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
>>> y
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])
>>> shape(y)
(2L, 5L)
>>> y = array([[[1,2],[3,4]],[[5,6],[7,8]]])
>>> y
array([[[1, 2],
[3, 4]],
       [[5, 6],
        [7, 8]]])
>>> shape(y)
(2L, 2L, 2L)

4.1.4 Array dtypes

homogeneous arrays can contain a varity of numeric data types. all int → 'int32', int and floats →'float64'

NumPy attampts to find the smallest data type which can represent the data when constructing an array. You can use the keyword arguemnt dtype=datetype to select a particular dtype

>>> x = [0, 1, 2, 3, 4]
>>> y = array(x, dtype = 'float64')
>>>y.dtype
dtype('float64')

4.2 Matrix

Matrices are a subset of arrays but always have 2 dimensions and follow the rules of linear algebra for *.

function mat or asmatrixcan generate a matrix:

>>> x = [0.0, 1, 2, 3, 4] 
>>> z = asmatrix(x)
>>> type(z)
numpy.matrixlib.defmatrix.matrix

4.3 1-dimensional Arrays

the difference

>>> w = array([[1.0, 2.0, 3.0, 4.0]])
>>> shape(w)
(1, 4)
>>> q = array([1.0, 2.0, 3.0, 4.0])
>>> shape(q)
(4,)

w is a 2x2 matrix while q is only a vector. Or q is a 1-dimensional array.

4.4 2-dimensional Arrays

two methods:

4.4.1 array()

>>> x=array([[1.0,2.0,3.0,4.0,5.0]])
array([[ 1.,  2.,  3.,  4.,  5.]])
>>> ndim(x) 2

4.4.2 matrix()

matrix() need only one[], while array() need two to make a nested list.

>>>x = matrix([1.0, 2.0, 3.0, 4.0, 5.0])
>>>x
matrix([[ 1.,  2.,  3.,  4.,  5.]])
>>> ndim(x) 2

4.4.3 general 2-dimensional Arrays

>>> x = array([[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]])
>>> x
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

to generate a 3x3 matrix

>>> x=matrix([[1.0],[2.0],[3.0],[4.0],[5.0]])
>>> x
matrix([[ 1.],
        [ 2.],
        [ 3.],
        [ 4.],
        [ 5.]])
>>> x = array([[1.0],[2.0],[3.0],[4.0],[5.0]])
>>> x
array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.]])

to generate a 5x1 matrix.

4.5 Multidimensional Arrays

Multidimensional arrays are available for N up to about 30, depending on the size of each matrix dimension. Function such as zeros((2,2,2)) and empty((2, 2, 2)) can be used to avoid tedious work and error.

4.6 Concatenation

concatenation is the process by which one vector or matrix is appended to another.

function1:concatenate, and the keyword argument axis = 0 if arrays are to be vertically or axis = 1 if horizontally.

>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> y = array([[5.0,6.0],[7.0,8.0]])
>>> z = concatenate((x,y),axis = 0)
>>> z
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.],
       [ 7.,  8.]])
>>> z = concatenate((x,y),axis = 1)
>>> z
array([[ 1.,  2.,  5.,  6.],
       [ 3.,  4.,  7.,  8.]])

functions2: vstack and hstack

>>> z = vstack((x,y)) # Same as z = concatenate((x,y),axis = 0)
>>> z = hstack((x,y)) # Same as z = concatenate((x,y),axis = 1)

4.7 Accessing Elements of an Array

four ways: Scalar selection, slicing, numerical indexing and logical( or Boolean) indexing.

4.7.1 Scalar Selection

x[i] for 1-dimensional arrays

[i, j] for 2-dimensional arrays

and [i1, i2, i3…in] for general n-dimensional arrays

>>> x = array([1.0,2.0,3.0,4.0,5.0])
>>> x[0]
1.0
>>> x = array([[1.0,2,3],[4,5,6]])
>>> x[1,2]
6.0
>>> type(x[1,2])
numpy.float64

note: index begin from 0

Scalar selection can also be used to assign values in an array.

>>> x = array([1.0,2.0,3.0,4.0,5.0])
>>> x[0] = -5
>>> x
array([-5.,  2.,  3.,  4.,  5.])

4.7.2 Array slicing

[:,:,…,:]to slice

standard: `[a:b:s] 1-dimension select every sTH elements where the indices i satisfy a<=i <b so that starting value a is always inclued in the list and the ending value b is always excluded.

for short:

  • : and :: are the same as 0:n:1
  • a: and a:n are the same as a:n:1 where n is the length of the array
  • :b is the same as 0:b:1
    -::s is the same as 0:n:s where n is the length of the array.

to dimension:

y[a:b, c:d] is the same as y[a:b,:] or y[a:b][:,c:d]

4.7.3 Mixed Selection using Scalar and Slice Selectors

>>> x = array([[0.0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
>>> x[:1,:] # Row 0, all columns, 2-dimensional
array([[ 0.,  1.,  2.,  3.,  4.]])
>>> ndim(x[:1,:])
2
>>> x[0,:] # Row 0, all column, dim reduction to 1-d array
array([ 0.,  1.,  2.,  3.,  4.])
>>> ndim(x[0,:])
1
>>> x[0,0] # Top left element, dim reduction to scalar (0-d array)
0.0
>>> ndim(x[0,0])
0
>>> x[:,0] # All rows, 1 column, dim reduction to 1-d array
array([ 0., 5.])

y[:1,:] return a 2-dimension array, while y[0,:] return 1-dimension array and y[0, 0] return a scalar( or 0-dimensional array)

4.7.4 Assignment using Slicing

noteto place a float into the array results in the float being truncated and stored as an integer.

arrays should be initialized to contain floats unless a considered decision is taken to use a different data type.

4.7.5 Linear Slicing using flat

In 2-dimensions, linear slicing works by first counting across rows, and then down columns. To use linear slicing, the method or function flat must first be used.

>>> y = reshape(arange(25.0),(5,5))
>>> y
array([[  0.,   1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.,  14.],
       [ 15.,  16.,  17.,  18.,  19.],
       [ 20.,  21.,  22.,  23.,  24.]])
>>> y.flat[0] # Scalar slice, flat is 1-dimensional
0
>>> y[6] # Error
IndexError: index out of bounds
>>> y.flat[6] # Element 6
6.0
>>> y.flat[12:15]
array([ 12.,  13.,  14.])
>>> y.flat[:] # All element slice
array([[   0.,   1.,   2.,   3.,   4.,
                               5.,   6.,   7.,
11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,
22.,  23.,  24.]])

4.8 Slicing and Memory Management

slice of arrays do not copy the underlying data.changes to silce will change underlying array.

to avoid change of slice to underlying array, three methods can be applied.

>>> x = reshape(arange(4.0),(2,2))
>>> s1 = copy(x[0,:]) # Function copy
>>> s2 = x[:,0].copy() # Method copy
>>> s3 = array(x[0,:]) # Create a new array

while using pure scalar selection value will return a copy.

>>> x = arange(5.0)
>>> y = x[0] # Pure scalar selection
>>> z = x[:1] # A pure slice
>>> y = -3.14
>>> y # y Changes
-3.14
>>> x # No propagation
array([ 0.,  1.,  2.,  3.,  4.])
>>> z  # No changes to z either
array([ 0.])
>>> z[0] = -2.79
>>> y # No propagation since y used pure scalar selection
-3.14
>>> x # z is a view of x, so changes propagate
array([-2.79,  1.  ,  2.  ,  3.  ,  4.  ])

assignments from functions which change values will automatically creat a copy of the underlying array.

>>> x = array([[0.0, 1.0],[2.0,3.0]])
>>> y = x
>>> print(id(x),id(y)) # Same
 129186368 129186368
>>> y = x + 1.0
>>> y
array([[ 1.,  2.],
[ 3., 4.]])
>>> print(id(x),id(y)) # Different
129186368 129183104
>>> x # Unchanged
array([[ 0.,  1.],
[ 2., 3.]])
>>> y = exp(x)
>>> print(id(x),id(y)) # Also Different
129186368 129185120

y = x + 0.0 can be used to copy x and change to y should not propagate to x.

4.9 import and Modules

from pylab import *

dangerous bacause function confllict.

import pylab

call function:pylab.log2

for short:

import pylab as pl

4.10 Calling Functions

function can take more than one input and return more than one output. `out1, out2, out3,… = functionname( in1, in2, in3,…)

  • if multiple output are returned, but only one output variable is provided, the output will be a tuple.
  • if more than one output variable is given in a function call, the number of output must match the number of output provided by the function.
  • both input and outputs must be separated by commas( , )
  • inputs can be the result of other functions.

Required Arguments

array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

object: required input

keyword arguments: dtype, copy, order, subok, ndmin

Keyword Arguments

  1. do NOT have to appear in order.(BUT you'd better list in order)
  2. do not to use when not needed.

Multiple Outputs

when a function can generate mutiple outputs, functions can be used in a single output mode or in multiple mode.

>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> s = shape(x)
>>> s
(2L, 2L)
>>> x = array([[1.0,2.0],[3.0,4.0]])
>>> M,N = shape(x)
>>> M
2L
>>> N 2L
>>> M,N,P = shape(x) # Error
ValueError: need more than 2 values to unpack
>>> x = randn(10,10,10)
>>> shape(x)
(10L, 10L, 10L)
>>> M,N = shape(x) # Error
ValueError: too many values to unpack
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容