Python sklearn.neighbors.KNeighborsRegressor() Examples

The following are code examples for showing how to use sklearn.neighbors.KNeighborsRegressor(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: mlens   Author: flennerhag   File: friedman_scores.py    MIT License 6 votes vote down vote up
def build_ensemble(**kwargs):
    """Generate ensemble."""

    ens = SuperLearner(**kwargs)
    prep = {'Standard Scaling': [StandardScaler()],
            'Min Max Scaling': [MinMaxScaler()],
            'No Preprocessing': []}

    est = {'Standard Scaling':
               [ElasticNet(), Lasso(), KNeighborsRegressor()],
           'Min Max Scaling':
               [SVR()],
           'No Preprocessing':
               [RandomForestRegressor(random_state=SEED),
                GradientBoostingRegressor()]}

    ens.add(est, prep)

    ens.add(GradientBoostingRegressor(), meta=True)

    return ens 
Example 2
Project: nyu_ml_lectures   Author: amueller   File: plot_kneighbors_regularization.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def plot_kneighbors_regularization():
    rnd = np.random.RandomState(42)
    x = np.linspace(-3, 3, 100)
    y_no_noise = np.sin(4 * x) + x
    y = y_no_noise + rnd.normal(size=len(x))
    X = x[:, np.newaxis]
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    x_test = np.linspace(-3, 3, 1000)

    for n_neighbors, ax in zip([2, 5, 20], axes.ravel()):
        kneighbor_regression = KNeighborsRegressor(n_neighbors=n_neighbors)
        kneighbor_regression.fit(X, y)
        ax.plot(x, y_no_noise, label="true function")
        ax.plot(x, y, "o", label="data")
        ax.plot(x_test, kneighbor_regression.predict(x_test[:, np.newaxis]),
                label="prediction")
        ax.legend(loc="best")
        ax.set_title("n_neighbors = %d" % n_neighbors) 
Example 3
Project: Localization_via_WiFi_Fingerprinting   Author: ryanmclark   File: models.py    MIT License 6 votes vote down vote up
def load_KNN():
    '''
    Loads K-Nearest Neighbor and gives a name for the output files.
    
    Parameters : None
    
    Returns    : model_name : (str) Name of the model for output file.
                       clf  : (Classifier) Building and Floor Classifier
                       regr : (REgressor) Longitude and Latitude Regressor
    '''
    model_name = "K-Nearest Neighbors"
    clf = KNeighborsClassifier(n_neighbors=1, algorithm='kd_tree',
                                leaf_size=50, p=1)
    regr = KNeighborsRegressor(n_neighbors=1, algorithm='kd_tree',
                                leaf_size=50, p=1)
    
    return model_name, clf, regr 
Example 4
Project: RPGOne   Author: RTHMaK   File: plot_kneighbors_regularization.py    Apache License 2.0 6 votes vote down vote up
def plot_kneighbors_regularization():
    rnd = np.random.RandomState(42)
    x = np.linspace(-3, 3, 100)
    y_no_noise = np.sin(4 * x) + x
    y = y_no_noise + rnd.normal(size=len(x))
    X = x[:, np.newaxis]
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    x_test = np.linspace(-3, 3, 1000)

    for n_neighbors, ax in zip([2, 5, 20], axes.ravel()):
        kneighbor_regression = KNeighborsRegressor(n_neighbors=n_neighbors)
        kneighbor_regression.fit(X, y)
        ax.plot(x, y_no_noise, label="true function")
        ax.plot(x, y, "o", label="data")
        ax.plot(x_test, kneighbor_regression.predict(x_test[:, np.newaxis]),
                label="prediction")
        ax.legend()
        ax.set_title("n_neighbors = %d" % n_neighbors) 
Example 5
Project: nyoka   Author: nyoka-pmml   File: testScoreWithAdapaSklearn.py    Apache License 2.0 6 votes vote down vote up
def test_33_knn_regressor(self):
        print("\ntest 33 (knn regressor without preprocessing)\n")
        X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression()

        model = KNeighborsRegressor()
        pipeline_obj = Pipeline([
            ("model", model)
        ])
        pipeline_obj.fit(X,y)
        file_name = 'test33sklearn.pmml'
        
        skl_to_pmml(pipeline_obj, features, target, file_name)
        model_name  = self.adapa_utility.upload_to_zserver(file_name)
        predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file)
        model_pred = pipeline_obj.predict(X_test)
        self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) 
Example 6
Project: nyoka   Author: nyoka-pmml   File: test_skl_to_pmml_UnitTest.py    Apache License 2.0 6 votes vote down vote up
def test_sklearn_15(self):
        df = pd.read_csv('nyoka/tests/auto-mpg.csv')
        X = df.drop(['mpg','car name'],axis=1)
        y = df['mpg']

        features = X.columns
        target = 'mpg'

        pipeline_obj = Pipeline([
            ('model',KNeighborsRegressor())
        ])

        pipeline_obj.fit(X,y)

        skl_to_pmml(pipeline_obj,features,target,"knnr.pmml")

        self.assertEqual(os.path.isfile("knnr.pmml"),True) 
Example 7
Project: linear_neuron   Author: uglyboxer   File: test_bagging.py    MIT License 6 votes vote down vote up
def test_regression():
    # Check regression for various parameter settings.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data[:50],
                                                        boston.target[:50],
                                                        random_state=rng)
    grid = ParameterGrid({"max_samples": [0.5, 1.0],
                          "max_features": [0.5, 1.0],
                          "bootstrap": [True, False],
                          "bootstrap_features": [True, False]})

    for base_estimator in [None,
                           DummyRegressor(),
                           DecisionTreeRegressor(),
                           KNeighborsRegressor(),
                           SVR()]:
        for params in grid:
            BaggingRegressor(base_estimator=base_estimator,
                             random_state=rng,
                             **params).fit(X_train, y_train).predict(X_test) 
Example 8
Project: linear_neuron   Author: uglyboxer   File: test_bagging.py    MIT License 6 votes vote down vote up
def test_single_estimator():
    # Check singleton ensembles.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data,
                                                        boston.target,
                                                        random_state=rng)

    clf1 = BaggingRegressor(base_estimator=KNeighborsRegressor(),
                            n_estimators=1,
                            bootstrap=False,
                            bootstrap_features=False,
                            random_state=rng).fit(X_train, y_train)

    clf2 = KNeighborsRegressor().fit(X_train, y_train)

    assert_array_equal(clf1.predict(X_test), clf2.predict(X_test)) 
Example 9
Project: mmltoolkit   Author: delton137   File: test_everything.py    MIT License 6 votes vote down vote up
def make_models():
    ''' return dictionary with models and their corresponding parameter grids for hyperparameter optimizaiton '''

    model_dict = {
            'KRR'   :{ 'model' : KernelRidge(), 'param_grid' : {"alpha": np.logspace(-15, 2, 200), "gamma": np.logspace(-14, -1, 100), "kernel" : ['rbf']}},
            #'SVR'   :{ 'model' : SVR(), 'param_grid' : {"C": np.logspace(-1, 4, 20), "epsilon": np.logspace(-2, 2, 20)}},
            #'Ridge' :{ 'model' : Ridge(), 'param_grid' : {"alpha": np.logspace(-6, 6, 150)}},
            #'Lasso' :{ 'model' : Lasso(max_iter = 20000), 'param_grid' : {"alpha": np.logspace(-2, 6, 100)}},
            #'BR'    :{ 'model' : BayesianRidge(), 'param_grid' : {"alpha_1": np.logspace(-13,-5,10),"alpha_2": np.logspace(-9,-3,10), "lambda_1": np.logspace(-10,-5,10),"lambda_2": np.logspace(-11,-4,10)}},
            #'GBoost':{ 'model' : GradientBoostingRegressor(), 'param_grid' : {"n_estimators": np.linspace(5, 350, 100).astype('int')}},
            #'RF'     :{ 'model' : RandomForestRegressor(), 'param_grid' : {"n_estimators": np.linspace(5, 100, 50).astype('int')}},
            #'kNN'   :{ 'model' : KNeighborsRegressor(), 'param_grid' : {"n_neighbors": np.linspace(2,20,18).astype('int')}},
            #'mean'  :{ 'model' : DummyRegressor(strategy='mean'), 'param_grid' : {} },
            }

    return model_dict

#---------------------------------------------------------------------------- 
Example 10
Project: mmltoolkit   Author: delton137   File: test_everything.py    MIT License 6 votes vote down vote up
def make_CV_models(X, y):
    '''performs grid searches to find all the best models for dataset X, y'''

    model_dict = {
            'KRR'    : grid_search(X, y, KernelRidge(), param_grid={"alpha": np.logspace(-10, 2, 300), "gamma": np.logspace(-10, -1, 100), "kernel" : ['rbf']}),
            'SVR'   : grid_search(X, y, SVR(), param_grid={"C": np.logspace(-1, 4, 20), "epsilon": np.logspace(-2, 2, 20)}),
            'Ridge' : grid_search(X, y, Ridge(), param_grid={"alpha": np.logspace(-6, 6, 150)} ),
            'Lasso' : grid_search(X, y, Lasso(max_iter = 20000), param_grid={"alpha": np.logspace(-2, 6, 100)} ),
            'BR'    : grid_search(X, y, BayesianRidge(), param_grid={"alpha_1": np.logspace(-13,-5,10),"alpha_2": np.logspace(-9,-3,10), "lambda_1": np.logspace(-10,-5,10),"lambda_2": np.logspace(-11,-4,10)}) ,
            'GBoost': grid_search(X, y, GradientBoostingRegressor(), param_grid={"n_estimators": np.linspace(5, 350, 100).astype('int')} ),
            'RF'    : grid_search(X, y, RandomForestRegressor(), param_grid={"n_estimators": np.linspace(5, 100, 50).astype('int')}, ),
            'kNN'   : grid_search(X, y, KNeighborsRegressor(), param_grid={"n_neighbors": np.linspace(2,20,18).astype('int')} ),
            'mean'  : DummyRegressor(strategy='mean'),
            }

    return model_dict


#---------------------------------------------------------------------------- 
Example 11
Project: Weiss   Author: WangWenjun559   File: test_bagging.py    Apache License 2.0 6 votes vote down vote up
def test_single_estimator():
    # Check singleton ensembles.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data,
                                                        boston.target,
                                                        random_state=rng)

    clf1 = BaggingRegressor(base_estimator=KNeighborsRegressor(),
                            n_estimators=1,
                            bootstrap=False,
                            bootstrap_features=False,
                            random_state=rng).fit(X_train, y_train)

    clf2 = KNeighborsRegressor().fit(X_train, y_train)

    assert_array_equal(clf1.predict(X_test), clf2.predict(X_test)) 
Example 12
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_kneighbors_regressor(n_samples=40,
                              n_features=5,
                              n_test_pts=10,
                              n_neighbors=3,
                              random_state=0):
    # Test k-neighbors regression
    rng = np.random.RandomState(random_state)
    X = 2 * rng.rand(n_samples, n_features) - 1
    y = np.sqrt((X ** 2).sum(1))
    y /= y.max()

    y_target = y[:n_test_pts]

    weight_func = _weight_func

    for algorithm in ALGORITHMS:
        for weights in ['uniform', 'distance', weight_func]:
            knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
                                                weights=weights,
                                                algorithm=algorithm)
            knn.fit(X, y)
            epsilon = 1E-5 * (2 * rng.rand(1, n_features) - 1)
            y_pred = knn.predict(X[:n_test_pts] + epsilon)
            assert_true(np.all(abs(y_pred - y_target) < 0.3)) 
Example 13
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_KNeighborsRegressor_multioutput_uniform_weight():
    # Test k-neighbors in multi-output regression with uniform weight
    rng = check_random_state(0)
    n_features = 5
    n_samples = 40
    n_output = 4

    X = rng.rand(n_samples, n_features)
    y = rng.rand(n_samples, n_output)

    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    for algorithm, weights in product(ALGORITHMS, [None, 'uniform']):
        knn = neighbors.KNeighborsRegressor(weights=weights,
                                            algorithm=algorithm)
        knn.fit(X_train, y_train)

        neigh_idx = knn.kneighbors(X_test, return_distance=False)
        y_pred_idx = np.array([np.mean(y_train[idx], axis=0)
                               for idx in neigh_idx])

        y_pred = knn.predict(X_test)

        assert_equal(y_pred.shape, y_test.shape)
        assert_equal(y_pred_idx.shape, y_test.shape)
        assert_array_almost_equal(y_pred, y_pred_idx) 
Example 14
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_kneighbors_regressor_sparse(n_samples=40,
                                     n_features=5,
                                     n_test_pts=10,
                                     n_neighbors=5,
                                     random_state=0):
    # Test radius-based regression on sparse matrices
    # Like the above, but with various types of sparse matrices
    rng = np.random.RandomState(random_state)
    X = 2 * rng.rand(n_samples, n_features) - 1
    y = ((X ** 2).sum(axis=1) < .25).astype(np.int)

    for sparsemat in SPARSE_TYPES:
        knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
                                            algorithm='auto')
        knn.fit(sparsemat(X), y)
        for sparsev in SPARSE_OR_DENSE:
            X2 = sparsev(X)
            assert_true(np.mean(knn.predict(X2).round() == y) > 0.95) 
Example 15
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_neighbors_iris():
    # Sanity checks on the iris dataset
    # Puts three points of each label in the plane and performs a
    # nearest neighbor query on points near the decision boundary.

    for algorithm in ALGORITHMS:
        clf = neighbors.KNeighborsClassifier(n_neighbors=1,
                                             algorithm=algorithm)
        clf.fit(iris.data, iris.target)
        assert_array_equal(clf.predict(iris.data), iris.target)

        clf.set_params(n_neighbors=9, algorithm=algorithm)
        clf.fit(iris.data, iris.target)
        assert_true(np.mean(clf.predict(iris.data) == iris.target) > 0.95)

        rgs = neighbors.KNeighborsRegressor(n_neighbors=5, algorithm=algorithm)
        rgs.fit(iris.data, iris.target)
        assert_true(np.mean(rgs.predict(iris.data).round() == iris.target)
                    > 0.95) 
Example 16
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_bagging.py    Apache License 2.0 6 votes vote down vote up
def test_regression():
    # Check regression for various parameter settings.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data[:50],
                                                        boston.target[:50],
                                                        random_state=rng)
    grid = ParameterGrid({"max_samples": [0.5, 1.0],
                          "max_features": [0.5, 1.0],
                          "bootstrap": [True, False],
                          "bootstrap_features": [True, False]})

    for base_estimator in [None,
                           DummyRegressor(),
                           DecisionTreeRegressor(),
                           KNeighborsRegressor(),
                           SVR()]:
        for params in grid:
            BaggingRegressor(base_estimator=base_estimator,
                             random_state=rng,
                             **params).fit(X_train, y_train).predict(X_test) 
Example 17
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_bagging.py    Apache License 2.0 6 votes vote down vote up
def test_single_estimator():
    # Check singleton ensembles.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data,
                                                        boston.target,
                                                        random_state=rng)

    clf1 = BaggingRegressor(base_estimator=KNeighborsRegressor(),
                            n_estimators=1,
                            bootstrap=False,
                            bootstrap_features=False,
                            random_state=rng).fit(X_train, y_train)

    clf2 = KNeighborsRegressor().fit(X_train, y_train)

    assert_array_equal(clf1.predict(X_test), clf2.predict(X_test)) 
Example 18
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_KNeighborsRegressor_multioutput_uniform_weight():
    # Test k-neighbors in multi-output regression with uniform weight
    rng = check_random_state(0)
    n_features = 5
    n_samples = 40
    n_output = 4

    X = rng.rand(n_samples, n_features)
    y = rng.rand(n_samples, n_output)

    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    for algorithm, weights in product(ALGORITHMS, [None, 'uniform']):
        knn = neighbors.KNeighborsRegressor(weights=weights,
                                            algorithm=algorithm)
        knn.fit(X_train, y_train)

        neigh_idx = knn.kneighbors(X_test, return_distance=False)
        y_pred_idx = np.array([np.mean(y_train[idx], axis=0)
                               for idx in neigh_idx])

        y_pred = knn.predict(X_test)

        assert_equal(y_pred.shape, y_test.shape)
        assert_equal(y_pred_idx.shape, y_test.shape)
        assert_array_almost_equal(y_pred, y_pred_idx) 
Example 19
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_kneighbors_regressor_multioutput(n_samples=40,
                                          n_features=5,
                                          n_test_pts=10,
                                          n_neighbors=3,
                                          random_state=0):
    # Test k-neighbors in multi-output regression
    rng = np.random.RandomState(random_state)
    X = 2 * rng.rand(n_samples, n_features) - 1
    y = np.sqrt((X ** 2).sum(1))
    y /= y.max()
    y = np.vstack([y, y]).T

    y_target = y[:n_test_pts]

    weights = ['uniform', 'distance', _weight_func]
    for algorithm, weights in product(ALGORITHMS, weights):
        knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
                                            weights=weights,
                                            algorithm=algorithm)
        knn.fit(X, y)
        epsilon = 1E-5 * (2 * rng.rand(1, n_features) - 1)
        y_pred = knn.predict(X[:n_test_pts] + epsilon)
        assert_equal(y_pred.shape, y_target.shape)

        assert_true(np.all(np.abs(y_pred - y_target) < 0.3)) 
Example 20
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_kneighbors_regressor_sparse(n_samples=40,
                                     n_features=5,
                                     n_test_pts=10,
                                     n_neighbors=5,
                                     random_state=0):
    # Test radius-based regression on sparse matrices
    # Like the above, but with various types of sparse matrices
    rng = np.random.RandomState(random_state)
    X = 2 * rng.rand(n_samples, n_features) - 1
    y = ((X ** 2).sum(axis=1) < .25).astype(np.int)

    for sparsemat in SPARSE_TYPES:
        knn = neighbors.KNeighborsRegressor(n_neighbors=n_neighbors,
                                            algorithm='auto')
        knn.fit(sparsemat(X), y)
        for sparsev in SPARSE_OR_DENSE:
            X2 = sparsev(X)
            assert_true(np.mean(knn.predict(X2).round() == y) > 0.95) 
Example 21
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 6 votes vote down vote up
def test_neighbors_iris():
    # Sanity checks on the iris dataset
    # Puts three points of each label in the plane and performs a
    # nearest neighbor query on points near the decision boundary.

    for algorithm in ALGORITHMS:
        clf = neighbors.KNeighborsClassifier(n_neighbors=1,
                                             algorithm=algorithm)
        clf.fit(iris.data, iris.target)
        assert_array_equal(clf.predict(iris.data), iris.target)

        clf.set_params(n_neighbors=9, algorithm=algorithm)
        clf.fit(iris.data, iris.target)
        assert_true(np.mean(clf.predict(iris.data) == iris.target) > 0.95)

        rgs = neighbors.KNeighborsRegressor(n_neighbors=5, algorithm=algorithm)
        rgs.fit(iris.data, iris.target)
        assert_greater(np.mean(rgs.predict(iris.data).round() == iris.target),
                       0.95) 
Example 22
Project: pcay   Author: zpace   File: totalmass.py    MIT License 6 votes vote down vote up
def knn_regr(trn_coords, trn_vals, good_trn, k=8):
    '''
    return a trained estimator for k-nearest-neighbors

    - trn_coords: list containing row-coordinate map and col-coordinate map
    - trn_vals: map of values used for training
    - good_trn: binary map (True signifies good data)
    '''
    II, JJ = trn_coords
    good = good_trn.flatten()
    coo = np.column_stack([II.flatten()[good], JJ.flatten()[good]])
    vals = trn_vals.flatten()[good]

    knn = KNeighborsRegressor(
        n_neighbors=k, weights='uniform', p=2)
    knn.fit(coo, vals)

    return knn 
Example 23
Project: nni   Author: microsoft   File: main.py    MIT License 6 votes vote down vote up
def get_model(PARAMS):
    '''Get model according to parameters'''
    model_dict = {
        'LinearRegression': LinearRegression(),
        'SVR': SVR(),
        'KNeighborsRegressor': KNeighborsRegressor(),
        'DecisionTreeRegressor': DecisionTreeRegressor()
    }
    if not model_dict.get(PARAMS['model_name']):
        LOG.exception('Not supported model!')
        exit(1)

    model = model_dict[PARAMS['model_name']]

    try:
        if PARAMS['model_name'] == 'SVR':
            model.kernel = PARAMS['svr_kernel']
        elif PARAMS['model_name'] == 'KNeighborsRegressor':
            model.weights = PARAMS['knr_weights']
    except Exception as exception:
        LOG.exception(exception)
        raise
    return model 
Example 24
Project: crirl   Author: albertometelli   File: lqg_run.py    MIT License 6 votes vote down vote up
def train(learner, states_actions, gaussian_kernel, k, penalty, feature, knn_penalty=None, plot=False):
    knn = KNeighborsRegressor(n_neighbors=k, weights=gaussian_kernel)
    knn.fit(states_actions, feature.ravel())

    if plot:
        if penalty:
            function = lambda states, actions: .5 * \
                        get_knn_function_for_plot(knn)(states, actions) *+ \
                        .5 * 1. / 2. * get_knn_function_for_plot(knn_penalty, True)(states, actions)
        else:
            function = get_knn_function_for_plot(knn)
        plot_state_action_function(function, '%sknn %s' % (k, '- penalty' if penalty else ''))

    if penalty:
        function = lambda traj: .5 * get_knn_function_for_prediction(knn)(traj) + \
                             .5 * 1. / 2. * get_knn_function_for_prediction(knn_penalty, True)(traj)
    else:
        function = get_knn_function_for_prediction(knn)



    theta, history = learner.optimize(-0.2, reward=function, return_history=True)
    return history 
Example 25
Project: smp_base   Author: x75   File: models_actinf.py    MIT License 6 votes vote down vote up
def __init__(self, conf):
        """smpKNN.__init__

        init
        """
        smpModel.__init__(self, conf)

        # comply
        if not hasattr(self, 'modelsize'):
            self.modelsize = 1000 # self.n_neighbors

        # the scikit base model
        self.fwd = KNeighborsRegressor(n_neighbors = self.n_neighbors)

        # the data store
        self.X_ = []
        self.y_ = []
        
        self.hidden_dist = np.zeros((1, self.n_neighbors))
        self.hidden_dist_sum = np.zeros((1, 1))
        self.hidden_dist_sum_avg = np.zeros((1, 1))
        self.hidden_idx = np.zeros((1, self.n_neighbors))
        # bootstrap the model with prior
        self.bootstrap() 
Example 26
Project: Jtyoui   Author: jtyoui   File: neighbor.py    MIT License 5 votes vote down vote up
def knn_regress_fit(train, label):
    clf = KNeighborsRegressor()
    clf.fit(train, label)
    return clf 
Example 27
Project: mlens   Author: flennerhag   File: friedman_memory.py    MIT License 5 votes vote down vote up
def build_ensemble(**kwargs):
    """Generate ensemble."""

    ens = SuperLearner(**kwargs)

    est = [ElasticNet(copy_X=False),
           Lasso(copy_X=False)]

    ens.add(est)
    ens.add(KNeighborsRegressor())

    return ens 
Example 28
Project: mlens   Author: flennerhag   File: friedman_memory.py    MIT License 5 votes vote down vote up
def knn():
    """Fit KNN."""
    print("Fitting KNN...", end=" ", flush=True)
    time.sleep(SLEEP)
    t0 = time.time()
    knn = KNeighborsRegressor()
    knn.fit(X, y)
    print_time(t0, "Done", end="") 
Example 29
Project: practicalDataAnalysisCookbook   Author: drabastomek   File: regression_knn.py    GNU General Public License v2.0 5 votes vote down vote up
def regression_kNN(x,y):
    '''
        Build the kNN classifier
    '''
    # create the classifier object
    knn = nb.KNeighborsRegressor(n_neighbors=80, 
        algorithm='kd_tree', n_jobs=-1)

    # fit the data
    knn.fit(x,y)

    #return the classifier
    return knn

# the file name of the dataset 
Example 30
Project: driverlessai-recipes   Author: h2oai   File: knearestneighbour.py    Apache License 2.0 5 votes vote down vote up
def fit(self, X, y, sample_weight=None, eval_set=None, sample_weight_eval_set=None, **kwargs):
        X = dt.Frame(X)

        orig_cols = list(X.names)
        feature_model = Ridge(alpha=1., random_state=self.random_state)

        self.params['n_neighbors'] = min(self.params['n_neighbors'], X.shape[0])

        if self.num_classes >= 2:

            model = KNeighborsClassifier(n_neighbors=self.params['n_neighbors'], metric=self.params['metric'],
                                         weights=self.params['weights'], n_jobs=self.params['n_jobs'])
            lb = LabelEncoder()
            lb.fit(self.labels)
            y = lb.transform(y)
        else:
            model = KNeighborsRegressor(n_neighbors=self.params['n_neighbors'], metric=self.params['metric'],
                                        weights=self.params['weights'], n_jobs=self.params['n_jobs'])
        self.means = dict()
        self.standard_scaler = StandardScaler()
        for col in X.names:
            XX = X[:, col]
            self.means[col] = XX.mean1()
            if self.means[col] is None:
                self.means[col] = 0
            XX.replace(None, self.means[col])
            X[:, col] = XX
            assert X[dt.isna(dt.f[col]), col].nrows == 0
        X = X.to_numpy()
        X = self.standard_scaler.fit_transform(X)
        feature_model.fit(X, y)
        model.fit(X, y)
        importances = np.array(abs(feature_model.coef_))

        self.set_model_properties(model=model,
                                  features=orig_cols,
                                  importances=importances.tolist(),  # abs(model.coef_[0])
                                  iterations=0) 
Example 31
Project: Tback   Author: zhy0313   File: model.py    MIT License 5 votes vote down vote up
def get_classifier(self, X, Y):
        """ 构建线性回归模型
        :param X: 训练数据
        :param Y: 训练数据结果
        :return: 模型
        """

        clf = KNeighborsRegressor(weights='uniform')
        clf.fit(X, Y)
        return clf 
Example 32
Project: nyoka   Author: nyoka-pmml   File: _validateSchema.py    Apache License 2.0 5 votes vote down vote up
def test_validate_sklearn_knn_models_regression(self):
        model = KNeighborsRegressor()
        pipe = Pipeline([
            ('model',model)
        ])
        pipe.fit(self.X_reg, self.y_reg)
        file_name = 'knn_model_regression.pmml'
        skl_to_pmml(pipe, self.features_reg, 'target',file_name)
        self.assertEqual(self.schema.is_valid(file_name), True) 
Example 33
Project: actinf   Author: x75   File: actinf_models.py    MIT License 5 votes vote down vote up
def __init__(self, idim = 1, odim = 1):
        self.fwd = KNeighborsRegressor(n_neighbors=5)
        ActInfModel.__init__(self, idim, odim)

        self.X_ = []
        self.y_ = []

        self.bootstrap() 
