Cách tạo ma trận lớn trong python
NumPy là một phần mở rộng của ngôn ngữ lập trình Python, bổ sung hỗ trợ cho các mảng và ma trận lớn, đa chiều (số), cùng với một thư viện lớn các hàm toán học cấp cao để hoạt động trên các mảng này Show
nguồn
thời gian tính toán
Tạo mảng¶Tạo ndarrays từ danh sách. Ghi chú. mọi phần tử phải cùng loại (sẽ được chuyển đổi nếu có thể) import numpy as np data1 = [1, 2, 3, 4, 5] # list arr1 = np.array(data1) # 1d array data2 = [range(1, 5), range(5, 9)] # list of lists arr2 = np.array(data2) # 2d array arr2.tolist() # convert array back to list Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]] tạo mảng đặc biệt np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points Ngoài array([ 1., 10., 100., 1000.]) arange giống như phạm vi, ngoại trừ nó trả về một mảng (không phải danh sách) int_array = np.arange(5) float_array = int_array.astype(float) Kiểm tra mảng¶arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis) Ngoài 2 Định hình lại¶arr = np.arange(10, dtype=float).reshape((2, 5)) print(arr.shape) print(arr.reshape(5, 2)) Ngoài ________số 8 Thêm một trục a = np.array([0, 1]) a_col = a[:, np.newaxis] print(a_col) #or a_col = a[:, None] Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]0 chuyển vị [[1, 2, 3, 4], [5, 6, 7, 8]]1 Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]2 làm phẳng. luôn trả về một bản sao phẳng của mảng ban đầu [[1, 2, 3, 4], [5, 6, 7, 8]]3 Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]4 Ravel. trả về chế độ xem của mảng ban đầu bất cứ khi nào có thể [[1, 2, 3, 4], [5, 6, 7, 8]]5 Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]6 Tóm tắt về trục, định hình lại/làm phẳng và lựa chọn¶Numpy nội bộ. Theo mặc định, Numpy sử dụng quy ước C, tức là ngôn ngữ Row-major. Ma trận được lưu trữ bởi các hàng. Trong C, chỉ mục cuối cùng thay đổi nhanh nhất khi một người di chuyển qua mảng được lưu trữ trong bộ nhớ Đối với mảng 2D, việc di chuyển tuần tự trong bộ nhớ sẽ
Đối với mảng 3D, việc di chuyển tuần tự trong bộ nhớ sẽ
[[1, 2, 3, 4], [5, 6, 7, 8]]7 Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]8 Định hình lại thành 3D (trục 0, trục 1, trục 2) [[1, 2, 3, 4], [5, 6, 7, 8]]9 Ngoài np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points0 Lựa chọn lấy kế hoạch đầu tiên np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points1 Ngoài np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points2 Lựa chọn lấy hàng đầu tiên np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points3 Ngoài np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points4 Lựa chọn lấy cột đầu tiên np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points5 Ngoài np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points6 Ví dụ đơn giản với 2 mảng Tập thể dục
np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points7 Ngoài np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points8 Ravel np.zeros(10) np.zeros((3, 6)) np.ones(10) np.linspace(0, 1, 5) # 0 to 1 (inclusive) with 5 points np.logspace(0, 3, 4) # 10^0 to 10^3 (inclusive) with 4 points9 Ngoài [[1, 2, 3, 4], [5, 6, 7, 8]]8 Ngăn xếp mảng¶array([ 1., 10., 100., 1000.])1 xếp ngang array([ 1., 10., 100., 1000.])2 Ngoài array([ 1., 10., 100., 1000.])3 xếp chồng dọc array([ 1., 10., 100., 1000.])4 Ngoài array([ 1., 10., 100., 1000.])5 Dọc mặc định array([ 1., 10., 100., 1000.])6 Ngoài array([ 1., 10., 100., 1000.])5 Lựa chọn¶Một vật thể array([ 1., 10., 100., 1000.])8 Ngoài array([ 1., 10., 100., 1000.])9 Cắt lát¶cú pháp. 24 với 25 (mặc định 0) 26 (mặc định cuối cùng) 27 (mặc định 1) int_array = np.arange(5) float_array = int_array.astype(float)0 Ngoài int_array = np.arange(5) float_array = int_array.astype(float)1 Cắt lát trả về một dạng xem (không phải bản sao) Sửa đổi int_array = np.arange(5) float_array = int_array.astype(float)2 Ngoài int_array = np.arange(5) float_array = int_array.astype(float)3 Hàng 0. thứ tự đảo ngược int_array = np.arange(5) float_array = int_array.astype(float)4 Ngoài int_array = np.arange(5) float_array = int_array.astype(float)5 lập chỉ mục ưa thích. Lập chỉ mục mảng số nguyên hoặc boolean¶Lập chỉ mục ưa thích trả về một bản sao không phải là một dạng xem Lập chỉ mục mảng số nguyên int_array = np.arange(5) float_array = int_array.astype(float)6 Ngoài int_array = np.arange(5) float_array = int_array.astype(float)7 Lập chỉ mục mảng Boolean int_array = np.arange(5) float_array = int_array.astype(float)8 Ngoài int_array = np.arange(5) float_array = int_array.astype(float)9 Tuy nhiên, trong bối cảnh lập chỉ mục lvalue (giá trị bên trái của một phép gán) Fancy cho phép sửa đổi mảng ban đầu arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)0 Ngoài arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)1 Lập chỉ mục mảng Boolean tiếp tục arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)2 Ngoài arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)3 Hoạt động véc tơ¶arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)4 Ngoài arr1.dtype # float64 arr2.ndim # 2 arr2.shape # (2, 4) - axis 0 is rows, axis 1 is columns arr2.size # 8 - total number of elements len(arr2) # 2 - size of first dimension (aka axis)5 Phát thanh truyền hình¶nguồn. https. // tài liệu. scipy. org/doc/numpy-1. 13. 0/người dùng/cơ bản. phát thanh truyền hình. html Chuyển đổi ngầm định để cho phép thao tác trên các mảng có kích thước khác nhau. - Mảng nhỏ hơn được kéo dài hoặc “phát sóng” trên mảng lớn hơn để chúng có hình dạng tương thích. - Thao tác vector hóa nhanh trong C thay vì Python. - Không cần sao chép Quy tắc¶Bắt đầu với trục theo dõi và làm việc ngược lại, Numpy so sánh các kích thước mảng
|