gpt4 book ai didi

python - 从头开始进行交叉验证

转载 作者:行者123 更新时间:2023-11-30 09:48:39 25 4
gpt4 key购买 nike

我在 Stack Overflow 上找到了这个函数定义:

def fold_i_of_k(dataset, i, k):
n = len(dataset)
return len(dataset[n*(i-1)//k:n*i//k])


# this is my code below

#Constants
FOLD_I = 1
FOLD_K =10

#Creating 10 folds
counter = 1
s=0
total_ac = 0
while counter!=FOLD_K+1:
print("Fold ",counter)
fold = fold_i_of_k(dataset,counter,10)
d_fold = dataset[s:s + fold]
#print(d_fold.index.values)
#print(d_fold.iloc[1:3,0:2])
d_test = d_fold
X_test = d_test.iloc[:,0:11]
y_test = d_test.iloc[:,11:12]
d_train = dataset.drop(dataset.index[s:s+fold])
X_train = d_train.iloc[:,0:11]
y_train = d_train.iloc[:,11:12]
##print(dataset)
##print(d_fold)
##print(d_train)
##print(d_test)
##print(len(X_train))
##print(len(y_train))
##print(X_test)
##print(y_test)
#print(fold)
X_train = X_train.as_matrix()
X_train = preprocessing.scale(X_train)

y_train = y_train.as_matrix()

X_test = X_test.as_matrix()
X_test = preprocessing.scale(X_test)

y_test = y_test.as_matrix()

#l1 = len(y_train)
#np.reshape(y_train, l1)
#print(y_train)
from numpy import array
#l = len(y_test)
#np.reshape(y_test, l)
#print(y_test)

data.reshape((data.shape[0], 1))

y_train = array(y_train)
print(y_train.shape)
lr = LogisticRegression()
lr.fit(X_train,y_train)
#lr_pred = lr.predict(X_test)
#ac = accuracy_score(y_test,lr_pred)
#print(ac)
##print(classification_report(y_test,lr_pred))

total_ac = total_ac + ac
s = s + fold
counter= counter+1

total_ac = total_ac / FOLD_K
print("Cross validation accuracy is: ",total_ac)`

我收到以下错误:

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().

y = column_or_1d(y, warn=True)

如何修复它?

最佳答案

y_train.ravel() 解决了问题。

关于python - 从头开始进行交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com