Example 34
Project: numpy-ml   Author: ddbourgin   File: tests.py    GNU General Public License v3.0 5 votes vote down vote up
def test_knn_regression():
    while True:
        N = np.random.randint(2, 100)
        M = np.random.randint(2, 100)
        k = np.random.randint(1, N)
        ls = np.min([np.random.randint(1, 10), N - 1])
        weights = np.random.choice(["uniform", "distance"])

        X = np.random.rand(N, M)
        X_test = np.random.rand(N, M)
        y = np.random.rand(N)

        knn = KNN(
            k=k, leaf_size=ls, metric=euclidean, classifier=False, weights=weights
        )
        knn.fit(X, y)
        preds = knn.predict(X_test)

        gold = KNeighborsRegressor(
            p=2,
            leaf_size=ls,
            n_neighbors=k,
            weights=weights,
            metric="minkowski",
            algorithm="ball_tree",
        )
        gold.fit(X, y)
        gold_preds = gold.predict(X_test)

        for mine, theirs in zip(preds, gold_preds):
            np.testing.assert_almost_equal(mine, theirs)
        print("PASSED") 
Example 35
Project: drifter_ml   Author: EricSchles   File: structural_tests.py    MIT License 5 votes vote down vote up
def reg_supervised_clustering(self, data):
        k_measures = []
        X = data[self.column_names]
        y = data[self.target_name]
        for k in range(2, 12):
            knn = neighbors.KNeighborsRegressor(n_neighbors=k)
            knn.fit(X, y)
            y_pred = knn.predict(X)
            k_measures.append((k, metrics.mean_squared_error(y, y_pred)))
        sorted_k_measures = sorted(k_measures, key=lambda t:t[1])
        lowest_mse = sorted_k_measures[0]
        best_k = lowest_mse[0]
        return best_k 
Example 36
Project: drifter_ml   Author: EricSchles   File: prototype_test_framework.py    MIT License 5 votes vote down vote up
def reg_clustering(self, data, columns, target):
        k_measures = []
        for k in range(2, 12):
            knn = neighbors.KNeighborsRegressor(n_neighbors=k)
            knn.fit(self.X, self.y)
            y_pred = knn.predict(self.X)
            k_measures.append((k, metrics.mean_squared_error(self.y, y_pred)))
        sorted_k_measures = sorted(k_measures, key=lambda t:t[1])
        lowest_mse = sorted_k_measures[0]
        best_k = lowest_mse[0]
        return best_k 
Example 37
Project: drifter_ml   Author: EricSchles   File: prototype_test_framework.py    MIT License 5 votes vote down vote up
def cls_clustering(self):
        k_measures = []
        for k in range(2, 12):
            knn = neighbors.KNeighborsRegressor(n_neighbors=k)
            knn.fit(self.X, self.y)
            y_pred = knn.predict(self.X)
            k_measures.append((k, metrics.mean_squared_error(self.y, y_pred)))
        sorted_k_measures = sorted(k_measures, key=lambda t:t[1])
        lowest_mse = sorted_k_measures[0]
        best_k = lowest_mse[0]
        return best_k 
Example 38
Project: Imputer.py   Author: bwanglzu   File: imputer.py    MIT License 5 votes vote down vote up
def _fit(self, X, column, k=10, is_categorical=False):
        """Fit a knn classifier for missing column.

        - Args:
                X(numpy.ndarray): input data
                column(int): column id to be imputed
                k(int): number of nearest neighbors, default 10
                is_categorical(boolean): is continuous or categorical feature
        - Returns:
                clf: trained k nearest neighbour classifier
        """
        clf = None
        if not is_categorical:
            clf = neighbors.KNeighborsRegressor(n_neighbors=k)
        else:
            clf = neighbors.KNeighborsClassifier(n_neighbors=k)
        # use column not null to train the kNN classifier
        missing_idxes = np.where(pd.isnull(X[:, column]))[0]
        if len(missing_idxes) == 0:
            return None
        X_copy = np.delete(X, missing_idxes, 0)
        X_train = np.delete(X_copy, column, 1)
        # if other columns still have missing values fill with mean
        col_mean = None
        if not is_categorical:
            col_mean = np.nanmean(X, 0)
        else:
            col_mean = np.nanmedian(X, 0)
        for col_id in range(0, len(col_mean) - 1):
            col_missing_idxes = np.where(np.isnan(X_train[:, col_id]))[0]
            if len(col_missing_idxes) == 0:
                continue
            else:
                X_train[col_missing_idxes, col_id] = col_mean[col_id]
        y_train = X_copy[:, column]
        # fit classifier
        clf.fit(X_train, y_train)
        return clf 
Example 39
Project: Weiss   Author: WangWenjun559   File: test_bagging.py    Apache License 2.0 5 votes vote down vote up
def test_regression():
    # Check regression for various parameter settings.
    rng = check_random_state(0)
    X_train, X_test, y_train, y_test = train_test_split(boston.data[:50],
                                                        boston.target[:50],
                                                        random_state=rng)
    grid = ParameterGrid({"max_samples": [0.5, 1.0],
                          "max_features": [0.5, 1.0],
                          "bootstrap": [True, False],
                          "bootstrap_features": [True, False]})

    for base_estimator in [None,
                           DummyRegressor(),
                           DecisionTreeRegressor(),
                           KNeighborsRegressor(),
                           SVR()]:
        for params in grid:
            BaggingRegressor(base_estimator=base_estimator,
                             random_state=rng,
                             **params).fit(X_train, y_train).predict(X_test) 
Example 40
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 5 votes vote down vote up
def test_neighbors_regressors_zero_distance():
    # Test radius-based regressor, when distance to a sample is zero.

    X = np.array([[1.0, 1.0], [1.0, 1.0], [2.0, 2.0], [2.5, 2.5]])
    y = np.array([1.0, 1.5, 2.0, 0.0])
    radius = 0.2
    z = np.array([[1.1, 1.1], [2.0, 2.0]])

    rnn_correct_labels = np.array([1.25, 2.0])

    knn_correct_unif = np.array([1.25, 1.0])
    knn_correct_dist = np.array([1.25, 2.0])

    for algorithm in ALGORITHMS:
        # we don't test for weights=_weight_func since user will be expected
        # to handle zero distances themselves in the function.
        for weights in ['uniform', 'distance']:
            rnn = neighbors.RadiusNeighborsRegressor(radius=radius,
                                                     weights=weights,
                                                     algorithm=algorithm)
            rnn.fit(X, y)
            assert_array_almost_equal(rnn_correct_labels, rnn.predict(z))

        for weights, corr_labels in zip(['uniform', 'distance'],
                                        [knn_correct_unif, knn_correct_dist]):
            knn = neighbors.KNeighborsRegressor(n_neighbors=2,
                                                weights=weights,
                                                algorithm=algorithm)
            knn.fit(X, y)
            assert_array_almost_equal(corr_labels, knn.predict(z)) 
Example 41
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 5 votes vote down vote up
def test_predict_sparse_ball_kd_tree():
    rng = np.random.RandomState(0)
    X = rng.rand(5, 5)
    y = rng.randint(0, 2, 5)
    nbrs1 = neighbors.KNeighborsClassifier(1, algorithm='kd_tree')
    nbrs2 = neighbors.KNeighborsRegressor(1, algorithm='ball_tree')
    for model in [nbrs1, nbrs2]:
        model.fit(X, y)
        assert_raises(ValueError, model.predict, csr_matrix(X)) 
Example 42
Project: poormining   Author: bowenpay   File: model.py    MIT License 5 votes vote down vote up
def get_classifier(self, X, Y):
        """ 构建线性回归模型
        :param X: 训练数据
        :param Y: 训练数据结果
        :return: 模型
        """

        clf = KNeighborsRegressor(weights='uniform')
        clf.fit(X, Y)
        return clf 
Example 43
Project: autotf   Author: DMALab   File: superknn.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def pred(self, dict):

        global result
        ind = list(dict.keys())[0]
        pred_X = np.array(list(dict.values())[0]).reshape(1, -1)
        # print("pred_X:", pred_X)

        points = []
        # count is the indice number.
        for count in range(0, len(self.stack_X)):
            if self.regression:
                distance = find_distance(self.stack_X[count], pred_X.ravel(), self.epsilon)
            else:
                if self.needs_proba:
                    distance = find_distance(self.stack_X[count], pred_X.ravel(), self.epsilon)
                else:
                    distance = find_equal(self.stack_X[count], pred_X.ravel())

            if distance >= self.alpha:
                points.append(count)

        # if no points are selected, return 0
        if len(points) == 0:
            if self.verbose:
                print(ind, "No points were selected, prediction is 0")
            result.put({ind:np.array([0])})
        else:
            '''定义距离,对于回归直接用距离与weight成反比,距离用空间距离'''
            if self.regression:
                neigh = KNeighborsRegressor(n_neighbors=len(points), weights='distance')
                neigh.fit(self.stack_X[points], self.y_[points])
                avg = neigh.predict(pred_X)
                op = {ind: avg}
                result.put(op)
            else:
                neigh = KNeighborsClassifier(n_neighbors=len(points), weights='distance')
                neigh.fit(self.stack_X[points], self.y_[points])
                avg = neigh.predict(pred_X)
                op = {ind: avg}
                result.put(op) 
