我正在关注张量流的IRIS示例.
我现在的情况是我将所有数据都放在一个CSV文件中,而不是分开,我想对该数据应用k-fold交叉验证.
我有
data_set = tf.contrib.learn.datasets.base.load_csv(filename="mydata.csv",
target_dtype=np.int)
Run Code Online (Sandbox Code Playgroud)
如何使用多层神经网络对此数据集执行k-fold交叉验证,与IRIS示例相同?
使用下面代码中所示的k -fold 方法cross_val_predict(参见doc,v0.18)是否计算每次折叠的准确度并最终平均它们?
cv = KFold(len(labels), n_folds=20)
clf = SVC()
ypred = cross_val_predict(clf, td, labels, cv=cv)
accuracy = accuracy_score(labels, ypred)
print accuracy
Run Code Online (Sandbox Code Playgroud) 我目前正在研究一个问题,该问题比较了同一数据集上三种不同的机器学习算法性能.我将数据集划分为70/30个训练/测试集,然后使用GridSearchCV和网格搜索每个算法的最佳参数X_train, y_train.
第一个问题,我想在训练集上进行网格搜索还是假设在整个数据集上?
第二个问题,我知道GridSearchCV在其实现中使用了K-fold,这是否意味着如果我X_train, y_train在GridSearchCV中比较的所有三种算法都使用了相同的交叉验证?
任何答案都将不胜感激,谢谢.
python machine-learning scikit-learn cross-validation grid-search
这是我的目标(y):
target = [7,1,2,2,3,5,4,
1,3,1,4,4,6,6,
7,5,7,8,8,8,5,
3,3,6,2,7,7,1,
10,3,7,10,4,10,
2,2,2,7]
Run Code Online (Sandbox Code Playgroud)
我不知道为什么在执行时:...#将数据集拆分为两个相等的部分X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.5,random_state = 0)
...
# Split the data set in two equal parts
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0)
# Set the parameters by cross-validation
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
scores = ['precision', 'recall']
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(SVC(C=1), …Run Code Online (Sandbox Code Playgroud) 我已经仔细阅读了CARET文档:http://caret.r-forge.r-project.org/training.html,插图,一切都很清楚(网站上的例子帮助很多!)但是我仍然对两个论点之间的关系感到困惑trainControl:
method
index
Run Code Online (Sandbox Code Playgroud)
和之间的相互作用trainControl和数据分割在插入符号的功能(例如createDataPartition,createResample,createFolds和createMultiFolds)
为了更好地构建我的问题,让我使用文档中的以下示例:
data(BloodBrain)
set.seed(1)
tmp <- createDataPartition(logBBB,p = .8, times = 100)
trControl = trainControl(method = "LGOCV", index = tmp)
ctreeFit <- train(bbbDescr, logBBB, "ctree",trControl=trControl)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如果我使用createDataPartition(这是我认为不分层引导),如上面的例子中,我传递的结果index,以trainControl做我需要使用LGOCV在我的电话的方法trainControl?如果我使用另一个(例如cv)它会有什么不同?在我的脑海中,一旦你修复index,你基本上选择了交叉验证的类型,所以我不确定method你使用时扮演什么角色index.
createDataPartition和之间有什么区别createResample?这是createDataPartition分层自举,而createResample不是吗?
3)如何使用插入符号进行分层 k折叠(例如10倍)交叉验证?以下是否会这样做?
tmp <- createFolds(logBBB, …Run Code Online (Sandbox Code Playgroud) 我想使用xgboost cv函数来查找我的训练数据集的最佳参数.api我很困惑.我如何找到最佳参数?这与sklearn grid_search交叉验证功能类似吗?如何max_depth确定参数([2,4,6])的哪个选项最佳?
from sklearn.datasets import load_iris
import xgboost as xgb
iris = load_iris()
DTrain = xgb.DMatrix(iris.data, iris.target)
x_parameters = {"max_depth":[2,4,6]}
xgb.cv(x_parameters, DTrain)
...
Out[6]:
test-rmse-mean test-rmse-std train-rmse-mean train-rmse-std
0 0.888435 0.059403 0.888052 0.022942
1 0.854170 0.053118 0.851958 0.017982
2 0.837200 0.046986 0.833532 0.015613
3 0.829001 0.041960 0.824270 0.014501
4 0.825132 0.038176 0.819654 0.013975
5 0.823357 0.035454 0.817363 0.013722
6 0.822580 0.033540 0.816229 0.013598
7 0.822265 0.032209 0.815667 0.013538
8 0.822158 0.031287 0.815390 0.013508
9 0.822140 0.030647 …Run Code Online (Sandbox Code Playgroud) 我适合一个Pipeline物体RandomizedSearchCV
pipe_sgd = Pipeline([('scl', StandardScaler()),
('clf', SGDClassifier(n_jobs=-1))])
param_dist_sgd = {'clf__loss': ['log'],
'clf__penalty': [None, 'l1', 'l2', 'elasticnet'],
'clf__alpha': np.linspace(0.15, 0.35),
'clf__n_iter': [3, 5, 7]}
sgd_randomized_pipe = RandomizedSearchCV(estimator = pipe_sgd,
param_distributions=param_dist_sgd,
cv=3, n_iter=30, n_jobs=-1)
sgd_randomized_pipe.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
我想访问该coef_属性,best_estimator_但我无法做到这一点.我尝试使用coef_下面的代码访问.
sgd_randomized_pipe.best_estimator_.coef_
但是我得到以下AttributeError ...
AttributeError:'Pipeline'对象没有属性'coef_'
scikit-learn文档说这coef_是属性SGDClassifier,属于我的类base_estimator_.
我究竟做错了什么?
我想用lgb.Dataset对LightGBM模型进行交叉验证,并使用early_stopping_rounds.以下方法在XGBoost的xgboost.cv中没有问题.我不想将Scikit Learn的方法与GridSearchCV一起使用,因为它不支持提前停止或lgb.Dataset.
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error
dftrainLGB = lgb.Dataset(data = dftrain, label = ytrain, feature_name = list(dftrain))
params = {'objective': 'regression'}
cv_results = lgb.cv(
params,
dftrainLGB,
num_boost_round=100,
nfold=3,
metrics='mae',
early_stopping_rounds=10
)
Run Code Online (Sandbox Code Playgroud)
任务是进行回归,但以下代码会引发错误:
Supported target types are: ('binary', 'multiclass'). Got 'continuous' instead.
LightGBM支持回归,还是我提供了错误的参数?
在使用cross_validation.KFold(n,n_folds = folds)之后,我想访问索引以进行单折的训练和测试,而不是遍历所有折叠.
那么我们来看一下示例代码:
from sklearn import cross_validation
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([1, 2, 3, 4])
kf = cross_validation.KFold(4, n_folds=2)
>>> print(kf)
sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False,
random_state=None)
>>> for train_index, test_index in kf:
Run Code Online (Sandbox Code Playgroud)
我想像这样访问kf中的第一个折叠(而不是for循环):
train_index, test_index in kf[0]
Run Code Online (Sandbox Code Playgroud)
这应该只返回第一个折叠,但我得到错误:"TypeError:'KFold'对象不支持索引"
我想要的输出:
>>> train_index, test_index in kf[0]
>>> print("TRAIN:", train_index, "TEST:", test_index)
TRAIN: [2 3] TEST: [0 1]
Run Code Online (Sandbox Code Playgroud)
链接:http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.KFold.html
如何检索列车和测试的索引只有一个折叠,而不通过整个for循环?
我有一个包含约300个点和32个不同标签的数据集,我想通过使用网格搜索和LabelKFold验证绘制其学习曲线来评估LinearSVR模型.
我的代码看起来像这样:
import numpy as np
from sklearn import preprocessing
from sklearn.svm import LinearSVR
from sklearn.pipeline import Pipeline
from sklearn.cross_validation import LabelKFold
from sklearn.grid_search import GridSearchCV
from sklearn.learning_curve import learning_curve
...
#get data (x, y, labels)
...
C_space = np.logspace(-3, 3, 10)
epsilon_space = np.logspace(-3, 3, 10)
svr_estimator = Pipeline([
("scale", preprocessing.StandardScaler()),
("svr", LinearSVR),
])
search_params = dict(
svr__C = C_space,
svr__epsilon = epsilon_space
)
kfold = LabelKFold(labels, 5)
svr_search = GridSearchCV(svr_estimator, param_grid = search_params, cv = ???)
train_space = …Run Code Online (Sandbox Code Playgroud) cross-validation ×10
python ×8
scikit-learn ×6
grid-search ×1
lightgbm ×1
pipeline ×1
r ×1
regression ×1
svm ×1
tensorflow ×1
xgboost ×1