Python之Numpy库的简单介绍(一)
发布日期:2021-05-10 06:25:20 浏览次数:11 分类:精选文章

本文共 4389 字,大约阅读时间需要 14 分钟。

Python���Numpy���������������������������

Numpy���������������������������������������������������������������������������������������������������������������������������Numpy���������������������������

������������Numpy���

���������Numpy������������������������������������������������������������

import numpy as np  # ���numpy���������np������������������������np������Numpy������
import numpy # ������������numpy������������

������Numpy������

np.array()���������������������Numpy������������������

vector = np.array([1, 2, 3, 4, 5])    # ������������������
matrix = np.array([[1, 2], # ���������������������2x2���
[3, 4],
[5, 6]])
cube = np.array([[[7,8,9], # ���������������������2x2x3���
[10,11,12]]],
dtype=np.float16)

���������cube���������������������������������������������������������������������������������������������������������������������������������������

Numpy���������������

np.array���������������������������������������������������������������������������������������

  • dtype���������������������������������������������������������������������������������������
  • order���������������������������������������������C������������������F������������������
  • a.shape������������������������������������...������
  • a.size������������������������������������
  • a.itemsize���������������������������������
  • a.nbytes������������������������������������
  • a.min() ��� a.max()���������������������������������������������������

���������������������

np.arange()���������������������������������������

np.arange(12)       #������[0, 1, 2,..., 11]
np.arange(3, 9, 1) #������[3,4,5,6,7,8]
np.arange(0, 15, 3) #������[0, 3, 6, 9, 12]

������������������

np.ndarray������������������������������������������������������������������

# ���������������������3x4���������������������
a = np.ndarray(shape=(3,4), dtype=int, buffer=np.arange(15).reshape(3,5))

������buffer���������������������������������������������������������������������������������������������������������������������������������buffer���Numpy������������������������������������

���������������������������

a = np.array([[1,2,3],[4,5,6]], np.float32)
b = np.ndarray(shape=(3,3), dtype=np.float64)

���������������������a���������������������������������������b������������ndarray���������������������������������������������������������������������������������������������������������

������������������������������

np.zeros() ��� np.ones() ���������������������������������������������������

# ������������������������������3x4x5������������������
np.zeros(shape=(3,4,5))
np.ones(shape=(3,4,5))

������������������������

Numpy������������������������������������������������������������������������������������

������np.dot()���������������������������������

matrix = np.array([[1,3,4],     # ������������A
[2,24,5],
[4,6,7]])
# ���������������������������������A * B
product = np.dot(matrix, matrix)
# ������������������
result = np.dot(matrix, np.array([2,3,4]))

���������������������������np.sum()������������������������������������

# ���������������������
matrix.sum(axis=1) # ������������������������
matrix.sum(axis=0) # ������������������������

���������������������������

������np.hsplit() ��� np.vsplit() ���������������������������������������������������������������������

# ������data_shape = (10, 5)
split_data = np.vsplit(data_shape, 2) # ���������������������������
# ������������������������
stacked_data = np.hstack((data1, data2)) # ������������������������

np.concatenate() ������������������������������������������������������������������������������������

a = np.array([[1,2,3], [4,5,6]])
b = np.array([[7,8,9], [10,11,12]])
# ���������������������������
merged_data = np.concatenate((a, b), axis=0)
# ������������������������������
merged_data = np.concatenate((a, b), axis=1)

���������������������������������

  • ���������������������������������������������������������������������������
  • ���������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������Numpy���������������������������������������������������������������������������������������������������������������������������

上一篇:Python之Numpy库的简单介绍(二)
下一篇:C++ primer第十章—泛型算法

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月26日 05时29分57秒