Example 44
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 5 votes vote down vote up
def test_precomputed_cross_validation():
    # Ensure array is split correctly
    rng = np.random.RandomState(0)
    X = rng.rand(20, 2)
    D = pairwise_distances(X, metric='euclidean')
    y = rng.randint(3, size=20)
    for Est in (neighbors.KNeighborsClassifier,
                neighbors.RadiusNeighborsClassifier,
                neighbors.KNeighborsRegressor,
                neighbors.RadiusNeighborsRegressor):
        metric_score = cross_val_score(Est(), X, y)
        precomp_score = cross_val_score(Est(metric='precomputed'), D, y)
        assert_array_equal(metric_score, precomp_score) 
Example 45
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 5 votes vote down vote up
def test_neighbors_regressors_zero_distance():
    # Test radius-based regressor, when distance to a sample is zero.

    X = np.array([[1.0, 1.0], [1.0, 1.0], [2.0, 2.0], [2.5, 2.5]])
    y = np.array([1.0, 1.5, 2.0, 0.0])
    radius = 0.2
    z = np.array([[1.1, 1.1], [2.0, 2.0]])

    rnn_correct_labels = np.array([1.25, 2.0])

    knn_correct_unif = np.array([1.25, 1.0])
    knn_correct_dist = np.array([1.25, 2.0])

    for algorithm in ALGORITHMS:
        # we don't test for weights=_weight_func since user will be expected
        # to handle zero distances themselves in the function.
        for weights in ['uniform', 'distance']:
            rnn = neighbors.RadiusNeighborsRegressor(radius=radius,
                                                     weights=weights,
                                                     algorithm=algorithm)
            rnn.fit(X, y)
            assert_array_almost_equal(rnn_correct_labels, rnn.predict(z))

        for weights, corr_labels in zip(['uniform', 'distance'],
                                        [knn_correct_unif, knn_correct_dist]):
            knn = neighbors.KNeighborsRegressor(n_neighbors=2,
                                                weights=weights,
                                                algorithm=algorithm)
            knn.fit(X, y)
            assert_array_almost_equal(corr_labels, knn.predict(z)) 
Example 46
Project: BorderPeelingClustering   Author: nadavbar   File: border_tools.py    MIT License 5 votes vote down vote up
def update_link_thresholds(current_data, original_indices ,original_data,
                           thresholds, dist_threshold, link_dist_expansion_factor, k=10):
    # index the filters according to the original data indices:
    original_data_filter = np.zeros(len(original_data)).astype(int)

    original_data_filter[original_indices] = 1

    xy = original_data[(original_data_filter == 0)]
    Z = thresholds[(original_data_filter == 0)]


    knn = KNeighborsRegressor(k, weights="uniform")

    try:
        new_thresholds = knn.fit(xy, Z).predict(current_data)
    except:
        #print "failed to run kneighbours regressor"
        return thresholds

    for i, p,t in zip(xrange(len(current_data)), current_data, new_thresholds):
        #original_index = original_data_points_indices[tuple(p)]
        original_index = original_indices[i]

        if np.isnan(t):
            print "threshold is nan"

        if np.isnan(t) or (t * link_dist_expansion_factor) > dist_threshold:
            #print "setting dist threshold"
            thresholds[original_index] = dist_threshold
        else:
            #print "setting threhold: %.2f"%(t * link_dist_expansion_factor)
            thresholds[original_index] = t * link_dist_expansion_factor

    return thresholds 
Example 47
Project: ML-Predictions   Author: ltfschoen   File: prediction_utils.py    MIT License 5 votes vote down vote up
def generate_model(self, regressor, qty_neighbors, algorithm, distance_type):
        """ Regressor Model Generation"""
        if regressor == "knn":
            return KNeighborsRegressor(n_neighbors=qty_neighbors, algorithm=algorithm, p=distance_type)
        elif regressor == "linear":
            return LinearRegression(fit_intercept=True) # copy_X=True, n_jobs=1, normalize=False
        elif regressor == "logistic":
            return LogisticRegression(class_weight='balanced') 
Example 48
Project: BARalgorithm   Author: TUT-ISI   File: BARfunctions.py    MIT License 5 votes vote down vote up
def loadSurfaceReflectancePrior(granuleMonth, granuleYear, surfacePriorModifier, settings):
    print('  Loading surface reflectance prior model {}/{}'.format(granuleMonth, settings['surfacePriorYear']))
    surflats, surflons = np.meshgrid((np.arange(-90.0, 90.0, 0.05))[::-1] + 0.05 / 2, (np.arange(-180.0, 180.0, 0.05)) + 0.05 / 2)
    surflats, surflons = surflats.T.ravel(), surflons.T.ravel()

    surfacePriorYear = settings['surfacePriorYear']

    surfprior466 = 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band3_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel() + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band3_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()
    surfprior466std = np.sqrt(0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band3_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2 + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band3_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2)

    surfprior550 = 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band4_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel() + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band4_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()
    surfprior550std = np.sqrt(0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band4_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2 + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band4_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2)

    surfprior644 = 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band1_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel() + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band1_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()
    surfprior644std = np.sqrt(0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band1_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2 + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band1_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2)

    surfprior211 = 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band7_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel() + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band7_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()
    surfprior211std = np.sqrt(0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_BSA_Band7_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2 + 0.5 * (imread(os.path.join('models', 'SurfaceReflectance', str(surfacePriorYear), 'Albedo_WSA_Band7_std_{}_{:02d}.png'.format(str(surfacePriorYear), granuleMonth))) / 65535.0).ravel()**2)

    surfmask = (surfprior466 > 0.0)
    surfstdmask = (surfprior466std > 0.0)

    surfacePrior = {
        'surfprior466': surfprior466[surfmask] * surfacePriorModifier['meancoef'],
        'surfprior550': surfprior550[surfmask] * surfacePriorModifier['meancoef'],
        'surfprior644': surfprior644[surfmask] * surfacePriorModifier['meancoef'],
        'surfprior211': surfprior211[surfmask] * surfacePriorModifier['meancoef'],
        'lat': surflats[surfmask],
        'lon': surflons[surfmask],
        'surfprior466std': surfprior466std[surfstdmask] * surfacePriorModifier['stdcoef'],
        'surfprior550std': surfprior550std[surfstdmask] * surfacePriorModifier['stdcoef'],
        'surfprior644std': surfprior644std[surfstdmask] * surfacePriorModifier['stdcoef'],
        'surfprior211std': surfprior211std[surfstdmask] * surfacePriorModifier['stdcoef'],
        'latstd': surflats[surfstdmask],
        'lonstd': surflons[surfstdmask],
        'kNNmean': KNeighborsRegressor(n_neighbors=3, leaf_size=50000, n_jobs=-1).fit(np.hstack((surflons[surfmask][:, np.newaxis], surflats[surfmask][:, np.newaxis])), np.zeros_like(surfprior466[surfmask])[:, np.newaxis]),
        'kNNstd': KNeighborsRegressor(n_neighbors=3, leaf_size=50000, n_jobs=-1).fit(np.hstack((surflons[surfstdmask][:, np.newaxis], surflats[surfstdmask][:, np.newaxis])), np.zeros_like(surfprior466[surfstdmask])[:, np.newaxis]),
    }
    return surfacePrior 
Example 49
Project: q2-sample-classifier   Author: qiime2   File: utilities.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _disable_feature_selection(estimator, optimize_feature_selection):
    '''disable feature selection for unsupported classifiers.'''

    unsupported = ['KNeighborsClassifier', 'SVC', 'KNeighborsRegressor', 'SVR']

    if estimator in unsupported:
        optimize_feature_selection = False
        calc_feature_importance = False
        _warn_feature_selection()
    else:
        calc_feature_importance = True

    return optimize_feature_selection, calc_feature_importance 
Example 50
Project: stacking   Author: ikki407   File: regression.py    MIT License 5 votes vote down vote up
def build_model(self):
            return KNeighborsRegressor(**self.params) 
Example 51
Project: curumim-automl-sandbox   Author: end-to-end-data-science   File: knn.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, config, **kwargs):
        super().__init__(config, **kwargs)
        self.model = KNeighborsRegressor(**self.param()) 
