交叉验证python代码
时间: 2023-10-13 15:27:17 浏览: 86
Python2.7.11在ARM平台上交叉编译步骤和方法
以下是一些基本的交叉验证Python代码:
使用Python的sklearn模块执行5倍交叉验证:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
X = iris.data
y = iris.target
clf = DecisionTreeClassifier(random_state=0)
scores = cross_val_score(clf, X, y, cv=5)
print(scores)
```
使用Python的sklearn模块执行留一法交叉验证:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import LeaveOneOut
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
X = iris.data
y = iris.target
clf = DecisionTreeClassifier(random_state=0)
loo = LeaveOneOut()
scores = cross_val_score(clf, X, y, cv=loo)
print(scores)
```
使用Python的sklearn模块执行k折交叉验证:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
X = iris.data
y = iris.target
clf = DecisionTreeClassifier(random_state=0)
kf = KFold(n_splits=5)
scores = cross_val_score(clf, X, y, cv=kf)
print(scores)
```
阅读全文