Numpy基本操作

import numpy as np
print(help(np.genfromtxt))
 
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
In [4]:
arr=np.array([[1,1,2,4],[4,2,3,3],[4,3,4,5]])
arr2=np.array([1,1,2,4,5,6])
In [5]:
print(arr)
print(arr2)
 
[[1 1 2 4]
 [4 2 3 3]
 [4 3 4 5]]
[1 1 2 4 5 6]
In [6]:
print(arr.shape)
print(arr2.shape)
 
(3, 4)
(6,)
In [7]:
# 数组的元素的类型一值
np.array([12,1,12,3.0])
Out[7]:
array([ 12.,   1.,  12.,   3.])
In [8]:
demo=np.array([12,1,12,3.0,8])
In [8]:
demo=np.array([12,1,12,3.0,8])
In [9]:
print(type(demo))
 
<class 'numpy.ndarray'>
In [10]:
#二维数组的二行三列的值是5
print(arr[2,3])
 
5
In [11]:
print(arr2[2:])
# 一维数组,隔列取值
print(arr2[0:5:2])
print(arr2[2:4])
# 取第二列
print(arr[:,1])
# 取第一二列
print(arr[:,0:2])
#取第一行
print(arr[1,])
# 取几行几列
print(arr[0:2,0:2])
 
[2 4 5 6]
[1 2 5]
[2 4]
[1 2 3]
[[1 1]
 [4 2]
 [4 3]]
[4 2 3 3]
[[1 1]
 [4 2]]
In [12]:
# 对numpy进行值验证
# 其返回的bool值也是数组的索引
index=arr==4
print(arr==4)
print(type(arr==4))
 
[[False False False  True]
 [ True False False False]
 [ True False  True False]]
<class 'numpy.ndarray'>
In [13]:
# 表示有四个值相等
print(arr[index])
 
[4 4 4 4]
In [14]:
# 根据哪列得值找出是哪行值相等
ind=arr[:,2]==4
print(arr[:,2]==4)
print(arr[ind, :])
 
[False False  True]
[[4 3 4 5]]
In [23]:
# |与$
# |会将两个结果的每一个bool值进行|计算,然后返回  true | false = true
print(arr)
print((arr == 2)|(arr == 4 ))
 
[[1 1 2 4]
 [4 2 3 3]
 [4 3 4 5]]
[[False False  True  True]
 [ True  True False False]
 [ True False  True False]]
In [24]:
# &会将两个结果的每一个bool值进行&计算,然后返回  true & false =false
print(arr)
print((arr == 2)&(arr == 4 ))
 
[[1 1 2 4]
 [4 2 3 3]
 [4 3 4 5]]
[[False False False False]
 [False False False False]
 [False False False False]]
In [31]:
arr3=np.array(['1','2','3','4'])
print(arr3.dtype)
print(arr3)
# arr3.astype(float)将数组的元素类型转换,返回一个转换后的值
arr3=arr3.astype(float)
print(arr3.dtype)
print(arr3)
 
<U1
['1' '2' '3' '4']
float64
[ 1.  2.  3.  4.]
In [30]:
# 最小值
print(arr.min())
# 最大值
print(arr.max())
 
1
5
In [33]:
# 求和
# 当axis=1时,按行求和
print(arr)
print(arr.sum(axis=1))
# 当axis=0时,按列求和
print(arr.sum(axis=0))
 
[[1 1 2 4]
 [4 2 3 3]
 [4 3 4 5]]
[ 8 12 16]
[ 9  6  9 12]
In [43]:
#np.arange(int)函数
arr4=np.arange(15)
print(arr4)
# arr4.reshape(3,5)将arr4 重构返回一个新值
arr5=arr4.reshape(3,5)
print(arr5)
print(arr5.shape)
# 维度
print(arr5.ndim)
# 数值个数
print(arr5.size)
 
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
(3, 5)
2
15
In [48]:
# 初始化一个新矩阵,需要被【】或者()包含
print(np.zeros([3,4]))
print(np.zeros((3,4)))
 
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
In [50]:
# 初始化1 ,并指定类型
print(np.ones((4,5),dtype=np.int32))
 
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
In [51]:
# 初始化一个序列,从1开始,15结束,间隔是4
print(np.arange(1,15,4))
 
[ 1  5  9 13]
In [54]:
# 随机矩阵值
# 随即一个两行三列的矩阵,范围在(-1,1)
print(np.random.random((2,3)))
 
[[ 0.64261278  0.20731143  0.52982881]
 [ 0.53455386  0.83654454  0.79983679]]
In [58]:
# 从0开始,2*pi结束,平均的获取40个值
from numpy import pi
print(np.linspace(0,2*pi,40))
# 重构矩阵
print(np.linspace(0,2*pi,40).reshape(5,8))
 
[ 0.          0.16110732  0.32221463  0.48332195  0.64442926  0.80553658
  0.96664389  1.12775121  1.28885852  1.44996584  1.61107316  1.77218047
  1.93328779  2.0943951   2.25550242  2.41660973  2.57771705  2.73882436
  2.89993168  3.061039    3.22214631  3.38325363  3.54436094  3.70546826
  3.86657557  4.02768289  4.1887902   4.34989752  4.51100484  4.67211215
  4.83321947  4.99432678  5.1554341   5.31654141  5.47764873  5.63875604
  5.79986336  5.96097068  6.12207799  6.28318531]
[[ 0.          0.16110732  0.32221463  0.48332195  0.64442926  0.80553658
   0.96664389  1.12775121]
 [ 1.28885852  1.44996584  1.61107316  1.77218047  1.93328779  2.0943951
   2.25550242  2.41660973]
 [ 2.57771705  2.73882436  2.89993168  3.061039    3.22214631  3.38325363
   3.54436094  3.70546826]
 [ 3.86657557  4.02768289  4.1887902   4.34989752  4.51100484  4.67211215
   4.83321947  4.99432678]
 [ 5.1554341   5.31654141  5.47764873  5.63875604  5.79986336  5.96097068
   6.12207799  6.28318531]]