Example 52
Project: python-base-docker-images   Author: LREN-CHUV   File: sklearn_to_pfa.py    Apache License 2.0 5 votes vote down vote up
def sklearn_to_pfa(estimator, types, featurizer=None):
    """
    Convert scikit-learn estimator to PFA format.
    :param estimator: Scikit-learn estimator, must be supported
    :param types: List of tuples (name, type)
    """
    types = _fix_types_compatibility(types)

    featurizer = featurizer or _construct_featurizer(types)

    if isinstance(estimator, SGDRegressor):
        return _pfa_sgdregressor(estimator, types, featurizer)
    elif isinstance(estimator, SGDClassifier):
        return _pfa_sgdclassifier(estimator, types, featurizer)
    elif isinstance(estimator, MLPRegressor):
        return _pfa_mlpregressor(estimator, types, featurizer)
    elif isinstance(estimator, MLPClassifier):
        return _pfa_mlpclassifier(estimator, types, featurizer)
    elif isinstance(estimator, MultinomialNB):
        return _pfa_multinomialnb(estimator, types, featurizer)
    elif isinstance(estimator, GaussianNB):
        return _pfa_gaussiannb(estimator, types, featurizer)
    elif isinstance(estimator, MixedNB):
        return _pfa_mixednb(estimator, types, featurizer)
    elif isinstance(estimator, KMeans):
        return _pfa_kmeans(estimator, types, featurizer)
    elif isinstance(estimator, KNeighborsRegressor):
        return _pfa_kneighborsregressor(estimator, types, featurizer)
    elif isinstance(estimator, KNeighborsClassifier):
        return _pfa_kneighborsclassifier(estimator, types, featurizer)
    elif isinstance(estimator, GradientBoostingRegressor):
        return _pfa_gradientboostingregressor(estimator, types, featurizer)
    elif isinstance(estimator, GradientBoostingClassifier):
        return _pfa_gradientboostingclassifier(estimator, types, featurizer)
    else:
        raise NotImplementedError('Estimator {} is not yet supported'.format(estimator.__class__.__name__)) 
Example 53
Project: python-base-docker-images   Author: LREN-CHUV   File: test_pfa_sklearn.py    Apache License 2.0 5 votes vote down vote up
def _kneighborsregressor(X, y, **kwargs):
    estimator = KNeighborsRegressor(**kwargs)
    estimator.fit(X, y)
    return estimator 
Example 54
Project: python-base-docker-images   Author: LREN-CHUV   File: test_pfa_sklearn.py    Apache License 2.0 5 votes vote down vote up
def test_estimator_to_pfa_kneighborsregressor():
    """Check that converted PFA is giving the same results as KNeighborsRegressor"""
    X, y, types = _regression_task()

    estimator = _kneighborsregressor(X, y, n_neighbors=2)

    pfa = sklearn_to_pfa(estimator, types)

    estimator_pred = estimator.predict(X)
    pfa_pred = _predict_pfa(X, types, pfa)

    assert all(estimator_pred == pfa_pred) 
Example 55
Project: ml-framework   Author: delerex   File: aiclass.py    MIT License 5 votes vote down vote up
def __init__(self, k=5):
        self.ai = KNeighborsRegressor(k) 
Example 56
Project: ml-framework   Author: delerex   File: quandl_test.py    MIT License 5 votes vote down vote up
def __init__(self, data, algo, par1, par2):
        self.dataset=data
        self.algo=algo
        self.par1=par1
        self.par2=par2
        if( self.algo == "NN"):
            self.ai = nnr( par1 )
            self.ai.fit(self.dataset.insample_data,self.dataset.insample_res) 
Example 57
Project: MI-POGUE   Author: twytock   File: montecarlo_crossvalid_minimal.py    GNU General Public License v3.0 5 votes vote down vote up
def create_growth_rate_mapping(lim_data,Z,**kwargs):
    """builds a model which maps the gene expression to growth rate
    includes no growth points outside the data"""
    # weights are not calculated here, should be done above this level
    #lim_data = lim_data
    if 'n_neighbors' in kwargs.keys():
        n_neighbors = kwargs['n_neighbors']
    else:
        n_neighbors=8
    allgrdat = lim_data
    allgr = Z
    clf = KNR(n_neighbors,'distance')
    clf.fit(allgrdat.values,allgr.values)
    return clf 
Example 58
Project: MI-POGUE   Author: twytock   File: figure-1.py    GNU General Public License v3.0 5 votes vote down vote up
def create_growth_rate_mapping(lim_data,Z,**kwargs):
    """builds a model which maps the gene expression to growth rate
    includes no growth points outside the data"""
    # weights are not calculated here, should be done above this level
    #lim_data = lim_data
    if 'n_neighbors' in kwargs.keys():
        n_neighbors = kwargs['n_neighbors']
    else:
        n_neighbors=8
    allgrdat = lim_data
    allgr = Z
    clf = KNR(n_neighbors,'distance')
    clf.fit(allgrdat.values,allgr.values)
    return clf 
Example 59
Project: fuzzy_labeling_scRNA   Author: maxime1310   File: imputation.py    MIT License 5 votes vote down vote up
def proximity_imputation(real_latent1, normed_gene_exp_1, real_latent2, k=4):
    knn = neighbors.KNeighborsRegressor(k, weights='distance')
    y = knn.fit(real_latent1, normed_gene_exp_1).predict(real_latent2)
    return y 
Example 60
Project: PyXtal_ml   Author: qzhu2017   File: ml_sklearn.py    MIT License 4 votes vote down vote up
def __init__(self, algo, feature, prop, 
                 tag, pipeline = False, test_size = 0.3, **kwargs):
        self.feature = feature
        self.prop = prop
        self.tag = tag
        self.pipeline = pipeline
        self.test_size = test_size
        self.dict = kwargs
        self.algo_options = ['KNN', 'KNeighborsRegressor', 'KRR', 
                             'KernelRidge', 'GB', 'GradientBoostingRegressor',
                             'RF', 'RandomForestRegressor', 'SGD', 
                             'SGDRegressor', 'MLPRegressor', 'ANN', 
                             'SVR', 'Lasso', 'ElasticNet', 
                             'ENet', 'GaussianProcessRegressor', 'GPR']
        self.pipeline_options = ['VT', 'VarianceThreshold', 'PCA']
        self.parameters_level = ['light', 'medium', 'tight']
        
        # Convert the algorithm to Scikit-learn names.
        if algo in ['KNN', 'KNeighborsRegressor']:
            self.algo = 'KNeighborsRegressor'
        elif algo in ['KRR', 'KernelRidge']:
            self.algo = 'KernelRidge'
        elif algo in ['GB', 'GradientBoostingRegressor']:
            self.algo = 'GradientBoostingRegressor'
        elif algo in ['RF', 'RandomForestRegressor']:
            self.algo = 'RandomForestRegressor'
        elif algo in ['SGD', 'SGDRegressor']:
            self.algo = 'SGDRegressor'
        elif algo in ['SVR']:
            self.algo = 'SVR'
        elif algo in ['ENet', 'ElasticNet']:
            self.algo = 'ElasticNet'
        elif algo in ['LASSO']:
            self.algo = 'LASSO'
        elif algo in ['GPR', 'GaussianProcessRegressor']:
            self.algo = 'GaussianProcessRegressor'
        elif algo in ['ANN', 'MLPRegressor']:
            self.algo = 'MLPRegressor'

        # Split feature to training and testing datasets
        if self.algo in self.algo_options:
            # Split data into training and test sets
            self.X_train, self.X_test, self.Y_train, self.Y_test = \
                train_test_split(self.feature, self.prop, 
                                 test_size=self.test_size, 
                                 random_state=0)
            
            with open(yaml_path, 'r') as stream:
                try:
                    self.algos_params = yaml.load(stream)
                except yaml.YAMLError as exc:
                    print(exc)
        else:
            raise NotImplementedError(f"Unknown algorithm: {self.algo}.")
        
        self.ml() 
Example 61
Project: predictive_imputer   Author: log0ymxm   File: predictive_imputer.py    MIT License 4 votes vote down vote up
def fit(self, X, y=None, **kwargs):
        X = check_array(X, dtype=np.float64, force_all_finite=False)

        X_nan = np.isnan(X)
        most_by_nan = X_nan.sum(axis=0).argsort()[::-1]

        imputed = self.initial_imputer.fit_transform(X)
        new_imputed = imputed.copy()

        self.statistics_ = np.ma.getdata(X)
        self.gamma_ = []

        if self.f_model == "RandomForest":
            self.estimators_ = [RandomForestRegressor(n_estimators=50, n_jobs=-1, random_state=i, **kwargs) for i in range(X.shape[1])]
        elif self.f_model == "KNN":
            self.estimators_ = [KNeighborsRegressor(n_neighbors=min(5, sum(~X_nan[:, i])), **kwargs) for i in range(X.shape[1])]
        elif self.f_model == "PCA":
            self.estimators_ = [PCA(n_components=int(np.sqrt(min(X.shape))), whiten=True, **kwargs)]

        for iter in range(self.max_iter):
            if len(self.estimators_) > 1:
                for i in most_by_nan:

                    X_s = np.delete(new_imputed, i, 1)
                    y_nan = X_nan[:, i]

                    X_train = X_s[~y_nan]
                    y_train = new_imputed[~y_nan, i]
                    X_unk = X_s[y_nan]

                    estimator_ = self.estimators_[i]
                    estimator_.fit(X_train, y_train)
                    if len(X_unk) > 0:
                        new_imputed[y_nan, i] = estimator_.predict(X_unk)

            else:
                estimator_ = self.estimators_[0]
                estimator_.fit(new_imputed)
                new_imputed[X_nan] = estimator_.inverse_transform(estimator_.transform(new_imputed))[X_nan]

            gamma = ((new_imputed-imputed)**2/(1e-6+new_imputed.var(axis=0))).sum()/(1e-6+X_nan.sum())
            self.gamma_.append(gamma)
            if np.abs(np.diff(self.gamma_[-2:])) < self.tol:
                break

        return self 
