这些问题都很naive,真的是我入门自己写语句时的困惑,如果你的水平不是入门,就不要浪费时间看了。
import numpy as np
容易犯错的地方
shape的问题
(3,)
表示一个一维行向量,该向量有3个元素。(3,1)
表示一个二维矩阵,形状是3*1.
x = np.array([1,2,3])
x
array([1, 2, 3])
x = x.reshape((3,1)) # 这不是列向量,这比列向量多了一对方括号!
x
array([[1],
[2],
[3]])
x = x.reshape((1,3)) # 比行向量多了一对方括号,这是个二维矩阵,不是向量!
x
array([[1, 2, 3]])
从上面的例子中可以看出,(3,)
和(3,1)
实际上是形状不同的,所以在进行矩阵乘法的时候一定要注意,要保证维数能对应。
行向量和列向量
上面讨论的形状的问题,接下来,讨论一下一维行向量和一维列向量。
x = np.array([1,2,3])
x.shape
(3,)
x = np.zeros(3)
x.shape
(3,)
无论是用np.array()
还是np.zero()
初始化,得到的向量都是行向量!
那么我们怎么得到一维的列向量呢?
首先,一个很自然的想法就是转置:
x = x.transpose()
x
array([0., 0., 0.])
x.shape
(3,)
可以看到转置后没有变化,说明转置没有办法把一维行向量转置为一维列向量。
另一种想法,就是对于一个二维矩阵我们来取它的某一列,这样会得到列向量吗?
x = np.eye(3)
x
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
x[0].shape # 取某一行发现得到的是一维行向量
(3,)
x[:,0].shape # 取某一列发现得到的还是一维行向量
(3,)
结果很绝望,怎么样都没有一维列向量。可以理解为对于一维向量,numpy统统视作行向量。
那么在计算中为了对应维数想要得到列向量怎么办?
只有将一维向量转为二维矩阵:如(3,)reshape为(1,3),对应的另一个向量/矩阵也要reshape为相应的维数。
常用操作
经常用到的基本操作就不记录在这里了。在这里记录一下我遇到的对我而言不太基本的函数。
np.unique()
help(np.unique)
Help on function unique in module numpy:
unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
Find the unique elements of an array.
Returns the sorted unique elements of an array. There are three optional
outputs in addition to the unique elements:
* the indices of the input array that give the unique values
* the indices of the unique array that reconstruct the input array
* the number of times each unique value comes up in the input array
Parameters
----------
ar : array_like
Input array. Unless `axis` is specified, this will be flattened if it
is not already 1-D.
return_index : bool, optional
If True, also return the indices of `ar` (along the specified axis,
if provided, or in the flattened array) that result in the unique array.
return_inverse : bool, optional
If True, also return the indices of the unique array (for the specified
axis, if provided) that can be used to reconstruct `ar`.
return_counts : bool, optional
If True, also return the number of times each unique item appears
in `ar`.
.. versionadded:: 1.9.0
axis : int or None, optional
The axis to operate on. If None, `ar` will be flattened. If an integer,
the subarrays indexed by the given axis will be flattened and treated
as the elements of a 1-D array with the dimension of the given axis,
see the notes for more details. Object arrays or structured arrays
that contain objects are not supported if the `axis` kwarg is used. The
default is None.
.. versionadded:: 1.13.0
Returns
-------
unique : ndarray
The sorted unique values.
unique_indices : ndarray, optional
The indices of the first occurrences of the unique values in the
original array. Only provided if `return_index` is True.
unique_inverse : ndarray, optional
The indices to reconstruct the original array from the
unique array. Only provided if `return_inverse` is True.
unique_counts : ndarray, optional
The number of times each of the unique values comes up in the
original array. Only provided if `return_counts` is True.
.. versionadded:: 1.9.0
See Also
--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.
repeat : Repeat elements of an array.
Notes
-----
When an axis is specified the subarrays indexed by the axis are sorted.
This is done by making the specified axis the first dimension of the array
(move the axis to the first dimension to keep the order of the other axes)
and then flattening the subarrays in C order. The flattened subarrays are
then viewed as a structured type with each element given a label, with the
effect that we end up with a 1-D array of structured types that can be
treated in the same way as any other 1-D array. The result is that the
flattened subarrays are sorted in lexicographic order starting with the
first element.
Examples
--------
>>> np.unique([1, 1, 2, 2, 3, 3])
array([1, 2, 3])
>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique(a)
array([1, 2, 3])
Return the unique rows of a 2D array
>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> np.unique(a, axis=0)
array([[1, 0, 0], [2, 3, 4]])
Return the indices of the original array that give the unique values:
>>> a = np.array(['a', 'b', 'b', 'c', 'a'])
>>> u, indices = np.unique(a, return_index=True)
>>> u
array(['a', 'b', 'c'], dtype='<U1')
>>> indices
array([0, 1, 3])
>>> a[indices]
array(['a', 'b', 'c'], dtype='<U1')
Reconstruct the input array from the unique values and inverse:
>>> a = np.array([1, 2, 6, 4, 2, 3, 2])
>>> u, indices = np.unique(a, return_inverse=True)
>>> u
array([1, 2, 3, 4, 6])
>>> indices
array([0, 1, 4, 3, 1, 2, 1])
>>> u[indices]
array([1, 2, 6, 4, 2, 3, 2])
Reconstruct the input values from the unique values and counts:
>>> a = np.array([1, 2, 6, 4, 2, 3, 2])
>>> values, counts = np.unique(a, return_counts=True)
>>> values
array([1, 2, 3, 4, 6])
>>> counts
array([1, 3, 1, 1, 1])
>>> np.repeat(values, counts)
array([1, 2, 2, 2, 3, 4, 6]) # original order not preserved
# negSampleWordIndices是列表,返回的unique_k和counts_k也是列表,分别对应不重复的负样本标号,和该标号对应的重复次数
unique_k, counts_k = np.unique(negSampleWordIndices, return_counts=True)
list和矩阵结合
取标号为x列表中的1,2,3行:
x = [1,2,3]
y = np.eye(5)
print(y[x])
[[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]]
取标号为x列表中的1,2,3列:
print(y[:,x])
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
初用numpy要注意:能用矩阵乘法代替的,尽量别用for
。
log
np.log
指的是以$e$为底的对数,np.log2
指的是以2为底的对数。就信息论本身而言,熵应该都是以2为底的(因为信息是0,1存储),但是深度学习里好像不讲究这么多,还是使用np.log
。
np.log1p
指的是$log(1+x)$。当$x$有可能取到0时使用这个函数。
文档信息
- 本文作者:weownthenight
- 本文链接:https://weownthenight.github.io/2021/06/23/numpy%E6%80%BB%E7%BB%93/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)