def predictKFoldSVM(X, y, kernel="linear", C=1, selectKBest=0, kfold=10):
"""
Classifies the data using Support vector machines and k-fold CV
:param X: The matrix of feature vectors
:type X: list
:param y: The vector containing the labels corresponding to feature vectors
:type y: list
:param kernel: The kernel used to elevate data into higher dimensionalities
:type kernel: str
:param C: The penalty parameter of the error term
:type C: int
:param selectKBest: The number of best features to select
:type selectKBest: int
:param kfold: The number of folds to use in K-fold CV
:type kfold: int
:return: A list of predicted labels across the k-folds
"""
try:
# Prepare data
X, y = numpy.array(X), numpy.array(y)
# Define classifier
clf = svm.SVC(kernel=kernel, C=C)
# Select K Best features if enabled
X_new = SelectKBest(chi2, k=selectKBest).fit_transform(X, y) if selectKBest > 0 else X
predicted = cross_val_predict(clf, X_new, y, cv=kfold).tolist()
except Exception as e:
prettyPrintError(e)
return []
return predicted
评论列表
文章目录