-
-
Notifications
You must be signed in to change notification settings - Fork 261
modified cross_validation.rst file with a k-fold cross validation example #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
806e7f7
47b3d31
5a80184
c89ac72
7c5099b
ec84a31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ The interface for splitting Dask arrays is the same as scikit-learn's version. | |
|
||
X_train.compute()[:3] | ||
|
||
|
||
While it's possible to pass dask arrays to :func:`sklearn.model_selection.train_test_split`, we recommend | ||
using the Dask version for performance reasons: the Dask version is faster | ||
for two reasons: | ||
|
@@ -36,4 +35,49 @@ However, if there's a strong pattern in your data, you'll want to perform a full | |
Second, the Dask version avoids allocating large intermediate NumPy arrays storing the indexes for slicing. | ||
For very large datasets, creating and transmitting ``np.arange(n_samples)`` can be expensive. | ||
|
||
Here is another illustration of performing k-fold cross validation purely in Dask. Here a link to gather more information on k-fold cross validation :class:`dask_ml.model_selection.KFold`: | ||
|
||
.. ipython:: python | ||
|
||
import dask.array as da | ||
from dask_ml.model_selection import KFold | ||
from dask_ml.datasets import make_regression | ||
from dask_ml.linear_model import LinearRegression | ||
from statistics import mean | ||
|
||
X, y = make_regression(n_samples=200, # choosing number of observations | ||
n_features=5, # number of features | ||
random_state=0, # random seed | ||
chunks=20) # partitions to be made | ||
|
||
train_scores: list[int] = [] | ||
test_scores: list[int] = [] | ||
|
||
model = LinearRegression() | ||
|
||
The Dask kFold method splits the data into k consecutive subsets of data. Here we specify k to be 5, hence, 5-fold cross validation | ||
|
||
|
||
.. ipython:: python | ||
|
||
kf = KFold(n_splits=5) | ||
|
||
for i, j in kf.split(X): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the doc build failed here: https://github.com/dask/dask-ml/actions/runs/9434455032/job/26270343032?pr=994 Is that from the lack of a newline after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for spotting that, added a new line |
||
X_train, X_test = X[i], X[j] | ||
y_train, y_test = y[i], y[j] | ||
|
||
model.fit(X_train, y_train) | ||
|
||
train_score = model.score(X_train, y_train) | ||
test_score = model.score(X_test, y_test) | ||
|
||
train_scores.append(train_score) | ||
test_scores.append(test_score) | ||
|
||
print("mean training score:", mean(train_scores)) | ||
print("mean testing score:", mean(train_scores)) | ||
|
||
|
||
|
||
|
||
.. _scikit-learn cross validation documentation: http:/scikit-learn.org/stable/modules/cross_validation.html |
Uh oh!
There was an error while loading. Please reload this page.