Đối với bộ dữ liệu phân tách Stratifiedkfold sẽ giúp
This cross-validation object is a variation of KFold that returns stratified folds. The folds are made by preserving the percentage of samples for each class.CÁCH SỬ DỤNG
>>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3]Tiền xử lý dữ liệu
Mã sẵn sàng để chạy với các kỹ thuật khác nhau để lấy mẫu bộ dữ liệu trong Python Pandas
Nó có thể xảy ra rằng bạn chỉ cần một số hàng dữ liệu Python của bạn. Bạn có thể đạt được kết quả này, thông qua các kỹ thuật lấy mẫu khác nhau.different sampling techniques.
Trong hướng dẫn này, tôi minh họa các kỹ thuật sau đây để thực hiện lấy mẫu hàng qua Python Pandas:Python Pandas:
- lấy mẫu ngẫu nhiên
- lấy mẫu với điều kiện
- lấy mẫu với tốc độ không đổi
Mã đầy đủ có thể được tải xuống từ kho lưu trữ GitHub của tôi.
Tải tập dữ liệu
Trong hướng dẫn này, tôi khai thác bộ dữ liệu >>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 6, được cung cấp bởi thư viện >>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 7 và tôi chuyển đổi nó thành >>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 8 DataFrame:
from sklearn.datasets import load_irisimport pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
Bộ dữ liệu bao gồm 4 cột và 150 hàng.
Lấy mẫu ngẫu nhiên
Đưa ra một khung dữ liệu với các hàng n, trích xuất lấy mẫu ngẫu nhiên x các hàng ngẫu nhiên từ khung dữ liệu, với X ≤ N. Python >>> from sklearn.model_selection import StratifiedKFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> skf = StratifiedKFold(n_splits=2)
>>> skf.get_n_splits(X, y)
2
>>> print(skf)
StratifiedKFold(n_splits=2, random_state=None, shuffle=False)
>>> for train_index, test_index in skf.split(X, y):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
TRAIN: [1 3] TEST: [0 2]
TRAIN: [0 2] TEST: [1 3]
8 cung cấp một hàm, được đặt tên là from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0 để thực hiện lấy mẫu ngẫu nhiên.
Số lượng mẫu được trích xuất có thể được thể hiện theo hai cách thay thế:
- Chỉ định số lượng các hàng ngẫu nhiên chính xác để trích xuất
- Chỉ định tỷ lệ phần trăm của các hàng ngẫu nhiên để trích xuất. Tỷ lệ phần trăm được biểu thị bằng một số từ 0 đến 1.
Số lượng chính xác
Trong trường hợp này, bạn có thể chuyển tham số from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)1 cho hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0, như sau:
Trong ví dụ trước, hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0 trích xuất 100 hàng ngẫu nhiên. Bạn có thể kiểm tra hình dạng của bộ dữ liệu kết quả from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)4 thông qua hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)5:
trong đó cung cấp đầu ra sau:
(100, 4)Tỷ lệ phần trăm
Nếu bạn muốn chỉ định tỷ lệ phần trăm của các hàng ngẫu nhiên được trích xuất, bạn có thể chuyển tham số from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)6 làm đầu vào của hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0:
Trong ví dụ trước, hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0 trích xuất 50% các hàng ngẫu nhiên. Lưu ý rằng bạn có thể chỉ định chỉ một giữa các tham số from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)1 và from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)6.
Lấy mẫu với điều kiện
Lấy mẫu với cho phép điều kiện chỉ trích xuất một số hàng thỏa mãn một điều kiện nhất định. Thứ nhất, điều kiện phải được chỉ định. Ví dụ: như một điều kiện, bạn chỉ có thể chọn các hàng trong đó giá trị của cột subset = df.sample(n=100)1 nhỏ hơn 3:
condition = df['sepal width (cm)'] < 3Biến subset = df.sample(n=100)2 là một chuỗi có cùng kích thước của subset = df.sample(n=100)3, chứa subset = df.sample(n=100)4, tùy thuộc vào việc hàng có đáp ứng điều kiện hay không.
Sau đó, bạn truy xuất các chỉ mục được liên kết với các hàng đáp ứng điều kiện trước đó:
true_index = condition[condition == True].indexTrong ví dụ hiện tại, 57 hàng thỏa mãn điều kiện, do đó bạn có thể lấy mẫu ở mức tối đa 57 hàng.
Hàm from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)0 có thể được áp dụng để thực hiện lấy mẫu với điều kiện như sau:
Lấy mẫu với tốc độ không đổi
Một chiến lược lấy mẫu khác là lấy mẫu ở tốc độ không đổi, điều đó có nghĩa là bạn muốn có khoảng cách không đổi giữa hai mẫu liền kề. Ví dụ: bạn có thể muốn lấy mẫu với tỷ lệ 4, như được hiển thị trong hình sau:
Trong trường hợp này, trước tiên bạn chỉ định tỷ lệ:
>>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 0Và sau đó, đơn giản là bạn có thể trích xuất các mẫu:
>>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 1Trong bộ dữ liệu >>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 6, số lượng mẫu là 150, do đó, tỷ lệ mẫu 10 sẽ tạo ra một tập hợp con với 15 hàng:
subset.shapetrong đó cung cấp đầu ra sau:
>>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 3Nhận phần còn lại của bộ dữ liệu
Khi bạn đã trích xuất một tập hợp con của bộ dữ liệu, bạn cũng có thể trích xuất phần còn lại. Chiến lược này có thể được sử dụng, ví dụ, nếu bạn muốn phân chia bộ dữ liệu của mình trong tập huấn luyện và kiểm tra, ví dụ, mà không cần sử dụng subset = df.sample(n=100)7function do thư viện >>> from sklearn.model_selection import StratifiedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = StratifiedKFold(n_splits=2) >>> skf.get_n_splits(X, y) 2 >>> print(skf) StratifiedKFold(n_splits=2, random_state=None, shuffle=False) >>> for train_index, test_index in skf.split(X, y): ... print("TRAIN:", train_index, "TEST:", test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [1 3] TEST: [0 2] TRAIN: [0 2] TEST: [1 3] 7 cung cấp.
Hai giải pháp khả thi có thể được áp dụng để trích xuất phần còn lại của bộ dữ liệu. Cả hai giải pháp tạo ra cùng một kết quả.
Giải pháp đầu tiên
Giải pháp đầu tiên giảm các hàng của DataFrame được trích xuất (from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)4) trong DataFrame gốc subset = df.sample(n=100)3 và lưu trữ kết quả trong một khung dữ liệu mới. Điều này có thể đạt được bằng cách chuyển danh sách các chỉ mục sẽ bị xóa vào hàm subset.shape1:
Giải pháp thứ hai
Giải pháp thứ hai chỉ chọn các hàng, trong DataFrame gốc subset = df.sample(n=100)3, trong đó chỉ mục không nằm trong danh sách các chỉ mục của DataFrame được trích xuất from sklearn.datasets import load_iris
import pandas as pddata = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)4:
Bản tóm tắt
Trong hướng dẫn này, tôi đã minh họa cách thực hiện lấy mẫu dữ liệu theo những cách khác nhau:
- lấy mẫu ngẫu nhiên, trích xuất n hàng ngẫu nhiên;
- lấy mẫu với điều kiện, áp dụng một điều kiện và sau đó trích xuất một n ngẫu nhiên;
- lấy mẫu với tốc độ không đổi.
Nếu bạn muốn được cập nhật về nghiên cứu của tôi và các hoạt động khác, bạn có thể theo dõi tôi trên Twitter, YouTube và GitHub.