In [60]:
# 矩阵的计算
a=np.array([4,8,12,16])
b=np.arange(4)

print(a)
print(b)
# 对应位置相减
c=a-b
print(c)
# 每一个元素都减 1
print(a-1)
# 每一个值平方
print(b**2)
# 比较
print(a<10)
 
[ 4  8 12 16]
[0 1 2 3]
[ 4  7 10 13]
[ 3  7 11 15]
[0 1 4 9]
[ True  True False False]
In [61]:
# 矩阵的乘法
a=np.array([[1,2],
           [3,4]])
b=np.array([[2,3],
           [3,4]])
print(a)
print(b)
# * 对应位置相乘
print(a*b)
 
[[1 2]
 [3 4]]
[[2 3]
 [3 4]]
[[ 2  6]
 [ 9 16]]
In [63]:
# 矩阵的. dot乘法   行与列相乘得到相应位置的值,例如:第一行的值乘以第一列得值得到对应位置的值
print(a)
print(b)
# 第一种
print(a. dot(b))
# 第二种
np.dot(a,b)
 
[[1 2]
 [3 4]]
[[2 3]
 [3 4]]
[[ 8 11]
 [18 25]]
Out[63]:
array([[ 8, 11],
       [18, 25]])
In [66]:
print(a)
print(np.sqrt(a))
 
[[1 2]
 [3 4]]
[[ 1.          1.41421356]
 [ 1.73205081  2.        ]]
In [68]:
# floor向下取整
a=np.floor(10*np.random.random([3,4]))
print(a)
 
[[ 9.  7.  0.  8.]
 [ 7.  0.  0.  8.]
 [ 3.  8.  1.  2.]]
In [69]:
# 将矩阵拉成向量
print(a.ravel())
 
[ 9.  7.  0.  8.  7.  0.  0.  8.  3.  8.  1.  2.]
In [75]:
# 重构矩阵
b=a.ravel()
b.shape=(4,3)
print(b)
print('============')
b=a.ravel()
print(b.reshape(4,3))
print('======')
# 转置
print(b.reshape(4,3).T)
 
[[ 9.  7.  0.]
 [ 8.  7.  0.]
 [ 0.  8.  3.]
 [ 8.  1.  2.]]
============
[[ 9.  7.  0.]
 [ 8.  7.  0.]
 [ 0.  8.  3.]
 [ 8.  1.  2.]]
======
[[ 9.  8.  0.  8.]
 [ 7.  7.  8.  1.]
 [ 0.  0.  3.  2.]]
In [84]:
q=np.floor(10*np.random.random((2,3)))
w=np.floor(10*np.random.random((3,2)))
print(q)
print(w)
 
[[ 0.  6.  8.]
 [ 7.  2.  9.]]
[[ 2.  2.]
 [ 0.  8.]
 [ 5.  2.]]
In [90]:
# 按行切分
a=np.floor(10*np.random.random((2,8)))
print(a)
print(np.hsplit(a,(2,5)))#表示水平从第2列的位置和5列的位置切
 
[[ 8.  8.  1.  7.  2.  2.  9.  4.]
 [ 3.  8.  1.  8.  8.  7.  6.  0.]]
[array([[ 8.,  8.],
       [ 3.,  8.]]), array([[ 1.,  7.,  2.],
       [ 1.,  8.,  8.]]), array([[ 2.,  9.,  4.],
       [ 7.,  6.,  0.]])]
In [91]:
# 按列切分
a=np.floor(10*np.random.random((6,3)))
print(a)
print(np.vsplit(a,(2,5)))#表示竖直从2行的位置和5行的位置切
 
[[ 6.  8.  9.]
 [ 4.  6.  3.]
 [ 7.  0.  2.]
 [ 7.  8.  4.]
 [ 6.  2.  2.]
 [ 0.  6.  7.]]
[array([[ 6.,  8.,  9.],
       [ 4.,  6.,  3.]]), array([[ 7.,  0.,  2.],
       [ 7.,  8.,  4.],
       [ 6.,  2.,  2.]]), array([[ 0.,  6.,  7.]])]
In [97]:
data=np.sin(np.arange(20).reshape(4,5))
print(data)
# 求最大值,axis=1表示行最大,axis=0表示列最大
ind=data.argmax(axis=0)
print(data.argmax(axis=0))
print(data.shape)
print(data[ind,range(data.shape[1])])
 
[[ 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]]
[3 0 0 1 2]
(4, 5)
[ 0.65028784  0.84147098  0.90929743  0.98935825  0.99060736]
In [98]:
# 矩阵的扩展
a=np.arange(0,40,10)
print(a)
b=np.tile(a,(4,3))
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]]
In [113]:
a=np.array([[4,6,5],[3,5,4]])
print(a)
b=np.sort(a,axis=1)
print(b)
print(np.sort(a,axis=0))
print('====')
print(np.argsort(a))
index=np.argsort(a)
print(np.sort(a,axis=0))
print('====')
print(np.argsort(a))
index=np.argsort(a)
 
[[4 6 5]
 [3 5 4]]
[[4 5 6]
 [3 4 5]]
[[3 5 4]
 [4 6 5]]
====
[[0 2 1]
 [0 2 1]]
[[3 5 4]
 [4 6 5]]
====
[[0 2 1]
 [0 2 1]]

posted on 2018-06-21 07:05  天下无槛,不服就干  阅读(163)  评论(0)    收藏  举报

导航