Đố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ệuMã 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 PandasHình ảnh của Peggy und Marco Lachmann-Anke từ PixabayNó 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ệuTrong 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_iris import pandas as pddata = load_iris() df = pd.DataFrame(data.data, columns=data.feature_names) Hình ảnh của tác giả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ácTrong 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:subset = df.sample(n=100) 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:subset.shape trong đó cung cấp đầu ra sau: (100, 4) Tỷ lệ phần trămNế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:subset = df.sample(frac=0.5) 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ệnLấ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)'] < 3 Biế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].index Trong 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:subset = df[condition].sample(n = 10) Lấy mẫu với tốc độ không đổiMộ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: Hình ảnh của tác giả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.shape trong đó 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ệuKhi 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ênGiả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.shape 1:>>> 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]
4Giải pháp thứ haiGiả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:>>> 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]
5Bản tóm tắtTrong 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. Những bài viết liên quan
Làm thế nào để bạn xử lý một bộ dữ liệu lớn trong Python?
3 cách để đối phó với các bộ dữ liệu lớn trong Python.Là một nhà khoa học dữ liệu, tôi thấy mình ngày càng phải đối phó với dữ liệu lớn..... Giảm sử dụng bộ nhớ bằng cách tối ưu hóa các loại dữ liệu..... Chia dữ liệu thành các khối..... Tận dụng đánh giá lười biếng ..
Python có thể xử lý 1 tỷ hàng không?
Giới thiệu về Vaex.VAEX là một thư viện Python là một khung dữ liệu ngoài core, có thể xử lý tới 1 tỷ hàng mỗi giây.Vaex is a python library that is an out-of-core dataframe, which can handle up to 1 billion rows per second.
Làm thế nào để bạn xử lý các bộ dữ liệu lớn với bộ nhớ hạn chế?
Bạn có thể xử lý dữ liệu không phù hợp với bộ nhớ bằng cách sử dụng bốn kỹ thuật cơ bản: chi tiền, nén, chunking và lập chỉ mục.spending money, compression, chunking, and indexing.
Dask có nhanh hơn gấu trúc không?
DASK chạy nhanh hơn gấu trúc cho truy vấn này, ngay cả khi loại cột không hiệu quả nhất được sử dụng, bởi vì nó song song với các tính toán.Pandas chỉ sử dụng 1 lõi CPU để chạy truy vấn.Máy tính của tôi có 4 lõi và dask sử dụng tất cả các lõi để chạy tính toán. for this query, even when the most inefficient column type is used, because it parallelizes the computations. pandas only uses 1 CPU core to run the query. My computer has 4 cores and Dask uses all the cores to run the computation. |