def main():
    y_train, x_train = svm_read_problem('../Data/hw5/satimage.scale')

    y_train = [temp == 3.0 for temp in y_train]

    model = svm_train(y_train, x_train, '-t 0 -c 10')

    sv_coef = np.array(model.get_sv_coef())
    sv_indices = [index - 1 for index in model.get_sv_indices()]

    _, x_support = svm_read_problem('../Data/hw5/satimage.scale',
                                    return_scipy=True)

    x_support = x_support.toarray()[sv_indices]

    print(f'||w|| is {np.linalg.norm(x_support.T @ sv_coef)}')
Esempio n. 2
0
def LoadLibSVMData(path: str,
                   features: int,
                   x_dtype: np.dtype = np.float,
                   y_dtype: np.dtype = np.int) -> np.ndarray:
    """
        加载 LibSVM 数据集的数据,转成numpy.ndarray类型

    @Param:
    -------
    path: 文件路径\n
    features: 数据维度

    @Return:
    --------
    data_x: [numpy.ndarray](n_samples, n_features)
    data_y: [numpy.ndarray](n_samples,)
    """
    libsvm_data = None
    try:
        libsvm_data = svm_read_problem(path)
    except IOError:
        raise ("文件路径出错")
    libsvm_y, libsvm_x = libsvm_data
    data_y = np.array(libsvm_y, dtype=y_dtype)
    data_x = np.zeros((len(libsvm_x), features), dtype=x_dtype)
    for line, map_ in zip(data_x, libsvm_x):
        for pair in map_.items():
            line[pair[0] - 1] = pair[1]
    return (data_x, data_y)
Esempio n. 3
0
def test_v_svr(prompt_idx, gamma=None):
    args = _parse_args()
    dataset = AESDataset(args.dataset_dir,
                         prompt_num=PROMPT_NUM,
                         is_cross_dataset=False)
    x_train_list, y_train_list = dataset.get_train()
    x_dev_list, y_dev_list = dataset.get_dev()
    x_test_list, _, _ = dataset.get_test()
    train_len, dev_len, test_len = len(x_train_list[prompt_idx-1]), len(x_dev_list[prompt_idx-1]),\
                                   len(x_test_list[prompt_idx-1])

    y, x = svm_read_problem(SVM_SCALE_DIR + '/prompt@' + str(prompt_idx) +
                            '-scale.txt')
    x_train, y_train = x[:train_len], y[:train_len]
    x_dev, y_dev = x[train_len:train_len + dev_len], y[train_len:train_len +
                                                       dev_len]
    x_test = x[train_len + dev_len:]

    if gamma:
        param = f'-s 4 -t 2 -c 1000 -n 0.1 -g {gamma}'
    else:
        param = f'-s 4 -t 2 -c 1000 -n 0.1'
    svm_model = svm_train(y_train + y_dev, x_train + x_dev, param)
    p_label, p_acc, p_val = svm_predict(np.zeros(shape=len(x_test)), x_test,
                                        svm_model)
    p_label = np.round(p_label)

    dev_label, dev_acc, dev_val = svm_predict(y_dev, x_dev, svm_model)
    dev_kappa = kappa(y_true=y_dev, y_pred=dev_label, weights='quadratic')
    print(f'Dev kappa: {dev_kappa}')
    return dev_kappa, p_label
Esempio n. 4
0
def main():
    y_train, x_train = svm_read_problem('../Data/hw5/satimage.scale')

    y_train = [temp == 6.0 for temp in y_train]

    y_test, x_test = svm_read_problem('../Data/hw5/satimage.scale.t')

    y_test = [temp == 6.0 for temp in y_test]

    for c in [0.01, 0.1, 1, 10, 100]:

        print(f'---------')
        print(f'C {c}')

        model = svm_train(y_train, x_train, f'-t 2 -g 10 -c {c}')

        p_label, p_acc, p_val = svm_predict(y_test, x_test, model)

        print(f'Eout = {p_acc[1]}')
Esempio n. 5
0
def main():
    y_train, x_train = svm_read_problem('../Data/hw5/satimage.scale')

    for i in range(5):

        print(f'Label {i+1}')

        temp_y_train = [temp == i+1 for temp in y_train]

        model = svm_train(temp_y_train, x_train, '-t 1 -d 2 -g 1 -r 1 -c 10')

        p_label, p_acc, p_val = svm_predict(temp_y_train, x_train, model)
Esempio n. 6
0
def main():
    y_train, x_train = svm_read_problem('../Data/hw5/satimage.scale',
                                        return_scipy=True)

    y_train = y_train == 6.0

    gammas = {g: 0 for g in [0.1, 1, 10, 100, 1000]}

    with ProcessPoolExecutor() as executor:
        for _, idx in tqdm(executor.map(one_iter, [y_train] * ITER,
                                        [x_train] * ITER),
                           total=ITER):
            gammas[idx] += 1

    print(gammas)
Esempio n. 7
0
def heart_scale():
    return svm_read_problem(os.path.join(TEST_RESOURCES_PATH, 'heart_scale'))
def get_data(path: str):
    root_dir = os.path.dirname(os.path.abspath(__file__))
    data = commonutil.svm_read_problem(os.path.join(root_dir, path), True)
    return np.insert(data[1].A, 0, np.arange(1,
                                             len(data[0]) + 1),
                     axis=1), data[0]