Numpy学习笔记


[TOC]

Numpy方法

np.ceil(x, y) 限制元素范围

  • x 输入的数据
  • y float型,每个元素的上限
housing["income_cat"] = np.ceil(housing["median_income"] / 1.5)     # 每个元素都除1.5

permutation(x) 随机生成一个排列或返回一个range

如果x是一个多维数组,则只会沿着它的第一个索引进行混洗。

import numpy as np

shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]

numpy.argmax() 返回沿轴的最大值的索引

返回沿轴的最大值的索引。

# some_digit_scores 内容
# array([[-311402.62954431, -363517.28355739, -446449.5306454 ,
#         -183226.61023518, -414337.15339485,  161855.74572176,
#         -452576.39616343, -471957.14962573, -518542.33997148,
#         -536774.63961222]])
np.argmax(some_digit_scores)
# Out
# 5
  • a : array_like; 输入数组
  • axis : int, optional; 默认情况下,索引是放在平面数组中,否则沿着指定的轴。
  • out : array, optional; 如果提供,结果将被插入到这个数组中。它应该是适当的形状和dtype。

np.linalg.inv() 计算矩阵的逆

X_b = np.c_[np.ones((100, 1)), X]  # add x0 = 1 to each instance
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
  • a : (…, M, M) array_like;被求逆的矩阵

numpy.dot(a, b, out=None) 计算两个数组的点积

>>> np.dot(3, 4)
12

# Neither argument is complex-conjugated:
>>> np.dot([2j, 3j], [2j, 3j])
(-13+0j)

# For 2-D arrays it is the matrix product:
>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1],
       [2, 2]])

>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
>>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
>>> np.dot(a, b)[2,3,2,1,2,2]
499128
>>> sum(a[2,3,2,:] * b[1,2,:,2])
499128
  • a : array_like;First argument.
  • b : array_like;Second argument.

numpy.ndarray.T() 计算矩阵的转置

self.transpose()相同,如果self.ndim < 2则返回它自身。

>>> x = np.array([[1.,2.],[3.,4.]])
>>> x
array([[ 1.,  2.],
       [ 3.,  4.]])
>>> x.T
array([[ 1.,  3.],
       [ 2.,  4.]])
>>> x = np.array([1.,2.,3.,4.])
>>> x
array([ 1.,  2.,  3.,  4.])
>>> x.T
array([ 1.,  2.,  3.,  4.])

numpy.random.seed() 生成器种子

该方法由RandomState初始化,它可以被重新设置。

np.random.seed(42)
theta = np.random.randn(2,1)  # random initialization
  • seed : int or array_like, optional;必须为32位无符号整数。

numpy.random.randn() 从标准正太分布返回样本

>>> theta = np.random.randn(2,1)
array([[ 4.21509616],
       [ 2.77011339]])
参数
  • d0, d1, …, dn : int, optional;返回的数组维度,应该都是正值。如果没有给出,将返回一个Python float值。
返回值
  • Z : ndarray or float;一个经过标准正态分布抽样的,(d0, d1, ..., dn)维度的浮点数组。

numpy.array() 创建一个数组

theta_path_bgd = np.array(theta_path_bgd)
theta_path_sgd = np.array(theta_path_sgd)
theta_path_mgd = np.array(theta_path_mgd)
  • object : array_like
  • dtype : data-type, optional

numpy.random.rand()&nmbsp;生成给定shap的随机值

m = 100
X = 6 * np.random.rand(m, 1) - 3
y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)
>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random
  • d0, d1, …, dn : int, optional;返回的数组维度,必须是正值。如果为空,则返回一个Python float值。

numpy.linspace() 在指定区间返回间隔均匀的样本[start, stop]

X_new=np.linspace(-3, 3, 100).reshape(100, 1)
X_new_poly = poly_features.transform(X_new)
y_new = lin_reg.predict(X_new_poly)
plt.plot(X, y, "b.")
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.legend(loc="upper left", fontsize=14)
plt.axis([-3, 3, 0, 10])
save_fig("quadratic_predictions_plot")
plt.show()
  • start : scalar;序列的起始值
  • stop : scalar;序列的结束值
  • num : int, optional;要生成的样本数量,默认为50个。
  • endpoint : bool, optional;若为True则包括结束值,否则不包括结束值,即[start, stop)区间。默认为True。
  • dtype : dtype, optional;输出数组的类型,若未给出则从输入数据推断类型。

