QSort随机分区

时间:2016-11-21 02:22:57

标签: java algorithm sorting qsort

qsort的以下实现来自“算法基础”一书,因此被认为是正确的。下面是我在Java中的实现。这是行不通的。问题是当随机选择分区时,生成的分区不正确。我希望有人可以告诉我我做错了什么:

鲍勃

    import optunity
import optunity.metrics
import numpy as np

# k nearest neighbours
from sklearn.neighbors import KNeighborsClassifier
# support vector machine classifier
from sklearn.svm import SVC
# Naive Bayes
from sklearn.naive_bayes import GaussianNB
# Random Forest
from sklearn.ensemble import RandomForestClassifier
from sklearn.externals import joblib
import sklearn
im_features,image_classes,training_names,stdSlr,kmeans,k = joblib.load("others.pkl")

n = len(image_classes)


data = im_features
labels = np.array(image_classes)


cv_decorator = optunity.cross_validated(x=data, y=labels, num_folds=5)

space = {'kernel': {'linear': {'C': [0, 2]},
                    'rbf': {'logGamma': [-5, 0], 'C': [0, 10]},
                    'poly': {'degree': [2, 5], 'C': [0, 5], 'coef0': [0, 2]}
                    }
         }


def train_model(x_train, y_train, kernel, C, logGamma, degree, coef0):
    """A generic SVM training function, with arguments based on the chosen kernel."""
    if kernel == 'linear':
        model = sklearn.svm.SVC(kernel=kernel, C=C)
    elif kernel == 'poly':
        model = sklearn.svm.SVC(kernel=kernel, C=C, degree=degree, coef0=coef0)
    elif kernel == 'rbf':
        model = sklearn.svm.SVC(kernel=kernel, C=C, gamma=10 ** logGamma)
    else:
        raise ArgumentError("Unknown kernel function: %s" % kernel)
    model.fit(x_train, y_train)
    return model


def svm_tuned_auroc(x_train, y_train, x_test, y_test, kernel='linear', C=0, logGamma=0, degree=0, coef0=0):
    model = train_model(x_train, y_train, kernel, C, logGamma, degree, coef0)
    decision_values = model.decision_function(x_test)
    return optunity.metrics.roc_auc(y_test, decision_values)

svm_tuned_auroc = cv_decorator(svm_tuned_auroc)

optimal_svm_pars, info, _ = optunity.maximize_structured(svm_tuned_auroc, space, num_evals=150)
print("Optimal parameters" + str(optimal_svm_pars))
print("AUROC of tuned SVM: %1.3f" % info.optimum)

1 个答案:

答案 0 :(得分:0)

首先需要将pivot元素与低索引元素交换,然后执行其他操作。检查我在以下代码块中添加的代码。

static int part(int low, int high, int [] S) {
    int i;
    int j;
    int randspot;
    int pivotitem;
    int pivotpoint;
    Random random = new Random(456);
    randspot = random.nextInt(high-low +1)+low;
    System.out.println( " p = " + randspot );
    System.out.println( " value = " + S[randspot] );
    pivotitem = S[randspot];
    //---------------Start code block
    int temp1 = S[randspot];
    S[randspot] = S[low];
    S[low] = temp1;
    //---------------End code block
    j = low;
    for (i = low + 1; i <= high; i++)
        if (S[i] < pivotitem){
            j++;
            int temp = S[j];
            S[j] = S[i]; //exchanges S[i] and S[j]
            S[i] = temp;
        }
    pivotpoint = j;
    int temp = S[low];
    S[low] = S[pivotpoint];
    S[pivotpoint] = temp;
    return pivotpoint;
}