Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Analysis with Python——07 #87

Open
hsipeng opened this issue Aug 23, 2019 · 0 comments
Open

Data Analysis with Python——07 #87

hsipeng opened this issue Aug 23, 2019 · 0 comments

Comments

@hsipeng
Copy link
Owner

hsipeng commented Aug 23, 2019

Data Analysis with Python——07

numpy 高级应用

ndarray 内部组成

  • 一个指向数组(一个系统内存块)的指针
  • 数据类型或dtype
  • 一个表示数组形状(shape)的元组
  • 一个跨度元组(stride)。 其中的整数指的是为了前进到当前维度下一个元素需要“跨过”的字节数

Numpy 数据类型体系

dtype 都有一个超类(比如np.integer, np.floating), 他们可以跟np.issubdtype 函数结合使用

ints = np.ones(10, dtype=np.uint16)
floats = np.ones(10, dtype=np.float32)

np.issubdtype(ints.dtype, np.integer)
# True

np.issubdtype(floats.dtype, np.floating)

dtype.mro 可以查看其所有的父类

mr9pnO.png

数组重塑

reshape

arr = np.arange(8)
arr
# array([0, 1, 2, 3, 4, 5, 6, 7])

arr.reshape((4, 2))
# array([[0, 1],
#			[2, 3],
#			[4, 5],
#			[6, 7]])

mr99BD.png

扁平化

ravel
flatten 产生副本

arr.ravel()
# array([0, 1, 2, 3, 4, 5, 6, 7]

arr.flatten
# array([0, 1, 2, 3, 4, 5, 6, 7]

C 和 Fortran 顺序
Fortran 矩阵列优先
C 行优先

数组的合并和拆分

合并

  • numpy.concatenate
  • np.vstack np.hstack
arr1 = np.array([[1, 2, 3], [4, 5, 6]])

arr2 = np.array([[7, 8, 9], [10, 11, 12]])

np.concatenate([arr1, arr2], axis=0)
# OR np.vstack((arr1, arr2))
# array([[ 1,  2,  3],
#        [ 4,  5,  6],
#        [ 7,  8,  9],
#        [10, 11, 12]])
np.concatenate([arr1, arr2], axis=1)
# OR np.hstack((arr1, arr2))
# array([[ 1,  2,  3,  7,  8,  9],
#        [ 4,  5,  6, 10, 11, 12]])

拆分
np.split

mr9V3t.png

广播

Screen Shot 2019-03-12 at 2.41.40 PM.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant