Example #1
0
def deserialize_ridge_regressor(model_dict):
    model = Ridge(model_dict["params"])
    model.coef_ = np.array(model_dict["coef_"])
    if "n_iter_" in model_dict:
        model.n_iter_ = np.array(model_dict["n_iter_"])
    if isinstance(model_dict["intercept_"], list):
        model.intercept_ = np.array(model_dict["intercept_"])
    else:
        model.intercept_ = float(model_dict["intercept_"])

    return model
Example #2
0
def get_Ridge(train_df, train_y, test_df):
    #train_df = train_df.applymap(lambda x:np.log(x+1))
    #test_df = test_df.applymap(lambda x:np.log(x+1))
    clf = Ridge(alpha=0.001)
    clf.fit(train_df, train_y)
    a = clf.coef_
    for i in range(len(a)):
        if a[i] < 0:
            a[i] = 0
    clf.coef_ = a
    #print (clf.coef_)
    return clf.predict(train_df), clf.predict(test_df)
def fit_learner(x, y, gamma, ridge=None):
    """
    Returns an trained model that works exactly the same as Ridge,
    but fit optimally
    """
    if ridge is None:
        ridge = Ridge()
    x_new, y_new = to_standard_form(x, y, gamma)
    ta_est_standard = ridge.fit(x_new, y_new).coef_
    ta_est = to_general_form(ta_est_standard, x, y, gamma)
    ridge.coef_ = ta_est
    return ridge
def deserialize_ridge_regressor(model_dict):
    model = Ridge(model_dict['params'])

    model.coef_ = np.array(model_dict['coef_'])

    if 'n_iter_' in model_dict:
        model.n_iter_ = np.array(model_dict['n_iter_'])

    if isinstance(model_dict['intercept_'], list):
        model.intercept_ = np.array(model_dict['intercept_'])
    else:
        model.intercept_ = float(model_dict['intercept_'])

    return model
Example #5
0
def sensitivity_ridge(data, runs):
    output_var = 'INCWAGE'

    lamb = 0.01
    train, test = train_test_split(data, test_size=0.2)
    # global_delta = (12 * (1./(lamb**0.5)) + 8) / (len(train) * lamb)
    global_delta = ((1. / (lamb**0.5)) + 1) * (5. / (len(train) * lamb))

    xtrain, ytrain = np.array(
        train.loc[:, train.columns != output_var]), np.array(train[output_var])
    xtest, ytest = np.array(test.loc[:, test.columns != output_var]), np.array(
        test[output_var])

    xtrain, xtest = normalize(xtrain), normalize(xtest)
    model = Ridge(alpha=2 * lamb)
    model.fit(xtrain, ytrain)
    orig_coef = model.coef_
    non_private_accuracy = (sum(
        (model.predict(xtest) - ytest)**2) / len(test))**0.5
    print("Ridge fit: ", non_private_accuracy)

    confidence = []
    utility_global = defaultdict(list)
    epsilons = np.linspace(0.1, 0.99, 10)
    for e in epsilons:
        confidence.append(bound(e, 1.0, 1))
        for _ in range(runs):
            model.coef_ = orig_coef + np.random.laplace(
                0, (global_delta / e), orig_coef.shape)
            utility_global[e].append((sum(
                (model.predict(xtest) - ytest)**2) / len(xtest))**0.5)
    result = {
        "confidence": confidence,
        "utility_global": utility_global,
        "utility": non_private_accuracy
    }
    pickle.dump(result, open("new_ridge_utility.pickle", "wb"))
