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

NumPy初步使用 #44

Open
huruji opened this issue Jun 6, 2018 · 0 comments
Open

NumPy初步使用 #44

huruji opened this issue Jun 6, 2018 · 0 comments

Comments

@huruji
Copy link
Owner

huruji commented Jun 6, 2018

创建ndarray对象:

import numpy as np

np.array([1,2,3,4])
np.array([[1,2,3,4],[5,6,7,8]])

转换为list

np.array([1,2,3,4]).tolist()

获取ndarray对象的基本信息:维数(ndim)、行列信息(shape)、数据存储类型(dtype)

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

print(arr.ndim)

print(arr.shape)

print(arr.dtype)

设置数据存储类型

np.array([1,2,3,4], dtype=np.int32)

np.arrat([1.2,1.3,1.4], dtype=np.float64)

创建特殊ndarray对象:全0(zeros)、全1(ones)、随机值(empty),参数是形状

np.zeros(8)

np.ones((2,3))

np.empty((3,4))

指定范围创建ndarray对象(arange)

arr1 = np.arange(1,8,2)

# [1 3 5 7]

创建网格数据(linspace)

np.linspace(0, 80, 5)

# [0 20 40 60 80]

修改形状(reshape)

np.arange(0,12).reshape((3,4))

# [[ 0,  1,  2,  3],
#  [ 4,  5,  6,  7],
#  [ 8,  9, 10, 11]]

展平,转化为一维数组(flatten)

a = np.arange(12).shape(3,4)

a.flatten()

矩阵转置(transpose)

a = np.arange(12).reshape(3,4)

a.transpose()

# 等同于

a.T

数学运算(+ - * / )、点乘(矩阵乘法)、三角函数

a = np.arange(12).reshape(3,4)

b = np.arange(12).reshape(4,3)

a + 1

a + b 

a - 1

a - b

a * 2

a * b

a / 2

a / b

# 平方
arr ** 2

# 点乘
np.dot(a, b)
a.dot(b)


np.sin(a)

深浅复制,赋值操作为浅复制,使用clone方法深复制:

a = np.arange(12)

b = a

c = a.clone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant