Numpy基础学习1

Numpy basical operation1

结合"Python for Data Analysis(2nd)", 同步练习Numpy基本操作

  • 常用的数组矢量运算
// 导入numpy包 起个别名为 np
import numpy as np

world_alcohol = np.genfromtxt("world_alcohol.txt", delimiter=",", dtype=str)
print(type(world_alcohol))
print(world_alcohol) # ndarray
print(help(np.genfromtxt))
<class 'np.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, encoding='bytes')
    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 line after
        the first `skip_header` lines.  This line can optionally be proceeded
        by a comment delimeter. 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
    encoding : str, optional
        Encoding used to decode the inputfile. Does not apply when `fname` is
        a file object.  The special value 'bytes' enables backward compatibility
        workarounds that ensure that you receive byte arrays when possible
        and passes latin1 encoded strings to converters. Override this value to
        receive unicode arrays and pass strings as input to converters.  If set
        to None the system default is used. The default value is 'bytes'.
    
        .. versionadded:: 1.14.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')])
# The np.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 = np.array([5, 10, 15, 20]) # list->ndarray
# When we input a list of lists, we get a matrix as a result:
matrix = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
triple_matrix = np.array([[[1,2,3], [4,5,6], [7,8,9], [0,1,2], [0,1,2], [0,1,2], [0,1,2], [0,1,2], [0,1,2]]])
print(vector)
print(matrix)
print(triple_matrix)

[ 5 10 15 20]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]
[[[1 2 3]
  [4 5 6]
  [7 8 9]
  [0 1 2]
  [0 1 2]
  [0 1 2]
  [0 1 2]
  [0 1 2]
  [0 1 2]]]
# We can use the ndarray.shape property to figure out how many elements are in the array
vector = np.array([1, 2, 3, 4]) #  一位数组-->行向量
print(vector.shape) # 表达向量或者矩阵的结构 在神经网络的测试环节很有用处 方便理解 debug
# For matrices, the shape property contains a tuple with 2 elements.
matrix = np.array([[5, 10, 15], [20, 25, 30]]) # 二维数组->矩阵 2行3列
print(matrix.shape)

(4,)
(2, 3)
#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 = np.array([1, 2, 3, 4])
print(numbers)
print(type(numbers))
numbers.dtype

[1 2 3 4]
<class 'numpy.ndarray'>
dtype('int32')

# When NumPy can't convert a value to a numeric data type like float or integer, it uses a special nan value that stands for Not a Number
# NAN is the missing data
# 1.98600000e+03 is actually 1.986 * 10 ^ 3
world_alcohol

array([['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']], dtype='<U52')
world_alcohol = np.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
print(world_alcohol)

[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
 ['1986' 'Americas' 'Uruguay' 'Other' '0.5']
 ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
 ...
 ['1987' 'Africa' 'Malawi' 'Other' '0.75']
 ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
 ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
uruguay_other_1986 = world_alcohol[1,4] # index begins from zero
third_country = world_alcohol[2,2]
print(uruguay_other_1986)
print(third_country)

0.5
Cte d'Ivoire
vector = np.array([5, 10, 15, 20])
print(vector[0:3])  # slice 左闭右开的区间

[ 5 10 15]
matrix = np.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,1]) #[:,]表示所有的行 取其中的一列

[10 25 40]
matrix = np.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
# print(matrix[:,1]) [10 25 40]
print(matrix[:,0:2]) # [:,0:2]所有样本 取两列 第零列和第一列

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

[[20 25]
 [35 40]]
[[20 25 30]]
# 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 = np.array([5, 10, 15, 20])
vector == 10 # 对其中每个元素都进行操作

array([False,  True, False, False])
matrix = np.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
matrix == 25

array([[False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

#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 = np.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten]) # 把bool值当做一个index(索引)传到当前numpy.array之中
# vector[index]index即为bool类型的数组 value为false不用返回 为true将会返回

[False  True False False]
[10]
matrix = np.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, :]) # 先锁定含25的行 再取行中的各元素

[False  True False]
[[20 25 30]]
# We can also perform comparisons with multiple conditions
vector = np.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5) # And "&"的判断
print(equal_to_ten_and_five)

[False False False False]
vector = np.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5) # OR"|"的判断
print(equal_to_ten_or_five)

[ True  True False False]
vector = np.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 = np.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]]
# We can convert the data type of an array with the ndarray.astype() method.
vector = np.array(["1", "2", "3"])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print(vector.dtype)
print(vector)

|S1
['1' '2' '3']
float64
[ 1.  2.  3.]
vector = np.array([5, 10, 15, 20])
# vector.min()
# vector.sum()
# print(help(np.array))
x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
# x['a']
print(x.dtype)
x['b']

[('a', '<i4'), ('b', '<i4')]
array([2, 4])
# 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 = np.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=1) # 按行求和axix=1 或按列求和axis0 axix"维度"

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

array([60, 75, 90])
# replace nan value with 0
world_alcohol = np.genfromtxt("world_alcohol.txt", delimiter=",")
#print(world_alcohol)
is_value_empty = np.isnan(world_alcohol[:,4])
#print(is_value_empty)
world_alcohol[is_value_empty, 4] = '0'
alcohol_consumption = world_alcohol[:,4]
alcohol_consumption = alcohol_consumption.astype(float)
total_alcohol = alcohol_consumption.sum()
average_alcohol = alcohol_consumption.mean()
print(total_alcohol)
print(average_alcohol)

1137.78
1.14006012024

To Be Continued…

  • Numpy 基础操作
  • 数组矢量 运算
  • Numpy 进阶深究
  • 理解Transpose方法 和 Swapaxes方法
  • cumsum 累加和操作 和 cumprod 累积和操作
  • 理解切片索引操作
    以上推文只是对Numpy的基本操作,进行一番小试牛刀,算是给大家抛砖引玉啦,接下来,期待大家的精彩表现哟。 ~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值