Danh sách chỉ mục Python với mảng boolean

Để gọn gàng bên dưới chúng ta sẽ chỉ hiển thị các số trong mảng đến 2 chữ số thập phân. Điều này không ảnh hưởng đến bất kỳ phép tính nào, nó chỉ thay đổi những gì chúng ta thấy khi hiển thị mảng trong Jupyter

# Set how many decimal places to display when showing arrays.
np.set_printoptions[precision=2]

Chọn giá trị với mảng Boolean

Ở đây chúng tôi đang sử dụng mảng Boolean để lập chỉ mục cho các mảng khác. Bạn sẽ thấy điều chúng tôi muốn nói ở cuối phần này

Chúng ta thường muốn chọn một số phần tử từ một mảng theo một số tiêu chí

Cách phổ biến nhất để thực hiện việc này là thực hiện cắt mảng, sử dụng mảng Boolean giữa các dấu ngoặc vuông

Có thể dễ hiểu điều này bằng ví dụ hơn là mô tả

Chúng tôi bắt đầu với bộ dữ liệu RateMyProfessors

Đó là một bảng trong đó các hàng là các môn học và các cột chứa các giá trị xếp hạng trung bình của sinh viên cho các môn học tương ứng. Chúng tôi sẽ tìm nạp các cột từ bảng này dưới dạng mảng

Nếu bạn đang chạy trên máy tính xách tay của mình, bạn nên tải xuống tệp

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
9 vào cùng thư mục với sổ ghi chép này

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]

Bây giờ chúng tôi có tên của các ngành có số lượng giáo sư lớn nhất

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]

Dưới đây là điểm số “Dễ dàng” cho sáu khóa học lớn nhất

array[[3.16, 3.06, 2.71, 3.32, 3.05, 2.65]]

Đây là những xếp hạng dễ dàng tương ứng với

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
0 mà chúng ta đã thấy trước đó. Kỷ luật hàng đầu [lớn nhất] là

Xếp hạng Dễ dàng cho khóa học đó là

Mảng Boolean là mảng chứa các giá trị là một trong các giá trị Đúng hoặc Sai

Đây là một mảng Boolean, được tạo từ việc áp dụng so sánh với một mảng

greater_than_3 = easiness > 3
greater_than_3

array[[ True,  True, False,  True,  True, False]]

Giá trị này có giá trị

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
1 tại vị trí của các phần tử > 3 và
array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
2 nếu không

Chúng ta có thể làm những việc như đếm số lượng giá trị

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
1 trong mảng Boolean

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
1

Bây giờ giả sử rằng chúng ta muốn lấy các phần tử từ

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
4 lớn hơn 3. Tức là chúng ta muốn lấy các phần tử trong
array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
4 mà phần tử tương ứng trong
array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
6 là
array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
1

Chúng ta có thể làm điều này với lập chỉ mục mảng Boolean. Mảng Boolean nằm giữa dấu ngoặc vuông, sau tên mảng. Như một lời nhắc nhở

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
6

array[[3.16, 3.06, 2.71, 3.32, 3.05, 2.65]]

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
8

array[[ True,  True, False,  True,  True, False]]

Chúng tôi đặt mảng Boolean giữa các dấu ngoặc vuông, sau mảng mà chúng tôi muốn lấy giá trị từ đó, như thế này

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
1

# We have not covered this code yet.  We will soon.
# Load the library for reading data files.
import pandas as pd
# Read the file into a table, select the first six rows.
big_courses = pd.read_csv['rate_my_course.csv'].head[6]
# Put the columns into arrays, each with six elements.
# The disciplines [names of disciplines].
disciplines = np.array[big_courses['Discipline']]
# The corresponding average scores for Easiness.
easiness = np.array[big_courses['Easiness']]
2

Chúng tôi đã chọn các số trong

array[['English', 'Mathematics', 'Biology', 'Psychology', 'History',
       'Chemistry'], dtype=object]
4 lớn hơn 3

Xem hình dưới đây để minh họa về những gì đang xảy ra

Chúng ta có thể sử dụng cùng một mảng Boolean này để lập chỉ mục cho một mảng khác. Ví dụ: ở đây chúng tôi hiển thị tên môn học tương ứng với các khóa học có điểm Dễ dàng lớn hơn 3

Chủ Đề