meshgrid() 从坐标向量返回坐标矩阵

>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = np.meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]])
>>> xv, yv = np.meshgrid(x, y, sparse=True)  # make sparse output arrays
>>> xv
array([[ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.],
       [ 1.]])
  • x1, x2,…, xn : array_like;代表网格坐标的一维数组。
  • indexing : {‘xy’, ‘ij’}, optional;输出的笛卡儿(’xy’,默认)或矩阵(’ij’)索引。
  • sparse : bool, optional;如果为True则返回稀疏矩阵以减少内存,默认为False。

norm() 矩阵或向量范数

t1a, t1b, t2a, t2b = -1, 3, -1.5, 1.5

# ignoring bias term
t1s = np.linspace(t1a, t1b, 500)
t2s = np.linspace(t2a, t2b, 500)
t1, t2 = np.meshgrid(t1s, t2s)
T = np.c_[t1.ravel(), t2.ravel()]
Xr = np.array([[-1, 1], [-0.3, -1], [1, 0.1]])
yr = 2 * Xr[:, :1] + 0.5 * Xr[:, 1:]

J = (1/len(Xr) * np.sum((T.dot(Xr.T) - yr.T)**2, axis=1)).reshape(t1.shape)

N1 = np.linalg.norm(T, ord=1, axis=1).reshape(t1.shape)
N2 = np.linalg.norm(T, ord=2, axis=1).reshape(t1.shape)

t_min_idx = np.unravel_index(np.argmin(J), J.shape)
t1_min, t2_min = t1[t_min_idx], t2[t_min_idx]

t_init = np.array([[0.25], [-1]])
  • x : array_like;输入的数组,如果axis是None,则x必须是1-D或2-D。
  • ord : {non-zero int, inf, -inf, ‘fro’, ‘nuc’}, optional;范数的顺序,inf表示numpy的inf对象。
  • axis : {int, 2-tuple of ints, None}, optional
  • keepdims : bool, optional

以下范数可以被计算:

ord norm for matrices norm for vectors
None Frobenius norm 2-norm
‘fro’ Frobenius norm
‘nuc’ nuclear norm
inf max(sum(abs(x), axis=1)) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
0 sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
-2 smallest singular value as below
other sum(abs(x)ord)(1./ord)

对于ord <= 0的值,它严格来说不是数学规范的范数,但它作为数值目的任然有用。

unravel_index() 将平面索引或平面索引数组转换为坐标数组的元组

>>> np.unravel_index([22, 41, 37], (7,6))
(array([3, 6, 6]), array([4, 5, 1]))
>>> np.unravel_index([31, 41, 13], (7,6), order='F')
(array([3, 6, 6]), array([4, 5, 1]))

>>> np.unravel_index(1621, (6,7,8,9))
(3, 1, 4, 1)
  • indices : array_like;一个整数数组,其元素是索引到维数组dims的平坦版本中。
  • dims : tuple of ints;用于分解索引的数组的形状。
  • order : {‘C’, ‘F’}, optional;决定indices应该按row-major (C-style) or column-major (Fortran-style) 顺序。

mean() 计算沿指定轴的算术平均值

>>> a = np.array([[1, 2], [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0)
array([ 2.,  3.])
>>> np.mean(a, axis=1)
array([ 1.5,  3.5])


>>> a = np.zeros((2, 512*512), dtype=np.float32)
>>> a[0, :] = 1.0
>>> a[1, :] = 0.1
>>> np.mean(a)
0.54999924
  • a : array_like;包含要求平均值的数组,如果不是数组,则尝试进行转换。
  • axis : None or int or tuple of ints, optional;计算平均值的轴,默认计算扁平数组。
  • dtype : data-type, optional;用于计算平均值的类型。
  • out : ndarray, optional

文章作者: Mercy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Mercy !
评论
  目录