「Python」:Module & Method

长期记录帖:关于遇到过的那些Python 的Packets & Module & Method & Attribute。中英记录。

Tricky

list comprehension

  • List comprehension provides a concise way to create lists.

    • e.g. : squares = [x**2 for x in range(10)]
  • A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

    • e.g. : Double loop: [(x,y) for x in [1,2,3] for y in [3,1,4] if x != y]
      • 输出7个
    • e.g. : (Using zip() to loop together): [(x, y) for x,y in zip([1,2,3], [3,1,4]) if x!=y]
      • 输出2个

Python-Build function

print

  • print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout)

len

  • Return the length(the number of items) of an object.

str.format()

  • 字符串格式化
  • eg:
    • “{} {}”.format(“Hello”,”World)
    • ‘Hello World’

zip(*iterables)

  • Make an iterator that aggregates【聚集】 elements from each of the iterales.

  • Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted.

    【返回一个元组的迭代器】

  • 使用zip可以同时对多个迭代器进行迭代

enumerate

  • Enumerate is a built-in funciton of Python. It allows us to loop over something and have an automatic counter.

  • e.g.

    • for counter, value in enumerate(some_list):

      print(counter, value)

  • e.g. : an optional argument: tell enumerate from where to start the index.

    • for c, value in enumerate(my_list, 1):

      print(c, value)

with open(path) as f:

  • 由于读写文件都可能产生IOError,一旦出错,后面的f.close()就不会调用。

  • 用try……finally来实现,比较麻烦。

    try:

    ​ f = open(path, ‘r’)

    ​ print(f.read())

    finally:

    ​ if f:

    ​ f.close()

  • 用with as 简化

    with open(path, ‘r’) as f:

    ​ print(f.read())

numpy

numpy.argsort

  • numpy.argsort(a, axis=-1, kind=None, order=None)

  • Returns the indices that would sort an array.

    Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.

  • Parameters:

    • a :array_like.

    • axis : int or None, optional

      Axis along which to sort. The default is -1(the last axis). 【默认按照最后一个维度】

      • 2-D: axis = 0按列排序
      • 2-D: axis = 1 按行排序
    • kind :{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional

      The default is ‘quicksort’

  • Return: index_array: ndarray, int.【返回的是降序排列的索引数组】

    • e.g.:

      x = np.array([5, 1, 2])

      np.argsort(x) # 降序

      array([1,2,0])

      np.argsort(-x) # 升序

      array([0,2,1])

Linear algebra(numpy.linalg)

numpy.dot

  • numpy.dot(a,b)
  • Dot product of two arrays.
    • If both a and b are 1-D arrays, it is inner porduct of vectors.
    • If both a and b are 2-D arrays, it is matrix multiplication, but using matmul is preferred.
    • Id either a or b is 0-D(scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a*b is preferred.
    • ……

numpy.matmul

  • Matrix product of two arrays.
  • numpy.matmul(x1, x2)

numpy.linalg.inv(a)

  • Compute the inverse of a matrix.

    Given a square matrix a, return the matrix ainv satisfying dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])/

  • numpy.linalg.inv(a)

  • Parameters:

    • a :(…, M, M) array_like. Matrix to e inverted.
  • Return: ainv.

numpy.linalg.svd

  • numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)

  • Singular Value Decomposition

  • 矩阵的奇异值分解

    A = u @ s @ vh

    u, vh是标准正交矩阵, inv(u) = uh

    s是对角矩阵

  • Parameters:

    • a :array_like, a real or complex array with a.ndim >=2

    • full_matrices :bool, optional. True(default)

      If True, u and vh have the shapes(…, M, M) and (…, N, N), respectively.

      Otherwise, the shapes are(…, M, K) and (…, K, N), respectively, where K = min(M,N)

    • compute_uv : bool, optional.True(default)

      Whether or not to compute u and vh in addition to s.【注,vh就是v的转置】

  • Return:

    • u: array
    • s: array
    • vh:array

numpy.zeros

  • numpy.zeros(shape, dtype=float, order=’C’)

  • Return a new array of given shape and type, filled with zeros.

  • parameters:

    • shape: int or truple of ints. e.g.,(2,3) or 2
    • dtype: data-type, optional.(Defaults is numpy.float64)
    • oder: optional
  • Returns: out ndarray

numpy.full

  • numpy.full(shape, fill_value, dtype=None)
  • Return a new array of given shape and type, filled with fill_value.
  • parameters:
    • shape :int or sequence of ints
      • (2,3) or 2
    • fill_value :scalar
    • dtype :data-type, optional

numpy.arange

  • numpy.arange([start, ]stop, [step, ]dtype=None)

  • Return evenly spaced values within a given interval.

  • parameters:

    • start: number, optional. (Defaults is 0)
    • stop :number. [start,stop)
    • step :number, optional(Defaults is 1)
    • dtype :
  • Returns :ndarray

  • differ with built-in range function

    • numpy.arange returnan ndarray rathan than a list.
    • numpy.arange’s step can be float.

numpy.meshgrid

  • numpy.meshgrid(x, y)
  • 生成用x向量为行,y向量为列的矩阵(坐标系)
  • 返回 X矩阵和Y矩阵
    • X矩阵:网格上所有点的x值
    • Y矩阵:网格上所有点的y值
  • e.g., X, Y = np.meshgrid(x, y) 【X,Y 都是网格点坐标矩阵】

numpy.genfromtext

  • numpy.genfromtxt (fname, delimiter=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 characters are discarded.

  • Parameters:

    • fname :file, str, list of str, generator.
    • dtype :dtype, optional.
    • delimiter :str, int, or sequence, optional.
      • (default = whitespace)
      • The strin used to separate values.
  • Python的列表读取处理数据很慢,numpy.genfromtext就很棒。

numpy.isnan

  • numpy.isnan(x)
  • Test element-wise for NaN(Not a number) and return result as a boolean array.
  • Parameters:
    • x :array_like
  • Returns: y:ndarray or bool.
    • True where x is NaN, false otherwise.

numpy.empty

  • nmpy.empty(shape, dtype=float, order=’C’)
  • Return a new arry of given shape and type, without initializing entries.
  • Parameters:
    • shape :int or tuple of int
    • dtype :data-type,optional
      • Default is numpy.float64.
  • Returns:
    • out: ndarray

numpy.reshape

  • numpy.reshape(a, newshape, order=’C’)
  • Gives a new shape to an array without changing its data.【改变张量的shape,不改变张量的数据】
  • Parameters:
    • a : array-like
    • newshape : int or tuple of ints
      • One shape dimension can be -1. The value is inferred from the length of the array and remaning dimensions.
  • Returns: reshaped_array:ndarray

numpy.mean

  • numpy.mean(a, axis=None)
  • Compute the arithmetic mean along the specifiied axis.(the average of the array elements)
  • Parameters:
    • a : array_like
    • axis :None or int or tuple of ints, optional
      • Axis or axes along which the means are computed.
      • axis=0 :沿行的垂直往下(列)
      • axis=1 :沿列的方向水平向右(行)

numpy.std

  • numpy.std(a, axis=None,)
  • Compute the standard deviation along the specified axis.【标准差】
  • Parameters:
    • a :array_like
    • axis :Axis or axes along which the means are computed.

numpy.shape

  • attribute
  • Tuple of array dimensions.

numpy.concatenate

  • numpy.concatenate((a1, a2, …), axis=0)
  • Join a sequence of arrays along an existing axis.
  • Parameters:
    • a1, a2, … :sequence of array_like
      • The arrays must have the same shape, excepting in the dimension corresponding to axis.【除了axia方向,其他维度的shape要相同】
      • If axis is None, arrays are flattened before use.【值为None,就先将向量变成一维的】
      • Default=0

numpy.ndarray.astype

  • method
  • numpy.ndarray.astype(dtype)
  • Copy of the array cast to a specified type.【强制转换数据类型】
  • Parameters:
    • dtype : str or dtype

numpy.ones

  • numpy.ones(shape, dtype=None)
  • Return a new array of given shape and type, filled with ones.
  • Parameters:
    • shape : int or sequence of ints.
    • dtype : data-type, optional

numpy.array

  • numpy.array(object, dtype = none)

  • Create an array

  • Parameters:

    • object :array_like

      An array, any object exposing the array interface, an object whose array method returns an array, or any(nested) sequence.

numpy ndarray 运算

  • [[1]]*3 = [[1],[1],[1]]
  • A * B 元素相乘
  • numpy.dot(A, B) 矩阵相乘

numpy.power

  • numpy.power(x1, x2)
  • First array elements raised to powers from second array.
  • Parameters:
    • x1 :array_like .
      • The bases.
    • x2 :array_like
      • The exponents.

numpy.sum

  • numpy.sum(a, axis=None, dtype=None)
  • Sum of arrays elements over a given axis.
  • Parameters:
    • a :array_like
      • Elements to sum.
    • axis :None or int or tuple of ints, optional
      • Axis or axes along which a sum is perfomed.
      • The default, None, will sum all of the elementsof the input array.

numpy.transpose

  • numpy.transpose(a, axes=None)

  • Permute the dimensions of the array.【tensor的维度换位】

  • Parameters:

    • a : array_like
    • axes : list of ints, optinal
      • Default, reverse the dimensions.
      • Otherwise permute the axes according to the values given.
  • Returns : ndarray

  • 张量a的shape是(10,2,15), numpy.transport(2,0,1)的shape就是(15,10,2)

  • 对于一维:行向量变成列向量

  • 对于二维:矩阵的转置

numpy.save

  • numpy.save(file, arr)

  • Save an array to a binary file in Numpy .npy format.

  • Parameters:

    • file :file, str, or pathlib

    • arr :array_like

      Array data to be saved.

numpy.clip

  • Clip(limit) the values in an array
  • numpy.clip(a, a_min, a_max)
  • Parameters:
    • a :array_like
    • a_min : scalar or array_like
    • a_max :scalar or array_like

numpy.around

  • Evenly round to the given number of decimals(十进制)

  • numpy.around(a)

  • Parameters:

    • a :array_like
  • Notes: For values exactly halfway between rounded decimal values, Numpy rounds to the nearest even values.

    【这什么意思呢? 就是说对于0.5的这种,为了统计上平衡,不会全部向上取整或者向下取整,会向最近的偶数取整,around(2.5)=2】

numpy.log

  • The natural logarithm log is the inverse of exponential functions, so that log(exp(x))=x.
  • numpy.log(x)
  • Parameters:
    • x : array_like

numpy.ndarray.T

  • attribute, the transpose array.
  • ndarray.T

numpy.random.shuffle

  • Modify a sequence in-space by shufflng its contents.

    This function only shuffles the array along the first axis of a multi-diensional array. The order of sub-arrays is changed but their contents remains the same.

  • numpy.random.shuffle(x)

  • Parameters:

    • x : array_like
  • e.g. shuffle two list, X and Y, together. 【以相同的顺序打乱两个array】

    • np.random.seed(0)

      randomize = np.arrange(len(X))

      np.random.shuffle(randomize)

      return X[randomize], Y[randomize]

sklearn

skelearn.linear_model.LinearRegression

  • class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=None)
  • Ordinary least squares Linear Regress(最小二乘法回归运算)
  • Parameters:
    • fit_intercept :bool, optional, defalut True【True:需要bias的截距项】
    • normalize :bool, optional, default False 【True:对样本做feature scaling】
  • Attributes:
    • coef_ :array of shape(n_features) 【权重】
    • intercept_ :bias
  • Methods:
    • fit(self,X,y[,sample_weight]) :Fit linear model
      • e.g. , LinearRegression().fit(x_data, y_data)

matplotlib.pyplot

matplotlib.pyplot.contourf

  • contour and contourf draw contour lines and filled contours, respectively.【一个画等高线,一个填充等高线/轮廓】

  • contour([X, Y, ] Z, [levels], **kwargs)

  • Parameters

    • X, Y: The coordinates of the values in Z. A and Y must both be 2-D with the sanme shape as Z(e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == M is the number of columns in Z and len(Y) = N is the number of rows in Z.

      【X,Y要么是由像numpy.mershgrid(x, y) 生成的网格点坐标矩阵,要么X,Y是(基)向量,X向量是x轴的,对应到Z矩阵,是Z矩阵的列数,Y向量同理】

    • Z :array-like(N,M)

    • levels : int or array-like, optional. Determines the number and positions of contour lines / religions.【划分多少块等高区域】

    • alpha :float, optional. Between 0(transparent) and 1(opaque).【透明度】

    • cmap :str or Colormap, optional.

  • e.g., pyplot.contourf(x, y, Z, 50, alpha=0.5, cmap=pyplot.get_cmap(‘jet’))【‘jet’是常用的那种红橙黄绿青蓝紫】

matplotlib.pyplot.plot

  • plot([x], y, [fmt], , data=None, *kwargs)
  • The coordinates of the points or line nodes are given by x, y.
  • Parameters:
    • x, y :array-like or scalar.
    • fmt :str, optional. A format string.
      • e.g., ‘.’, point marker. ‘-‘, solid line style. ‘–’,dashed line style. ‘b’, blue.
    • ms/markersize : float
    • lw/linewidth :float
    • color :

matplotlib.pyplot.xlim

  • xlim(args, *kwargs)
  • Get or set the x limits of the current axes.
  • e.g.
    • left, right = xlim() :get
    • xlim(left, right) :set

matplotlib.pyplot.show

  • show(args, *kwargs)
  • display a figure.

matplotlib.pyplot.vlines

  • Plot vertical lines.
  • vlines(x, ymin, ymax, color=’k’, linestyles=’solid’)
  • Parameters:
    • x :scalar or 1D array_like
    • ymin, ymax :scalar or 1D array_like

matplotlib.pyplot.hlines

  • Plot horizontal lines.
  • vlines(y, xmin, xmax, color=’k’, linestyles=’solid’)
  • Parameters:
    • y :scalar or 1D array_like
    • xmin, xmax :scalar or 1D array_like

matplotlib.pyplot.savefig

  • Save the current figure.
  • savefig(fname)
  • Parameters:
    • fname :str ot Pathlike

matplotlib.pyplot.legend

  • Place a lengend on the axes.
  • e.g. : legend() Labeling exisiting plot elements
    • plt.plot(train_loss)
    • plt.plot(dev_loss)
    • plt.legend([‘train’, ‘dev’])
  • e.g. : le

sys

sys.argv[]

  • python a.py data.csv
    • sys.argv = [‘a.py’, ‘data.csv’]

重定向到文件

  • f = open(‘out.csv’, ‘w’)

    sys.stdout = f

    print(‘此时print掉用的就是文件对象的write方法’)

「Python」:Module & Method

https://f7ed.com/2020/03/07/python/

Author

f7ed

Posted on

2020-03-07

Updated on

2020-07-03

Licensed under

CC BY-NC-SA 4.0


Comments