Example 62
Project: Smart-Trader   Author: i-sultan   File: st_train_model.py    GNU General Public License v3.0 4 votes vote down vote up
def train_validate(self, df, validation_range, update_progress):
        """ Train and validate regressor on df samples with indices listed in validation_range. """
        training_summary = pd.DataFrame()
        first_sample, samples, labels = prepare_samples(df, self.indicators_samples)

        # progress bar parameters
        total_steps = len(self.model_params['sample_presentation']) * \
                      len(self.model_params['exp_weight']) * len(self.model_params['k'])
        completed_steps = 0

        # loop over model parameters
        for sample_presentation in self.model_params['sample_presentation']:
            presented_samples, presented_labels, normalizer = set_presentation(samples, labels, sample_presentation, self.indicators_samples['Daily'])

            for exp_weight in self.model_params['exp_weight']:
                weighted_samples = apply_exp_weights(presented_samples, exp_weight)

                for k in self.model_params['k']:
                    model, total_train_time, total_test_time = [[0 for i in range (len(h))] for j in range(3)]
                    error_list, relative_error_list, hit_list = [[[] for i in range (len(h))] for j in range(3)]
                    params = (sample_presentation, exp_weight, k)

                    # model training and validation core
                    for h_index in range(len(h)):
                        for index in validation_range:
                            i = index-first_sample                        
                            x_train, x_validate = weighted_samples[:i-h[h_index]+1,:], weighted_samples[i,:] #need to stop training h steps before test
                            y_train, y_validate = presented_labels[h_index][:i-h[h_index]+1], presented_labels[h_index][i]
                            #train
                            t1 = time.time()
                            model[h_index] = KNeighborsRegressor(n_neighbors=k) # train a separate model for each horizon
                            model[h_index].fit(x_train, y_train)
                            t2 = time.time()
                            train_time = (t2-t1)
                            #test
                            y_predict = model[h_index].predict(x_validate.reshape(1,-1))
                            test_time = (time.time()-t2)
                            #apend new results
                            y_validate_absolute = remove_presentation(y_validate,normalizer[i], sample_presentation)
                            y_predict_absolute  = remove_presentation(y_predict ,normalizer[i], sample_presentation)
                            error_list[h_index] += [y_validate_absolute - y_predict_absolute]
                            relative_error_list[h_index] += [(y_validate_absolute - y_predict_absolute)/y_validate_absolute]
                            hit_list[h_index] += [(y_validate-x_validate[-1])*(y_predict-x_validate[-1]) > 0]
            
                            total_train_time[h_index] += train_time
                            total_test_time[h_index] += test_time
                            if i == len(presented_labels[h_index])-1:
                                #very last training point, include last training oppurtunity
                                x_train = weighted_samples[:i+1,:]
                                y_train = presented_labels[h_index][:i+1]
                                model[h_index].fit(x_train, y_train)
                                break
                    
                    completed_steps += 1
                    update_progress(100.0 * completed_steps/total_steps)

                    #save last trained model, and add to training summary
                    training_summary = training_summary.append(summarize(self, model, error_list, relative_error_list, hit_list, 
                                                                        params, total_train_time, total_test_time))
        return training_summary, make_presentable(training_summary, self.summary_name) 
Example 63
Project: pymach   Author: gusseppe   File: evaluate.py    MIT License 4 votes vote down vote up
def get_feature_importance(self):
        non_tree_based_models = ['KNeighborsClassifier', 'MLPClassifier', 'SVC',
                                 'LinearDiscriminantAnalysis', 'GaussianNB',
                                 'LogisticRegression', 'KNeighborsRegressor',
                                 'MLPRegressor', 'SVR', 'LinearRegression',
                                 'BayesianRidge']

        models = self.estimators.keys()
        if self.problem_type == 'classification':
            for name_model in models:

                feature_imp = {'feature': [], 'importance':[]}
                if name_model in non_tree_based_models:
                    # estimator = self.estimators[name_model]
                    # y_pred = estimator.predict(self.X_test.values)

                    kbest = SelectKBest(score_func=chi2, k=self.X_train.shape[1])
                    kbest = kbest.fit(self.X_train, self.y_train)

                    print(kbest.scores_)
                    feature_importance = kbest.scores_
                    feature_names = list(self.X_train.columns)
                    for score, name in sorted(zip(feature_importance, feature_names), reverse=True):
                        feature_imp['feature'].append(name)
                        feature_imp['importance'].append(score)

                    df_fi =  pd.DataFrame(feature_imp)
                    df_fi['importance'] = df_fi['importance'] / df_fi['importance'].sum()
                    self.feature_importance[name_model] = df_fi
                else:
                    # Tree based models
                    estimator = self.estimators[name_model].named_steps[name_model]
                    if not hasattr(estimator, 'feature_importances_'):
                        feature_importance = np.mean([
                            tree.feature_importances_ for tree in estimator.estimators_], axis=0)
                        feature_names = list(self.X_train.columns)
                        for score, name in sorted(zip(feature_importance, feature_names), reverse=True):
                            feature_imp['feature'].append(name)
                            feature_imp['importance'].append(score)

                        self.feature_importance[name_model] = pd.DataFrame(feature_imp)
                    else:
                        feature_importance = estimator.feature_importances_
                        feature_names = list(self.X_train.columns)
                        for score, name in sorted(zip(feature_importance, feature_names), reverse=True):
                            feature_imp['feature'].append(name)
                            feature_imp['importance'].append(score)

                        df_fi =  pd.DataFrame(feature_imp)
                        if name_model == 'LGBMClassifier':
                            df_fi['importance'] = df_fi['importance'] / df_fi['importance'].sum()

                        self.feature_importance[name_model] = pd.DataFrame(df_fi)


        else:
            # Here fill with regression
            self.feature_importance = None
        # print(self.metrics) 
Example 64
Project: Weiss   Author: WangWenjun559   File: test_neighbors.py    Apache License 2.0 4 votes vote down vote up
def test_neighbors_badargs():
    # Test bad argument values: these should all raise ValueErrors
    assert_raises(ValueError,
                  neighbors.NearestNeighbors,
                  algorithm='blah')

    X = rng.random_sample((10, 2))
    Xsparse = csr_matrix(X)
    y = np.ones(10)

    for cls in (neighbors.KNeighborsClassifier,
                neighbors.RadiusNeighborsClassifier,
                neighbors.KNeighborsRegressor,
                neighbors.RadiusNeighborsRegressor):
        assert_raises(ValueError,
                      cls,
                      weights='blah')
        assert_raises(ValueError,
                      cls, p=-1)
        assert_raises(ValueError,
                      cls, algorithm='blah')
        nbrs = cls(algorithm='ball_tree', metric='haversine')
        assert_raises(ValueError,
                      nbrs.predict,
                      X)
        assert_raises(ValueError,
                      ignore_warnings(nbrs.fit),
                      Xsparse, y)
        nbrs = cls()
        assert_raises(ValueError,
                      nbrs.fit,
                      np.ones((0, 2)), np.ones(0))
        assert_raises(ValueError,
                      nbrs.fit,
                      X[:, :, None], y)
        nbrs.fit(X, y)
        assert_raises(ValueError,
                      nbrs.predict,
                      [])

    nbrs = neighbors.NearestNeighbors().fit(X)

    assert_raises(ValueError,
                  nbrs.kneighbors_graph,
                  X, mode='blah')
    assert_raises(ValueError,
                  nbrs.radius_neighbors_graph,
                  X, mode='blah') 
Example 65
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 4 votes vote down vote up
def test_precomputed(random_state=42):
    """Tests unsupervised NearestNeighbors with a distance matrix."""
    # Note: smaller samples may result in spurious test success
    rng = np.random.RandomState(random_state)
    X = rng.random_sample((10, 4))
    Y = rng.random_sample((3, 4))
    DXX = metrics.pairwise_distances(X, metric='euclidean')
    DYX = metrics.pairwise_distances(Y, X, metric='euclidean')
    for method in ['kneighbors']:
        # TODO: also test radius_neighbors, but requires different assertion

        # As a feature matrix (n_samples by n_features)
        nbrs_X = neighbors.NearestNeighbors(n_neighbors=3)
        nbrs_X.fit(X)
        dist_X, ind_X = getattr(nbrs_X, method)(Y)

        # As a dense distance matrix (n_samples by n_samples)
        nbrs_D = neighbors.NearestNeighbors(n_neighbors=3, algorithm='brute',
                                            metric='precomputed')
        nbrs_D.fit(DXX)
        dist_D, ind_D = getattr(nbrs_D, method)(DYX)
        assert_array_almost_equal(dist_X, dist_D)
        assert_array_almost_equal(ind_X, ind_D)

        # Check auto works too
        nbrs_D = neighbors.NearestNeighbors(n_neighbors=3, algorithm='auto',
                                            metric='precomputed')
        nbrs_D.fit(DXX)
        dist_D, ind_D = getattr(nbrs_D, method)(DYX)
        assert_array_almost_equal(dist_X, dist_D)
        assert_array_almost_equal(ind_X, ind_D)

        # Check X=None in prediction
        dist_X, ind_X = getattr(nbrs_X, method)(None)
        dist_D, ind_D = getattr(nbrs_D, method)(None)
        assert_array_almost_equal(dist_X, dist_D)
        assert_array_almost_equal(ind_X, ind_D)

        # Must raise a ValueError if the matrix is not of correct shape
        assert_raises(ValueError, getattr(nbrs_D, method), X)

    target = np.arange(X.shape[0])
    for Est in (neighbors.KNeighborsClassifier,
                neighbors.RadiusNeighborsClassifier,
                neighbors.KNeighborsRegressor,
                neighbors.RadiusNeighborsRegressor):
        print(Est)
        est = Est(metric='euclidean')
        est.radius = est.n_neighbors = 1
        pred_X = est.fit(X, target).predict(Y)
        est.metric = 'precomputed'
        pred_D = est.fit(DXX, target).predict(DYX)
        assert_array_almost_equal(pred_X, pred_D) 
