码迷,mamicode.com
首页 > 其他好文 > 详细

numpy基础篇-简单入门教程2

时间:2019-02-24 20:14:21      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:+=   print   入门   基本   ide   import   rac   float   1.0   

import numpy as np
  • Array 数组
print(np.zeros((2, 2)))  # [[0. 0.] [0. 0.]]
print(np.ones((2, 2)))  # [[1. 1.] [1. 1.]]
print(np.full((2, 2), 7))  # [[7 7] [7 7]]
print(np.eye(2))  # [[1. 0.] [0. 1.]]

print(np.random.random((2, 2)))  # [[0.67151478 0.61234823] [0.85594251 0.0654221 ]]
a = np.array(np.arange(9).reshape(3, 3))
print(a)   # [[0 1 2] [3 4 5] [6 7 8]]

print(a[0, 0])  # 0
print(a[0][0])  # 0
print(type(a[0, 0]))  # <class 'numpy.int64'>
print(type(a[0][0]))  # <class 'numpy.int64'>
print(a[0, 0].shape)  # ()
print(a[0][0].shape)  # ()

print(a[1, :])  # [3 4 5]
print(a[1][:])  # [3 4 5]
print(type(a[1, :]))  # <class 'numpy.ndarray'>
print(type(a[1][:]))  # <class 'numpy.ndarray'>
print(a[1, :].shape)  # (3,)
print(a[1][:].shape)  # (3,)
a = np.array(np.arange(9).reshape(3, 3))
print(a)  # [[0 1 2] [3 4 5]  [6 7 8]]

print(a[1, :])  # [3 4 5]
print(a[1:2, :])  # [[3 4 5]]
print(a[1, :].shape)  # (3,)
print(a[1:2, :].shape)  # (1, 3)

print(a[:, 1])  # [1 4 7]
print(a[:, 1:2])  # [[1] [4] [7]]
print(a[:, 1].shape)  # (3,)
print(a[:, 1:2].shape)  # (3, 1)
a = np.array(np.arange(1, 7, 1).reshape(3, 2))
print(a)  # [[1 2] [3 4] [5 6]]
print(a[[0, 1, 2], [0, 1, 0]])  # [1 4 5]
print(np.array([a[0, 0], a[1, 1], a[2, 0]]))  # [1 4 5]

a = np.array(np.arange(1, 13, 1).reshape(3, 4))
print(a)  # [[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12]]
b = np.array([0, 2])
print(a[np.arange(1, 3, 1), b])  # [ 5 11]
a[np.arange(1, 3, 1), b] += 10
print(a)  # [[ 1  2  3  4] [15  6  7  8] [ 9 10 21 12]]
  • bool 布尔
a = np.array(np.arange(1, 7, 1).reshape(3, 2))

print(a)  # [[1 2] [3 4] [5 6]]

bool_idx = (a > 2)

print(bool_idx)  # [[False False] [ True  True] [ True  True]]
print(a[bool_idx])  # [3 4 5 6]
print(a[a > 2])  # [3 4 5 6]
  • type 类型
x = np.array([1, 2])
print(x.dtype)  # int64

x = np.array([1.0, 2.0])
print(x.dtype)  # float64

x = np.array([1, 2], dtype=np.int8)
print(x.dtype)  # int8
  • 基本运算
x = np.array(np.arange(1, 5, 1).reshape(2, 2), dtype=np.float64)
y = np.array(np.arange(5, 9, 1).reshape(2, 2), dtype=np.float64)

print(x)  # [[1. 2.] [3. 4.]]
print(y)  # [[5. 6.] [7. 8.]]

print(x + y)
print(np.add(x, y))  # [[ 6.  8.] [10. 12.]]

print(x - y)
print(np.subtract(x, y))  # [[-4. -4.]  [-4. -4.]]

print(x * y)
print(np.multiply(x, y))  # [[ 5. 12.]  [21. 32.]]

print(x / y)
print(np.divide(x, y))  # [[0.2        0.33333333] [0.42857143 0.5       ]]

print(np.sqrt(x))  # [[1.         1.41421356] [1.73205081 2.        ]]
  • 矩阵乘法
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])

v = np.array([9, 10])
w = np.array([11, 12])

print(v.dot(w))
print(np.dot(v, w))  # 219

print(x.dot(v))
print(np.dot(x, v))  # [29 67]

print(x.dot(y))
print(np.dot(x, y))  # [[19 22]  [43 50]]
  • sum 求和
x = np.array([[1, 2], [3, 4]])

print(x)  # [[1 2] [3 4]]
print(np.sum(x))  # 10
print(np.sum(x, axis=0))  # [4 6]  Compute sum of each column
print(np.sum(x, axis=1))  # [3 7]  Compute sum of each row
  • .T 转置
x = np.array([[1, 2], [3, 4]])
v = np.array([1, 2, 3])

print(x.T)  # [[1 3] [2 4]]    2D array
print(v.T)  # [1 2 3]     1D array
  • broadcasting 广播
x = np.array(np.arange(1, 13, 1).reshape(4, 3))  # [[ 1  2  3] [ 4  5  6] [ 7  8  9] [10 11 12]]
v = np.array([1, 0, 1])
y = np.empty_like(x)

print(y)  # [[ 1  2  3] [ 4  5  6]  [ 7  8  9] [10 11 12]]

for i in range(4):
    y[i, :] = x[i, :] + v

print(y)  # [[ 2  2  4] [ 5  5  7] [ 8  8 10] [11 11 13]]

x = np.array(np.arange(1, 13, 1).reshape(4, 3))
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))  # [[1 0 1]  [1 0 1] [1 0 1] [1 0 1]]
y = x + vv

print(y)  # [[ 2  2  4] [ 5  5  7] [ 8  8 10] [11 11 13]]

y = x + v  # broadcaste

print(y)  # [[ 2  2  4] [ 5  5  7] [ 8  8 10] [11 11 13]]

v = np.array([1, 2, 3])
w = np.array([4, 5])

print(np.reshape(v, (3, 1)) * [0, 1, 2])  # [[0 1 2] [0 2 4] [0 3 6]]  w has 3 numbers,so has 3 rows
x = np.array([[1, 2, 3], [4, 5, 6]])

print(x + v)  # [[2 4 6] [5 7 9]]
print((x.T + w).T)  # [[ 5  6  7] [ 9 10 11]]
print(x + np.reshape(w, (2, 1)))  # [[ 5  6  7] [ 9 10 11]]
print(x * 2)  # [[ 2  4  6] [ 8 10 12]]

numpy基础篇-简单入门教程2

标签:+=   print   入门   基本   ide   import   rac   float   1.0   

原文地址:https://www.cnblogs.com/yangzhaonan/p/10427462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!