教女朋友学数据分析—科学计算库Numpy

在这里插入图片描述

学习笔记

科学计算库numpy

NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix)),支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

一、IO文件操作

numpy提供了一个打开txt文件的函数genfromtxt,文件:

import numpy 
#delimiter分隔符
world_alcohol = numpy.genfromtxt("G:\\world_alcohol.txt",delimiter=",",dtype="str")
print(type(world_alcohol))
print(world_alcohol)
#打印帮助文档,参看参数
print(help(numpy.genfromtxt))
<class 'numpy.ndarray'>
[['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']
 ['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
 ['1986' 'Americas' 'Uruguay' 'Other' '0.5']
 ..., 
 ['1987' 'Africa' 'Malawi' 'Other' '0.75']
 ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
 ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
Help on function genfromtxt in module numpy.lib.npyio:

genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)
    Load data from a text file, with missing values handled as specified.
    
    Each line past the first `skip_header` lines is split at the `delimiter`
    character, and characters following the `comments` character are discarded.
    
    Parameters
    ----------
    fname : file, str, pathlib.Path, list of str, generator
        File, filename, list, or generator to read.  If the filename
        extension is `.gz` or `.bz2`, the file is first decompressed. Note
        that generators must return byte strings in Python 3k.  The strings
        in a list or produced by a generator are treated as lines.
    dtype : dtype, optional
        Data type of the resulting array.
        If None, the dtypes will be determined by the contents of each
        column, individually.
    comments : str, optional
        The character used to indicate the start of a comment.
        All the characters occurring on a line after a comment are discarded
    delimiter : str, int, or sequence, optional
        The string used to separate values.  By default, any consecutive
        whitespaces act as delimiter.  An integer or sequence of integers
        can also be provided as width(s) of each field.
    skiprows : int, optional
        `skiprows` was removed in numpy 1.10. Please use `skip_header` instead.
    skip_header : int, optional
        The number of lines to skip at the beginning of the file.
    skip_footer : int, optional
        The number of lines to skip at the end of the file.
    converters : variable, optional
        The set of functions that convert the data of a column to a value.
        The converters can also be used to provide a default value
        for missing data: ``converters = {3: lambda s: float(s or 0)}``.
    missing : variable, optional
        `missing` was removed in numpy 1.10. Please use `missing_values`
        instead.
    missing_values : variable, optional
        The set of strings corresponding to missing data.
    filling_values : variable, optional
        The set of values to be used as default when the data are missing.
    usecols : sequence, optional
        Which columns to read, with 0 being the first.  For example,
        ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns.
    names : {None, True, str, sequence}, optional
        If `names` is True, the field names are read from the first valid line
        after the first `skip_header` lines.
        If `names` is a sequence or a single-string of comma-separated names,
        the names will be used to define the field names in a structured dtype.
        If `names` is None, the names of the dtype fields will be used, if any.
    excludelist : sequence, optional
        A list of names to exclude. This list is appended to the default list
        ['return','file','print']. Excluded names are appended an underscore:
        for example, `file` would become `file_`.
    deletechars : str, optional
        A string combining invalid characters that must be deleted from the
        names.
    defaultfmt : str, optional
        A format used to define default field names, such as "f%i" or "f_%02i".
    autostrip : bool, optional
        Whether to automatically strip white spaces from the variables.
    replace_space : char, optional
        Character(s) used in replacement of white spaces in the variables
        names. By default, use a '_'.
    case_sensitive : {True, False, 'upper', 'lower'}, optional
        If True, field names are case sensitive.
        If False or 'upper', field names are converted to upper case.
        If 'lower', field names are converted to lower case.
    unpack : bool, optional
        If True, the returned array is transposed, so that arguments may be
        unpacked using ``x, y, z = loadtxt(...)``
    usemask : bool, optional
        If True, return a masked array.
        If False, return a regular array.
    loose : bool, optional
        If True, do not raise errors for invalid values.
    invalid_raise : bool, optional
        If True, an exception is raised if an inconsistency is detected in the
        number of columns.
        If False, a warning is emitted and the offending lines are skipped.
    max_rows : int,  optional
        The maximum number of rows to read. Must not be used with skip_footer
        at the same time.  If given, the value must be at least 1. Default is
        to read the entire file.
    
        .. versionadded:: 1.10.0
    
    Returns
    -------
    out : ndarray
        Data read from the text file. If `usemask` is True, this is a
        masked array.
    
    See Also
    --------
    numpy.loadtxt : equivalent function when no data is missing.
    
    Notes
    -----
    * When spaces are used as delimiters, or when no delimiter has been given
      as input, there should not be any missing data between two fields.
    * When the variables are named (either by a flexible dtype or with `names`,
      there must not be any header in the file (else a ValueError
      exception is raised).
    * Individual values are not stripped of spaces by default.
      When using a custom converter, make sure the function does remove spaces.
    
    References
    ----------
    .. [1] NumPy User Guide, section `I/O with NumPy
           <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_.
    
    Examples
    ---------
    >>> from io import StringIO
    >>> import numpy as np
    
    Comma delimited file with mixed dtype
    
    >>> s = StringIO("1,1.3,abcde")
    >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
    ... ('mystring','S5')], delimiter=",")
    >>> data
    array((1, 1.3, 'abcde'),
          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
    
    Using dtype = None
    
    >>> s.seek(0) # needed for StringIO example only
    >>> data = np.genfromtxt(s, dtype=None,
    ... names = ['myint','myfloat','mystring'], delimiter=",")
    >>> data
    array((1, 1.3, 'abcde'),
          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
    
    Specifying dtype and names
    
    >>> s.seek(0)
    >>> data = np.genfromtxt(s, dtype="i8,f8,S5",
    ... names=['myint','myfloat','mystring'], delimiter=",")
    >>> data
    array((1, 1.3, 'abcde'),
          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
    
    An example with fixed-width columns
    
    >>> s = StringIO("11.3abcde")
    >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
    ...     delimiter=[1,3,5])
    >>> data
    array((1, 1.3, 'abcde'),
          dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])

None

二、创建数组

2.1 array()函数:

The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:

vector = numpy.array([5, 10, 15, 20])
print(vector)
[ 5 10 15 20]

When we input a list of lists, we get a matrix as a result:

matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(matrix)
    [[ 5 10 15]
     [20 25 30]
     [35 40 45]]

2.1 创建零、一矩阵

创建零矩阵:

np.zeros ((3,4)) 
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

元素全为1的矩阵,定指定数据类型为int:

np.ones( (2,3,4), dtype=np.int32 )
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]]])

2.3 创建等差数列

#To create sequences of numbers
np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
np.arange( 0, 2, 0.3 )
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])
np.arange(12).reshape(4,3)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
from numpy import pi
np.linspace( 0, 2*pi, 100 )
array([ 0.        ,  0.06346652,  0.12693304,  0.19039955,  0.25386607,
        0.31733259,  0.38079911,  0.44426563,  0.50773215,  0.57119866,
        0.63466518,  0.6981317 ,  0.76159822,  0.82506474,  0.88853126,
        0.95199777,  1.01546429,  1.07893081,  1.14239733,  1.20586385,
        1.26933037,  1.33279688,  1.3962634 ,  1.45972992,  1.52319644,
        1.58666296,  1.65012947,  1.71359599,  1.77706251,  1.84052903,
        1.90399555,  1.96746207,  2.03092858,  2.0943951 ,  2.15786162,
        2.22132814,  2.28479466,  2.34826118,  2.41172769,  2.47519421,
        2.53866073,  2.60212725,  2.66559377,  2.72906028,  2.7925268 ,
        2.85599332,  2.91945984,  2.98292636,  3.04639288,  3.10985939,
        3.17332591,  3.23679243,  3.30025895,  3.36372547,  3.42719199,
        3.4906585 ,  3.55412502,  3.61759154,  3.68105806,  3.74452458,
        3.8079911 ,  3.87145761,  3.93492413,  3.99839065,  4.06185717,
        4.12532369,  4.1887902 ,  4.25225672,  4.31572324,  4.37918976,
        4.44265628,  4.5061228 ,  4.56958931,  4.63305583,  4.69652235,
        4.75998887,  4.82345539,  4.88692191,  4.95038842,  5.01385494,
        5.07732146,  5.14078798,  5.2042545 ,  5.26772102,  5.33118753,
        5.39465405,  5.45812057,  5.52158709,  5.58505361,  5.64852012,
        5.71198664,  5.77545316,  5.83891968,  5.9023862 ,  5.96585272,
        6.02931923,  6.09278575,  6.15625227,  6.21971879,  6.28318531])
np.sin(np.linspace( 0, 2*pi, 100 ))
array([  0.00000000e+00,   6.34239197e-02,   1.26592454e-01,
         1.89251244e-01,   2.51147987e-01,   3.12033446e-01,
         3.71662456e-01,   4.29794912e-01,   4.86196736e-01,
         5.40640817e-01,   5.92907929e-01,   6.42787610e-01,
         6.90079011e-01,   7.34591709e-01,   7.76146464e-01,
         8.14575952e-01,   8.49725430e-01,   8.81453363e-01,
         9.09631995e-01,   9.34147860e-01,   9.54902241e-01,
         9.71811568e-01,   9.84807753e-01,   9.93838464e-01,
         9.98867339e-01,   9.99874128e-01,   9.96854776e-01,
         9.89821442e-01,   9.78802446e-01,   9.63842159e-01,
         9.45000819e-01,   9.22354294e-01,   8.95993774e-01,
         8.66025404e-01,   8.32569855e-01,   7.95761841e-01,
         7.55749574e-01,   7.12694171e-01,   6.66769001e-01,
         6.18158986e-01,   5.67059864e-01,   5.13677392e-01,
         4.58226522e-01,   4.00930535e-01,   3.42020143e-01,
         2.81732557e-01,   2.20310533e-01,   1.58001396e-01,
         9.50560433e-02,   3.17279335e-02,  -3.17279335e-02,
        -9.50560433e-02,  -1.58001396e-01,  -2.20310533e-01,
        -2.81732557e-01,  -3.42020143e-01,  -4.00930535e-01,
        -4.58226522e-01,  -5.13677392e-01,  -5.67059864e-01,
        -6.18158986e-01,  -6.66769001e-01,  -7.12694171e-01,
        -7.55749574e-01,  -7.95761841e-01,  -8.32569855e-01,
        -8.66025404e-01,  -8.95993774e-01,  -9.22354294e-01,
        -9.45000819e-01,  -9.63842159e-01,  -9.78802446e-01,
        -9.89821442e-01,  -9.96854776e-01,  -9.99874128e-01,
        -9.98867339e-01,  -9.93838464e-01,  -9.84807753e-01,
        -9.71811568e-01,  -9.54902241e-01,  -9.34147860e-01,
        -9.09631995e-01,  -8.81453363e-01,  -8.49725430e-01,
        -8.14575952e-01,  -7.76146464e-01,  -7.34591709e-01,
        -6.90079011e-01,  -6.42787610e-01,  -5.92907929e-01,
        -5.40640817e-01,  -4.86196736e-01,  -4.29794912e-01,
        -3.71662456e-01,  -3.12033446e-01,  -2.51147987e-01,
        -1.89251244e-01,  -1.26592454e-01,  -6.34239197e-02,
        -2.44929360e-16])

2.4 创建随机数组

创建一个两行三列的从-1到1的随机数组:

np.random.random((2,3))
array([[ 0.90544953,  0.61800127,  0.78889816],
       [ 0.11201221,  0.98374787,  0.67135473]])

三、数组属性

3.1 shape()函数

这是我最常用的函数,不论是python还是R。当然,在R中,是dim。

#We can use the ndarray.shape property to figure out how many elements are in the array
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#For matrices, the shape property contains a tuple with 2 elements.
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
(4,)
(2, 3)

3.2 dtype函数

注意:Each value in a NumPy array has to have the same data type
NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays.
#You can check the data type of a NumPy array using the dtype property.

numbers = numpy.array([1, 2, 3, 4])
print(numbers)
numbers.dtype 
[1 2 3 4]

dtype('int32')
numbers = numpy.array([1, 2, 3, '4'])
print(numbers)
numbers.dtype
['1' '2' '3' '4']

dtype('<U11')
numbers = numpy.array([1, 2, 3, 4.0])
print(numbers)
numbers.dtype 
[ 1.  2.  3.  4.]

dtype('float64')

3.3 shape函数

#修改numpy别名为np
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)

3.4 ndim函数

#the number of axes (dimensions) of the array
a.ndim
2

3.5 dtype.name函数

a.dtype.name
'int32'

3.6 size函数

#the total number of elements of the array
a.size
15

四、切片和索引

ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样。

4.1 取矩阵中某一或某些数元素

通过下标来获得某一个数据元素:

world_alcohol = numpy.genfromtxt("G:\\world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
#print(world_alcohol)
uruguay_other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print(uruguay_other_1986)
print(third_country)
0.5
Cte d'Ivoire

通过指定下标范围来获得一组序列的元素:

vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])  
[ 5 10 15]

取矩阵中的某一列:

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,1])
[10 25 40]

取矩阵中的某几列:

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,0:2])
[[ 5 10]
 [20 25]
 [35 40]]

取矩阵中的几行,某几列:

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[1:3,0:2])
[[20 25]
 [35 40]]

4.2 索引:

寻找每列最大值的索引,从而得到每列最大值:

import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print(data)
ind = data.argmax(axis=0)
print(ind)
data_max = data[ind, range(data.shape[1])]
print(data_max)
all(data_max == data.max(axis=0))
[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[2 0 3 1]
[ 0.98935825  0.84147098  0.99060736  0.6569866 ]

True

五、判断是否存在给定的某值

import numpy
#it will compare the second value to each element in the vector
# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = numpy.array([5, 10, 15, 20])
vector == 10
array([False,  True, False, False], dtype=bool)
matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
matrix == 25
array([[False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

返回的布尔值也可以当成一个索引,会返回布尔值为TRUE对应元素:

#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])
[False  True False False]
[10]

返回矩阵中第2列值为25所在行的行数据:

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])
[False  True False]
[[20 25 30]]

六、位运算

6.1 与操作:

#We can also perform comparisons with multiple conditions
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print(equal_to_ten_and_five)
[False False False False]

6.2 或操作:

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print(equal_to_ten_or_five)
[ True  True False False]

将与、或操作返回的布尔值作为索引:

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
vector[equal_to_ten_or_five] = 50
print(vector)
[50 50 15 20]
matrix = numpy.array([
            [5, 10, 15], 
            [20, 25, 30],
            [35, 40, 45]
         ])
second_column_25 = matrix[:,1] == 25
print(second_column_25)
matrix[second_column_25, 1] = 10
print(matrix)
[False  True False]
[[ 5 10 15]
 [20 10 30]
 [35 40 45]]

七、统计函数

7.1 最小值:

vector = numpy.array([5, 10, 15, 20])
vector.min()
5

7.2 最大值:

vector = numpy.array([5, 10, 15, 20])
vector.max()
20

7.3 求和

按行求和:

# The axis dictates which dimension we perform the operation on
#1 means that we want to perform the operation on each row, and 0 means on each column
matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=1)
array([ 30,  75, 120])

按列求和:

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=0)
array([60, 75, 90])

7.4 排序

a = np.array([[4, 3, 5], [1, 2, 1]])
print(a)
b = np.sort(a, axis=1)
print(b)
b
a.sort(axis=1)
print(a)
[[4 3 5]
 [1 2 1]]
[[3 4 5]
 [1 1 2]]
[[3 4 5]
 [1 1 2]]
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print(j)
print(a[j])
[2 3 1 0]
[1 2 3 4]

八、数组操作

8.1 矩阵数据类型转换:

#Each value in a NumPy array has to have the same data type
#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. 
#You can check the data type of a NumPy array using the dtype property.
numbers = numpy.array([1, 2, 3, 4])
print(numbers)
numbers.dtype
[1 2 3 4]

dtype('int32')
numbers = numpy.array([1, 2, 3, 4.0])
print(numbers)
numbers.dtype
[ 1.  2.  3.  4.]

dtype('float64')
numbers = numpy.array([1, 2, 3, '4'])
print(numbers)
numbers.dtype
['1' '2' '3' '4']

dtype('<U11')

8.2 修改数组形状:

#修改numpy别名为np
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]])
#Return the floor of the input
a = np.floor(10*np.random.random((3,4)))
#a.shape
## flatten the array
# 将矩阵拉平为向量:
#print a.ravel()
a.shape = (6, 2)
#print a 
#print a.T
#print(a.resize((2,6)))
print(a)

#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
#a.reshape(3,-1)
[[ 0.  1.]
 [ 2.  6.]
 [ 1.  9.]
 [ 2.  3.]
 [ 3.  7.]
 [ 2.  5.]]

8.3 矩阵合并:

行合并:

a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print('---')
print(b)
print('---')
print(np.hstack((a,b)))
[[ 3.  4.]
 [ 3.  5.]]
---
[[ 7.  2.]
 [ 9.  1.]]
---
[[ 3.  4.  7.  2.]
 [ 3.  5.  9.  1.]]

列合并:

print(np.vstack((a,b)))
[[ 0.  1.]
 [ 2.  6.]
 [ 1.  9.]
 [ 2.  3.]
 [ 3.  7.]
 [ 2.  5.]
 [ 7.  2.]
 [ 9.  1.]]

8.4 矩阵分割

行切割:

a = np.floor(10*np.random.random((2,12)))
print('a:')
print(a)
print('行切割结果:')
print(np.hsplit(a,3))
a:
[[ 8.  6.  2.  5.  6.  6.  7.  1.  5.  5.  9.  3.]
 [ 7.  6.  8.  3.  4.  1.  4.  8.  6.  6.  7.  8.]]
行切割结果:
[array([[ 8.,  6.,  2.,  5.],
       [ 7.,  6.,  8.,  3.]]), array([[ 6.,  6.,  7.,  1.],
       [ 4.,  1.,  4.,  8.]]), array([[ 5.,  5.,  9.,  3.],
       [ 6.,  6.,  7.,  8.]])]

指定切割位置:

print(np.hsplit(a,(3,4)))   # Split a after the third and the fourth column
[array([[ 8.,  6.,  2.],
       [ 7.,  6.,  8.]]), array([[ 5.],
       [ 3.]]), array([[ 6.,  6.,  7.,  1.,  5.,  5.,  9.,  3.],
       [ 4.,  1.,  4.,  8.,  6.,  6.,  7.,  8.]])]

列切割:

a = np.floor(10*np.random.random((12,2)))
print(a)
np.vsplit(a,3)
[[ 7.  1.]
 [ 1.  6.]
 [ 1.  7.]
 [ 7.  4.]
 [ 8.  9.]
 [ 4.  3.]
 [ 8.  0.]
 [ 7.  7.]
 [ 8.  4.]
 [ 7.  5.]
 [ 2.  5.]
 [ 8.  2.]]


[array([[ 7.,  1.],
        [ 1.,  6.],
        [ 1.,  7.],
        [ 7.,  4.]]), array([[ 8.,  9.],
        [ 4.,  3.],
        [ 8.,  0.],
        [ 7.,  7.]]), array([[ 8.,  4.],
        [ 7.,  5.],
        [ 2.,  5.],
        [ 8.,  2.]])]

8.5 矩阵扩展

a = np.arange(0, 40, 10)
b = np.tile(a, (3, 5)) 
print(b)
[[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]]

九、对矩阵元素进行数学运算

9.1 幂指运算

#the product operator * operates elementwise in NumPy arrays
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
print(a) 
print(b)
c = a-b
print(c)
#求平方
b**2
#print(b**2)
print(a<35)
[20 30 40 50]
[0 1 2 3]
[20 29 38 47]
[ True  True False False]

对矩阵元素求幂、开根号等数学运算:

import numpy as np
B = np.arange(3)
print('B:')
print(B)
print('np.exp(B):')
print(np.exp(B))
print('np.sqrt(B):')
print(np.sqrt(B))
B:
[0 1 2]
np.exp(B):
[ 1.          2.71828183  7.3890561 ]
np.sqrt(B):
[ 0.          1.          1.41421356]

9.2 向下取整

#Return the floor of the input
a = np.floor(10*np.random.random((3,4)))
print(a)
[[ 8.  4.  9.  3.]
 [ 8.  2.  7.  4.]
 [ 0.  0.  4.  3.]]

9.3 矩阵乘法

#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
               [0,1]] )
B = np.array( [[2,0],
               [3,4]] )
print('A:')
print(A)
print('B:')
print(B)
#A*B 指的是对应位置相乘
print('A*B:')
print(A*B)
# A.dot(B)或者np.dot(A, B)指的是我们实际所学的矩阵乘法:
print('A.dot(B):')
print(A.dot(B))
print('np.dot(A, B):')
print(np.dot(A, B)) 
A:
[[1 1]
 [0 1]]
B:
[[2 0]
 [3 4]]
A*B:
[[2 0]
 [0 4]]
A.dot(B):
[[5 4]
 [3 4]]
np.dot(A, B):
[[5 4]
 [3 4]]

十、复制

Simple assignments make no copy of array objects or of their data.

a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
print(b is a)
b.shape = 3,4
print(a.shape)
print(id(a))
print(id(b))
True
(3, 4)
124979680
124979680

The view method creates a new array object that looks at the same data.

c = a.view()
c is a
c.shape = 2,6
#print a.shape
c[0,4] = 1234
a
array([[   0,    1,    2,    3],
       [1234,    5,    6,    7],
       [   8,    9,   10,   11]])

The copy method makes a complete copy of the array and its data.

d = a.copy() 
d is a
d[0,0] = 9999
print(d) 
print(a)
[[9999    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]

End

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值