def pca_regress_pipeline_log(features, grades, groups, n_components=0.9, solver='full', whitening=True, standard=False,
                             seed=42, mod_coefs=True, alpha=0.1, grade_name='', savepath=None):

    feature_names = ['Center +', 'Center -', 'Large U-1', 'Large U-2', 'Large U-3', 'Large U-4', 'Large U-5', 'Large U-6',
                     'Large U-7', 'Large N-U', 'Small U-1', 'Small U-2', 'Small U-3', 'Small U-4', 'Small U-5', 'Small U-6', 'Small U-7',
                     'Small N-U', 'Radial U-0', 'Radial U-1', 'Radial U-2', 'Radial U-3', 'Radial U-4', 'Radial U-5', 'Radial U-6',
                     'Radial U-7', 'Radial U-8', 'Radial N-U']
    grades_log = grades

    # Fit PCA to full data
    pca = PCA(n_components=n_components, svd_solver=solver, whiten=whitening, random_state=seed)
    pca.fit(features)

    # Leave one out split
    logo = LeaveOneGroupOut()
    logo.get_n_splits(features, grades_log, groups)
    logo.get_n_splits(groups=groups)  # 'groups' is always required
    all_shap_values, all_shap_values_lin = [], []
    for train_idx, test_idx in logo.split(features, grades_log, groups):
        # Indices
        x_train, x_test = features[train_idx], features[test_idx]
        y_train, y_test = grades_log[train_idx], grades_log[test_idx]

        # Normalize with mean and std
        if standard:
            x_test -= x_train.mean(0)
            x_train -= x_train.mean(0)

        # Logistic regression
        model = LogisticRegression(solver='newton-cg', max_iter=1000, random_state=seed, fit_intercept=False)
        model.fit(pca.transform(x_train), y_train > 1)

        model_lin = Ridge(alpha=alpha, normalize=True, random_state=seed, fit_intercept=True)
        model_lin.fit(pca.transform(x_train), y_train)

        # Predicted score (for logistic regression)
        p = model.predict_proba(pca.transform(x_test))
        p_lin = model_lin.predict(pca.transform(x_test))

        # Merge PCA into the linear model
        if mod_coefs:
            coef = (pca.components_.T / pca.singular_values_) @ model.coef_.T * np.sqrt(pca.n_samples_ - 1)
            coef_lin = (pca.components_.T / pca.singular_values_) @ model_lin.coef_.T * np.sqrt(pca.n_samples_ - 1)

            # Update models
            model.coef_ = coef.T
            model_lin.coef_ = coef_lin.T

            p2_lin = model_lin.predict(x_test)
            p2 = model.predict_proba(x_test)

            # Inference
            p_inf = (x_test @ coef).squeeze()
            p_inf = (1 + np.exp(-p_inf)) ** -1

            eps = 1.0e-10
            assert np.sum(np.abs(p - p2)) < eps, 'LOGReg results are not equal'
            assert np.sum(np.abs(p_inf - p[:, 1])) < eps, 'LOGReg results are not equal'
            assert np.sum(np.abs(p_lin - p2_lin)) < eps, 'LINReg results are not equal'
        else:  # Otherwise run PCA
            x_train = pca.transform(x_train)
            x_test = pca.transform(x_test)

        # Interpretability

        # Logistic regression
        explainer = shap.LinearExplainer(model, x_train, feature_dependence='correlation', nsamples=x_train.shape[0])
        shap_values = explainer.shap_values(x_test)

        # Linear regression
        explainer_lin = shap.LinearExplainer(model_lin, x_train, feature_dependence='correlation',
                                             nsamples=x_train.shape[0])
        shap_values_lin = explainer_lin.shap_values(x_test)

        # Append prediction
        all_shap_values.append(shap_values)
        all_shap_values_lin.append(shap_values_lin)

    # Combine shap values and plot the summary
    all_shap_values = np.vstack(all_shap_values)
    all_shap_values_lin = np.vstack(all_shap_values_lin)

    # Inverse PCA for the model without PCA
    if not mod_coefs:
        all_shap_values = pca.inverse_transform(all_shap_values)
        all_shap_values_lin = pca.inverse_transform(all_shap_values_lin)

    # Force plot
    # shap.force_plot(explainer.expected_value, all_shap_values, features)
    # plt.show()

    # Summary plots
    shap.summary_plot(all_shap_values, features, show=False, feature_names=feature_names)
    # plt.title(f'Logistic Regression ({grade_name})')
    if savepath is not None:
        plt.savefig(f'{savepath}{grade_name}_logistic_cov.png', transparent=False, bbox_inches='tight')
        plt.show()
    else:
        plt.show()
    shap.summary_plot(all_shap_values_lin, features, show=False, feature_names=feature_names)
    # plt.title(f'Linear Ridge Regression ({grade_name})')
    if savepath is not None:
        plt.savefig(f'{savepath}{grade_name}_linear_cov.png', transparent=False, bbox_inches='tight')
        plt.show()
    else:
        plt.show()
    model.addLayer(DenseLayer(95, activation()))
model.addLayer(DenseLayer(7, LinearActivation()))
model.initialize(QuadraticCost())
ridge = Ridge(alpha=0.1)
# load model parameters
path1 = './model/my_model_W_{}.dat'
path2 = './model/my_model_b_{}.dat'
path3 = './model/my_model_ridge_W.dat'
path4 = './model/my_model_ridge_intercept.dat'
model_W, model_b, model_ridge_W, model_ridge_intercept = load_model(
    4, path1, path2, path3, path4)
# update the model
for i in range(len(model.layers)):
    model.layers[i].W = model_W[i]
    model.layers[i].b = model_b[i]
ridge.coef_ = model_ridge_W
ridge.intercept_ = model_ridge_intercept[0]

# get the data point
inputlist = ball_tracking()

# predict the configuration
tar_position_in_list = list(ridge.predict(model.predict(inputlist)))

for i in range(len(joint_names)):
    tar_position[joint_names[i]] = tar_position_in_list[i]

while True:
    cur_pos = limb.joint_angles()
    for key, value in tar_position.iteritems():
        error_new[key] = tar_position[key] - cur_pos[key]