Example 66
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_neighbors.py    Apache License 2.0 4 votes vote down vote up
def test_neighbors_badargs():
    # Test bad argument values: these should all raise ValueErrors
    assert_raises(ValueError,
                  neighbors.NearestNeighbors,
                  algorithm='blah')

    X = rng.random_sample((10, 2))
    Xsparse = csr_matrix(X)
    y = np.ones(10)

    for cls in (neighbors.KNeighborsClassifier,
                neighbors.RadiusNeighborsClassifier,
                neighbors.KNeighborsRegressor,
                neighbors.RadiusNeighborsRegressor):
        assert_raises(ValueError,
                      cls,
                      weights='blah')
        assert_raises(ValueError,
                      cls, p=-1)
        assert_raises(ValueError,
                      cls, algorithm='blah')
        nbrs = cls(algorithm='ball_tree', metric='haversine')
        assert_raises(ValueError,
                      nbrs.predict,
                      X)
        assert_raises(ValueError,
                      ignore_warnings(nbrs.fit),
                      Xsparse, y)
        nbrs = cls()
        assert_raises(ValueError,
                      nbrs.fit,
                      np.ones((0, 2)), np.ones(0))
        assert_raises(ValueError,
                      nbrs.fit,
                      X[:, :, None], y)
        nbrs.fit(X, y)
        assert_raises(ValueError,
                      nbrs.predict,
                      [[]])
        if (isinstance(cls, neighbors.KNeighborsClassifier) or
                isinstance(cls, neighbors.KNeighborsRegressor)):
            nbrs = cls(n_neighbors=-1)
            assert_raises(ValueError, nbrs.fit, X, y)

    nbrs = neighbors.NearestNeighbors().fit(X)

    assert_raises(ValueError, nbrs.kneighbors_graph, X, mode='blah')
    assert_raises(ValueError, nbrs.radius_neighbors_graph, X, mode='blah') 
Example 67
Project: ceml   Author: andreArtelt   File: test_sklearn_knn.py    MIT License 4 votes vote down vote up
def test_knn_regressor():
    # Load data
    X, y = load_boston(True)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=4242)

    # Create and fit model
    model = KNeighborsRegressor(n_neighbors=3)
    model.fit(X_train, y_train)

    # Select data point for explaining its prediction
    x_orig = X_test[1:4][0,:]
    y_orig_pred = model.predict([x_orig])
    assert y_orig_pred >= 20. and y_orig_pred <= 21.

    # Compute counterfactual
    y_target = 25.
    y_target_done = lambda z: np.abs(z - y_target) < 2.

    features_whitelist = None

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization="l1", C=1.0, optimizer="bfgs", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization=None, optimizer="bfgs", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization=None, optimizer="nelder-mead", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))


    features_whitelist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization="l1", C=1.0, optimizer="bfgs", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))
    assert all([True if i in features_whitelist else delta[i] == 0. for i in range(x_orig.shape[0])])

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization="l1", C=1.0, optimizer="nelder-mead", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))
    assert all([True if i in features_whitelist else delta[i] == 0. for i in range(x_orig.shape[0])])

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization=None, optimizer="bfgs", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))
    assert all([True if i in features_whitelist else delta[i] == 0. for i in range(x_orig.shape[0])])

    x_cf, y_cf, delta = generate_counterfactual(model, x_orig, y_target, done=y_target_done, features_whitelist=features_whitelist, regularization=None, optimizer="nelder-mead", return_as_dict=False)
    assert y_target_done(y_cf)
    assert y_target_done(model.predict(np.array([x_cf])))
    assert all([True if i in features_whitelist else delta[i] == 0. for i in range(x_orig.shape[0])]) 
Example 68
Project: DataScience-webapp-with-flask   Author: alvarodemig   File: models.py    MIT License 4 votes vote down vote up
def regressionModels():

    models = {}
    
    ######################################
    # Linear Regression
    ######################################
    from sklearn.linear_model import LinearRegression
    models['Linear Regression'] = {}
    models['Linear Regression'] = LinearRegression()
    
    ######################################
    # Random Forest
    ######################################
    from sklearn.ensemble import RandomForestRegressor
    models['Random Forests'] = RandomForestRegressor()
    
    ######################################
    # K Nearest Neighbors
    ######################################
    from sklearn.neighbors import KNeighborsRegressor
    models['K Nearest Neighbors'] = KNeighborsRegressor()
    
    ######################################
    # AdaBoost
    ######################################
    from sklearn.ensemble import AdaBoostRegressor
    from sklearn.tree import DecisionTreeRegressor
    models['AdaBoost'] = AdaBoostRegressor()
    
    ######################################
    # X Gradient Boosting
    ######################################
    from xgboost import XGBRegressor
    models['X Gradient Boosting'] = XGBRegressor()
    
    ######################################
    # Neural Networks: MultiLayer Perceptron
    ######################################
    from sklearn.neural_network import MLPRegressor
    models['MultiLayer Perceptron'] = MLPRegressor()
    
    return models 
Example 69
Project: CECNAN   Author: JonasDHomburg   File: plot_ea.py    GNU General Public License v2.0 4 votes vote down vote up
def fitness_landscape(coords, filter_heights=True):
  Y, height = zip(*coords.values())
  if filter_heights:
    Y, height = zip(*[(y, h) for y, h in zip(Y, height) if h >= 0])

  def _distance(distance):
    tmp = np.exp(-distance)
    tmp = tmp / tmp.sum()
    return tmp

  neigh = KNeighborsRegressor(n_neighbors=7, weights=_distance)
  Y = np.asarray(Y)
  height = np.asarray(height)
  neigh.fit(Y, height)
  xmin, xmax = Y[:, 0].min(), Y[:, 0].max()
  ymin, ymax = Y[:, 1].min(), Y[:, 1].max()
  xmin, xmax = xmin - 0.2 * (xmax - xmin), xmax + .2 * (xmax - xmin)
  ymin, ymax = ymin - 0.2 * (ymax - ymin), ymax + .2 * (ymax - ymin)
  zmin, zmax = height.min(), height.max()
  scale = max(ymax - ymin, xmax - xmin) / (zmax - zmin)

  # x_grid, y_grid = np.mgrid[Y[:,0].min():Y[:,0].max():0.5j, Y[:,1].min():Y[:,1].max():0.5j]
  y_grid, x_grid = np.meshgrid(np.linspace(xmin, xmax, 200), np.linspace(ymin, ymax, 200))

  def _height(x_grid, y_grid):
    _shape = x_grid.shape
    t = np.stack([x_grid.reshape((-1)), y_grid.reshape((-1))], axis=-1)
    heights = neigh.predict(t).reshape(_shape)
    return heights

  # mlab.title('Search Space Evaluation')
  # f = mlab.figure('Surf', bgcolor=(1,1,1), fgcolor=(0,0,0), size=(1000,1000))
  f = ma_figure("Surf", bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(2000, 2000))
  # print(heights.min(), heights.max())
  s = ma_surf(x_grid, y_grid, _height, warp_scale=scale, colormap='spectral')  # , vmin=min(height), vmax=max(height))
  contour_heights = ((1 - np.exp(-np.linspace(3, 20, 100))) * max(height)).tolist()
  # print(contours)
  cs = ma_contour_surf(x_grid, y_grid, _height, warp_scale=scale, contours=contour_heights, colormap='binary',
                       line_width=1)
  s.actor.actor.scale = (1, 1, 1)
  s.actor.property.lighting = False
  ma_colorbar(s, title='Fitness', orientation='vertical')

  # mlab.options.offscreen = True
  ma_axes(s, x_axis_visibility=True, xlabel='', line_width=4,
          y_axis_visibility=True, ylabel='',
          z_axis_visibility=True, zlabel='',
          ranges=(-0.5, .5, -0.5, .5, min(height), max(height)))
  # f.scene.isometric_view()
  ma_show()

# </editor-fold>