Làm thế nào ma trận được lưu trữ trong python?

Trước khi bắt đầu, hãy đảm bảo rằng thư viện Numpy đã được cài đặt. Bạn có thể chỉ cần cài đặt thư viện trong Windows, bằng cách nhấp vào Menu Bắt đầu, nhập “cmd” và nhấn enter và nhập “pip install numpy”. Tuy nhiên, rất có thể, thư viện Numpy đã được cài đặt cùng với bản phân phối Python của bạn

Ví dụ, xét các ma trận sau

(1)  

Làm thế nào ma trận được lưu trữ trong python?

Sau khi nhập thư viện Numpy dưới dạng “np” (“np” chỉ là bí danh mà chúng ta đã chọn cho Numpy), chúng ta có thể định nghĩa các ma trận này trong Python như sau

import numpy as np 

#defining matrices
A=np.array([[1,2],[3,4]])
B=np.array([[5,6],[7,8]])
C=np.array([[1,2,3],[4,5,6],[7,8,9]])

Về cơ bản, chúng ta xác định ma trận theo hàng, sử dụng hàm “np. mảng()". Các hàng được bao quanh bởi “[ ]” và các hàng được phân tách bằng dấu phẩy. Ngoài ra, ở phần sau của bài đăng này, chúng tôi giải thích một phương pháp thay thế để xác định ma trận dựa trên hàm “np. ma trận()”

Một kỹ thuật hữu ích khác để xác định ma trận là chỉ định trực tiếp kiểu dữ liệu trong khi xác định ma trận. Điều này có thể cần thiết nếu chúng ta muốn chuyển dữ liệu ma trận từ Python sang một số ngôn ngữ lập trình khác, chẳng hạn như MATLAB chẳng hạn. Ví dụ, chúng ta có thể chỉ định rằng các mục nhập của ma trận sẽ được lưu trữ dưới dạng tăng gấp đôi bằng cách nhập

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)

và với phương thức “dtype”, chúng ta có thể lấy được kiểu dữ liệu

G.dtype

Ma trận không và ma trận đồng nhất có thể được định nghĩa như sau

# zero and identity matrices
I4=np.identity(4)
Z4=np.zeros(shape=(4,4))

Chúng ta có thể truy cập các hàng, cột và khối của ma trận bằng các lệnh sau đây tương tự như các lệnh cơ bản của MATLAB

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]

Ở đây, cần lưu ý rằng Python liệt kê các mục nhập mảng từ 0 chứ không phải từ 1. Do đó, ký hiệu C[0,0], tương ứng với mục (1,1) của ma trận C

Kích thước ma trận có thể thu được bằng cách sử dụng các dòng mã sau

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]

Có ít nhất hai cách để tính toán chuyển vị ma trận

#transpose 
A.T
np.transpose(A)

Phép cộng (trừ) hai ma trận đơn giản

#add two matrices 
D=A+B

Tuy nhiên, toán tử nhân “*” trong Numpy biểu thị phép nhân theo phần tử và nó không phải là toán tử nhân ma trận. Bạn có thể thuyết phục bản thân bằng cách gõ

________số 8

Có ít nhất hai cách để thực hiện đúng phép nhân ma trận. Cái đầu tiên được đưa ra dưới đây

Dcorrect=A.dot(B) # THIS IS CORRECT WAY FOR MULTIPLYING TWO MATRICES

Có một phương pháp khác để xác định ma trận trong Python. Cá nhân tôi thích phương pháp được giải thích bên dưới hơn phương pháp dựa trên “np. hàm mảng()”. Đây là cách chúng tôi xác định ma trận và thực hiện các hoạt động ma trận cơ bản

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
0

Ma trận khối có thể được định nghĩa như sau

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
1

Đảo ngược ma trận có thể được thực hiện như sau

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
2

Có ít nhất hai cách để lưu và tải ma trận từ tệp. Cách tiếp cận đầu tiên là lưu ma trận trong một “. định dạng thảm. Định dạng này là định dạng MATLAB để lưu ma trận. Chúng tôi sẽ xác định một ma trận và trên cơ sở ma trận này, chúng tôi sẽ xác định một từ điển sẽ được lưu dưới dạng “. mat” loại tập tin. Sau đó, chúng tôi sẽ tải ma trận từ tệp

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
3

