Python Numpy 笔记

这次机器学习的作业可以用第三方库了,果断抛弃 MATLAB 改用 Python
但是操作数组的 Numpy 之前一直没用过,今天先看看官方教程入个门

The Basics

Numpy 中主要的对象是同类元素组成的多维数组,可以通过一个正整数的元组进行索引。
在 Numpy 中维度(dimension)称为轴(axes),轴的数量称为秩rank

[[1., 0., 0.], [0., 1., 2.]] :rank=2
the first dimension has a length of 2, the second dimension has a length of 3

Numpy 的数组类叫做 ndarray or array
attributes:

  1. ndarray.ndim
  2. ndarray.shape
  3. ndarray.size
  4. ndarray.dtypendarray.dtype.name 返回字符串表示的类型名称
  5. ndarray.data

Example:

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
<type 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)
<type 'numpy.ndarray'>

Array Creation

  1. create from list/tupe:

    • a = np.array([1, 2, 3])
    • b = np.array([[1, 2, 3], [4, 5, 6]], dtype=float)
  2. create with shape:

    • zeros: np.zeros((3, 4))
    • ones: np.ones((2, 4, 3))
    • empty: np.empty((2, 3)) uninitialized
  3. create sequences of number (similar to range()):

    • arange: np.arange(10, 30, 5) syntax is the same as range() but returns array

      接受浮点数,但是由于精度影响,输出的元素个数不确定。这种情况应使用
      linspace

    • linspace: np.linspace(0, 2, 9) return an array contains 9 numbers from 0 to 2

Basic Operations

  1. 算数操作对于 array 是按元素运算的,并返回一个新的 array
>>> 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)
array([[5, 4],
       [3, 4]])
>>> np.dot(A, B)
array([[5, 4],
       [3, 4]])
  1. 一元运算 (sum, min, max)
>>> a = np.random.random((2,3))
>>> a
array([[ 0.18626021,  0.34556073,  0.39676747],
       [ 0.53881673,  0.41919451,  0.6852195 ]])
>>> a.sum()
2.5718191614547998
>>> a.min()
0.1862602113776709
>>> a.max()
0.6852195003967595
>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>
>>> b.sum(axis=0)                            # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1)                            # min of each row
array([0, 4, 8])

Indexing, slicing and iterating

    原文作者:__AllenZ__
    原文地址: https://segmentfault.com/a/1190000013954178
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