Một cách khác để lưu trữ ma trận là lưu trữ chúng trực tiếp bằng hàm “save()” của Numpy. Chúng ta có thể tải các ma trận bằng hàm “load()”. Các ma trận được lưu trong một tệp có phần mở rộng “. npy”

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
4

Đó sẽ là tất cả. Tôi hy vọng rằng bạn đã học được một cái gì đó mới ngày hôm nay. Trong bài đăng tiếp theo của chúng tôi, chúng tôi sẽ giải thích cách giải các hệ phương trình tuyến tính trong Python và cách tính toán các phân tách ma trận cơ bản

Ma trận không là gì ngoài sự sắp xếp dữ liệu hoặc số theo hình chữ nhật. Nói cách khác, nó là một mảng dữ liệu hoặc số hình chữ nhật. Các mục theo chiều ngang trong ma trận được gọi là 'hàng' trong khi các mục theo chiều dọc được gọi là 'cột'. Nếu một ma trận có r số hàng và c số cột thì thứ tự của ma trận là r x c. Mỗi phần tử trong ma trận có thể là giá trị nguyên, giá trị động hoặc thậm chí có thể là số phức

ví dụ

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
5

Trong Python, chúng ta có thể lấy ma trận đầu vào của người dùng theo nhiều cách khác nhau. Một số phương thức cho ma trận đầu vào của người dùng trong Python được hiển thị bên dưới

Mã số 1




# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
8

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
0_______51
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2____53____54
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
61____162

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
63
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2____53____54
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
69
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
62

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

G.dtype
12

G.dtype
13
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
G.dtype
15

G.dtype
16
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3____218
G.dtype
19

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
81

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
83
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
86
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
87

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
88
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
89____51
G.dtype
15

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
88
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
94
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
97____498

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
99
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
00
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2____53
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
4
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
04

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
88
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
06

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
08

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
83
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
13

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
88
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
94
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
19

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
20
G.dtype
16____522
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
24
G.dtype
19

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
88
G.dtype
16
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
28

đầu ra

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
6

Lót




# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
29

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
30
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
32
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
4
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
36
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
38
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
41
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
82
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
43
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
84
# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
85
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
46

 
Mã số 2. Sử dụng chức năng

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
47 và
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
48.

Trong Python tồn tại một thư viện phổ biến tên là NumPy. Thư viện này là một thư viện cơ bản cho bất kỳ tính toán khoa học nào. Nó cũng được sử dụng cho mảng nhiều chiều và như chúng ta đã biết ma trận là mảng chữ nhật, chúng ta sẽ sử dụng thư viện này cho ma trận đầu vào của người dùng




# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
49
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
600

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
0_______51
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2____53____54
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
61____162

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
63
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2____53____54
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
69
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
62

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

G.dtype
16
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3____1622
G.dtype
19

# first row 
C[0,:]

# first column
C[:,0]

# first two rows

C[0:2,:]

# sub-block
C[0:2,0:2]

 C[-2:,-2:]
9

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
625

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
626

G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
627
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
1
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
629_______53
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
631
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
3
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
2
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
634
# get the number of rows and columns 
A.shape

#row number directly
A.shape[0]

#column number directly
A.shape[1]
4
G=np.array([[1, 2, 3], [4, 5, 6]], np.double)
636

Ma trận được lưu trữ trong Numpy như thế nào?

Ma trận được lưu trữ theo hàng . Lưu ý rằng trong cả hai trường hợp, nó cho rằng quy ước ma trận để lập chỉ mục đang được sử dụng, tôi. e. , đối với cả Fortran và C, chỉ mục đầu tiên là hàng. Lưu ý quy ước này ngụ ý rằng quy ước lập chỉ mục là bất biến và thứ tự dữ liệu thay đổi để giữ nguyên như vậy.

Ma trận được biểu diễn bằng Python như thế nào?

Trong Python, Ma trận được biểu diễn theo kiểu dữ liệu danh sách .

Có thư viện ma trận cho Python không?

matlib ) Mô-đun này chứa tất cả các hàm trong không gian tên gọn gàng, với các hàm thay thế sau trả về ma trận thay vì ndarray. Diễn giải đầu vào dưới dạng ma trận.

Tải thêm tài liệu liên quan đến bài viết Làm thế nào ma trận được lưu trữ trong python?