当前位置: 首页>>代码示例>>Python>>正文


Python ElasticNet.fit方法代码示例

本文整理汇总了Python中sklearn.linear_model.coordinate_descent.ElasticNet.fit方法的典型用法代码示例。如果您正苦于以下问题:Python ElasticNet.fit方法的具体用法?Python ElasticNet.fit怎么用?Python ElasticNet.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.linear_model.coordinate_descent.ElasticNet的用法示例。


在下文中一共展示了ElasticNet.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_enet_toy_list_input

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_toy_list_input():
    # Test ElasticNet for various values of alpha and l1_ratio with list X

    X = np.array([[-1], [0], [1]])
    X = sp.csc_matrix(X)
    Y = [-1, 0, 1]       # just a straight line
    T = np.array([[2], [3], [4]])  # test sample

    # this should be the same as unregularized least squares
    clf = ElasticNet(alpha=0, l1_ratio=1.0)
    # catch warning about alpha=0.
    # this is discouraged but should work.
    ignore_warnings(clf.fit)(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [1])
    assert_array_almost_equal(pred, [2, 3, 4])
    assert_almost_equal(clf.dual_gap_, 0)

    clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=1000)
    clf.fit(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.50819], decimal=3)
    assert_array_almost_equal(pred, [1.0163,  1.5245,  2.0327], decimal=3)
    assert_almost_equal(clf.dual_gap_, 0)

    clf = ElasticNet(alpha=0.5, l1_ratio=0.5)
    clf.fit(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.45454], 3)
    assert_array_almost_equal(pred, [0.9090,  1.3636,  1.8181], 3)
    assert_almost_equal(clf.dual_gap_, 0)
开发者ID:0664j35t3r,项目名称:scikit-learn,代码行数:33,代码来源:test_sparse_coordinate_descent.py

示例2: _test_sparse_enet_not_as_toy_dataset

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def _test_sparse_enet_not_as_toy_dataset(alpha, fit_intercept, positive):
    n_samples, n_features, max_iter = 100, 100, 1000
    n_informative = 10

    X, y = make_sparse_data(n_samples, n_features, n_informative,
                            positive=positive)

    X_train, X_test = X[n_samples / 2:], X[:n_samples / 2]
    y_train, y_test = y[n_samples / 2:], y[:n_samples / 2]

    s_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept,
                       max_iter=max_iter, tol=1e-7, positive=positive,
                       warm_start=True)
    s_clf.fit(X_train, y_train)

    assert_almost_equal(s_clf.dual_gap_, 0, 4)
    assert_greater(s_clf.score(X_test, y_test), 0.85)

    # check the convergence is the same as the dense version
    d_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept,
                       max_iter=max_iter, tol=1e-7, positive=positive,
                       warm_start=True)
    d_clf.fit(X_train.todense(), y_train)

    assert_almost_equal(d_clf.dual_gap_, 0, 4)
    assert_greater(d_clf.score(X_test, y_test), 0.85)

    assert_almost_equal(s_clf.coef_, d_clf.coef_, 5)
    assert_almost_equal(s_clf.intercept_, d_clf.intercept_, 5)

    # check that the coefs are sparse
    assert_less(np.sum(s_clf.coef_ != 0.0), 2 * n_informative)
开发者ID:2011200799,项目名称:scikit-learn,代码行数:34,代码来源:test_sparse_coordinate_descent.py

示例3: test_enet_positive_constraint

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_positive_constraint():
    X = [[-1], [0], [1]]
    y = [1, 0, -1]       # just a straight line with negative slope

    enet = ElasticNet(alpha=0.1, max_iter=1000, positive=True)
    enet.fit(X, y)
    assert min(enet.coef_) >= 0
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:9,代码来源:test_coordinate_descent.py

示例4: test_sparse_enet_not_as_toy_dataset

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_sparse_enet_not_as_toy_dataset():
    n_samples, n_features, max_iter = 100, 100, 1000
    n_informative = 10

    X, y = make_sparse_data(n_samples, n_features, n_informative)

    X_train, X_test = X[n_samples / 2:], X[:n_samples / 2]
    y_train, y_test = y[n_samples / 2:], y[:n_samples / 2]

    s_clf = SparseENet(alpha=0.1, rho=0.8, fit_intercept=False,
                       max_iter=max_iter, tol=1e-7)
    s_clf.fit(X_train, y_train)
    assert_almost_equal(s_clf.dual_gap_, 0, 4)
    assert s_clf.score(X_test, y_test) > 0.85

    # check the convergence is the same as the dense version
    d_clf = DenseENet(alpha=0.1, rho=0.8, fit_intercept=False,
                      max_iter=max_iter, tol=1e-7)
    d_clf.fit(X_train, y_train)
    assert_almost_equal(d_clf.dual_gap_, 0, 4)
    assert d_clf.score(X_test, y_test) > 0.85

    assert_almost_equal(s_clf.coef_, d_clf.coef_, 5)

    # check that the coefs are sparse
    assert np.sum(s_clf.coef_ != 0.0) < 2 * n_informative
开发者ID:Yangqing,项目名称:scikit-learn,代码行数:28,代码来源:test_coordinate_descent.py

示例5: test_enet_copy_X_True

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_copy_X_True(check_input):
    X, y, _, _ = build_dataset()
    X = X.copy(order='F')

    original_X = X.copy()
    enet = ElasticNet(copy_X=True)
    enet.fit(X, y, check_input=check_input)

    assert_array_equal(original_X, X)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:11,代码来源:test_coordinate_descent.py

示例6: test_enet_copy_X_False_check_input_False

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_copy_X_False_check_input_False():
    X, y, _, _ = build_dataset()
    X = X.copy(order='F')

    original_X = X.copy()
    enet = ElasticNet(copy_X=False)
    enet.fit(X, y, check_input=False)

    # No copying, X is overwritten
    assert np.any(np.not_equal(original_X, X))
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:12,代码来源:test_coordinate_descent.py

示例7: test_normalize_option

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_normalize_option():
    """ Check that the normalize option in enet works """
    X = sp.csc_matrix([[-1], [0], [1]])
    y = [-1, 0, 1]
    clf_dense = ElasticNet(fit_intercept=True, normalize=True)
    clf_sparse = ElasticNet(fit_intercept=True, normalize=True)
    clf_dense.fit(X, y)
    X = sp.csc_matrix(X)
    clf_sparse.fit(X, y)
    assert_almost_equal(clf_dense.dual_gap_, 0)
    assert_array_almost_equal(clf_dense.coef_, clf_sparse.coef_)
开发者ID:MarkyV,项目名称:scikit-learn,代码行数:13,代码来源:test_sparse_coordinate_descent.py

示例8: test_warm_start

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_warm_start():
    X, y, _, _ = build_dataset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", UserWarning)
        clf = ElasticNet(alpha=0.1, max_iter=5, warm_start=True)
        clf.fit(X, y)
        clf.fit(X, y)  # do a second round with 5 iterations

        clf2 = ElasticNet(alpha=0.1, max_iter=10)
        clf2.fit(X, y)
        assert_array_almost_equal(clf2.coef_, clf.coef_)
开发者ID:99plus2,项目名称:scikit-learn,代码行数:13,代码来源:test_coordinate_descent.py

示例9: test_enet_multitarget

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_multitarget():
    n_targets = 3
    X, y, _, _ = build_dataset(n_samples=10, n_features=8, n_informative_features=10, n_targets=n_targets)
    estimator = ElasticNet(alpha=0.01, fit_intercept=True)
    estimator.fit(X, y)
    coef, intercept, dual_gap = (estimator.coef_, estimator.intercept_, estimator.dual_gap_)

    for k in range(n_targets):
        estimator.fit(X, y[:, k])
        assert_array_almost_equal(coef[k, :], estimator.coef_)
        assert_array_almost_equal(intercept[k], estimator.intercept_)
        assert_array_almost_equal(dual_gap[k], estimator.dual_gap_)
开发者ID:nelson-liu,项目名称:scikit-learn,代码行数:14,代码来源:test_coordinate_descent.py

示例10: test_enet_multitarget

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_multitarget():
    n_targets = 3
    X, y = make_sparse_data(n_targets=n_targets)

    estimator = ElasticNet(alpha=0.01, fit_intercept=True, precompute=None)
    # XXX: There is a bug when precompute is not None!
    estimator.fit(X, y)
    coef, intercept, dual_gap = (estimator.coef_, estimator.intercept_, estimator.dual_gap_)

    for k in range(n_targets):
        estimator.fit(X, y[:, k])
        assert_array_almost_equal(coef[k, :], estimator.coef_)
        assert_array_almost_equal(intercept[k], estimator.intercept_)
        assert_array_almost_equal(dual_gap[k], estimator.dual_gap_)
开发者ID:agamemnonc,项目名称:scikit-learn,代码行数:16,代码来源:test_sparse_coordinate_descent.py

示例11: test_enet_toy

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_enet_toy():
    """
    Test ElasticNet for various parameters of alpha and l1_ratio.

    Actually, the parameters alpha = 0 should not be allowed. However,
    we test it as a border case.

    ElasticNet is tested with and without precomputed Gram matrix
    """

    X = np.array([[-1.], [0.], [1.]])
    Y = [-1, 0, 1]       # just a straight line
    T = [[2.], [3.], [4.]]  # test sample

    # this should be the same as lasso
    clf = ElasticNet(alpha=1e-8, l1_ratio=1.0)
    clf.fit(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [1])
    assert_array_almost_equal(pred, [2, 3, 4])
    assert_almost_equal(clf.dual_gap_, 0)

    clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=100,
                     precompute=False)
    clf.fit(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.50819], decimal=3)
    assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3)
    assert_almost_equal(clf.dual_gap_, 0)

    clf.set_params(max_iter=100, precompute=True)
    clf.fit(X, Y)  # with Gram
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.50819], decimal=3)
    assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3)
    assert_almost_equal(clf.dual_gap_, 0)

    clf.set_params(max_iter=100, precompute=np.dot(X.T, X))
    clf.fit(X, Y)  # with Gram
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.50819], decimal=3)
    assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3)
    assert_almost_equal(clf.dual_gap_, 0)

    clf = ElasticNet(alpha=0.5, l1_ratio=0.5)
    clf.fit(X, Y)
    pred = clf.predict(T)
    assert_array_almost_equal(clf.coef_, [0.45454], 3)
    assert_array_almost_equal(pred, [0.9090, 1.3636, 1.8181], 3)
    assert_almost_equal(clf.dual_gap_, 0)
开发者ID:AngelaGuoguo,项目名称:scikit-learn,代码行数:52,代码来源:test_coordinate_descent.py

示例12: test_fit_simple_backupsklearn

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_fit_simple_backupsklearn():
    df = pd.read_csv("./open_data/simple.txt", delim_whitespace=True)
    X = np.array(df.iloc[:, :df.shape[1] - 1], dtype='float32', order='C')
    y = np.array(df.iloc[:, df.shape[1] - 1], dtype='float32', order='C')
    Solver = h2o4gpu.ElasticNet

    enet = Solver(glm_stop_early=False)
    print("h2o4gpu fit()")
    enet.fit(X, y)
    print("h2o4gpu predict()")
    print(enet.predict(X))
    print("h2o4gpu score()")
    print(enet.score(X,y))

    enet_wrapper = Solver(positive=True, random_state=1234)
    print("h2o4gpu scikit wrapper fit()")
    enet_wrapper.fit(X, y)
    print("h2o4gpu scikit wrapper predict()")
    print(enet_wrapper.predict(X))
    print("h2o4gpu scikit wrapper score()")
    print(enet_wrapper.score(X, y))

    from sklearn.linear_model.coordinate_descent import ElasticNet
    enet_sk = ElasticNet(positive=True, random_state=1234)
    print("Scikit fit()")
    enet_sk.fit(X, y)
    print("Scikit predict()")
    print(enet_sk.predict(X))
    print("Scikit score()")
    print(enet_sk.score(X, y))

    enet_sk_coef = csr_matrix(enet_sk.coef_, dtype=np.float32).toarray()

    print(enet_sk.coef_)

    print(enet_sk_coef)

    print(enet_wrapper.coef_)

    print(enet_sk.intercept_)
    print(enet_wrapper.intercept_)

    print(enet_sk.n_iter_)
    print(enet_wrapper.n_iter_)

    print("Coeffs, intercept, and n_iters should match")
    assert np.allclose(enet_wrapper.coef_, enet_sk_coef)
    assert np.allclose(enet_wrapper.intercept_, enet_sk.intercept_)
开发者ID:wamsiv,项目名称:h2o4gpu,代码行数:50,代码来源:test_elasticnet_sklearn_wrapper.py

示例13: test_check_input_false

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_check_input_false():
    X, y, _, _ = build_dataset(n_samples=20, n_features=10)
    X = check_array(X, order='F', dtype='float64')
    y = check_array(X, order='F', dtype='float64')
    clf = ElasticNet(selection='cyclic', tol=1e-8)
    # Check that no error is raised if data is provided in the right format
    clf.fit(X, y, check_input=False)
    # With check_input=False, an exhaustive check is not made on y but its
    # dtype is still cast in _preprocess_data to X's dtype. So the test should
    # pass anyway
    X = check_array(X, order='F', dtype='float32')
    clf.fit(X, y, check_input=False)
    # With no input checking, providing X in C order should result in false
    # computation
    X = check_array(X, order='C', dtype='float64')
    assert_raises(ValueError, clf.fit, X, y, check_input=False)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:18,代码来源:test_coordinate_descent.py

示例14: fit

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
 def fit(self,X,y):
     '''
     Fits ElasticNet Regression with kernelised features
     
     Parameters
     ----------
     X: array-like of size [n_samples, n_features]
        Matrix of explanatory variables
        
     y: array-like of size (n_samples,)
        Vector of dependent variable
     
     Returns
     -------
     obj: self
       self
     '''
     X,y = check_X_y(X,y, dtype = np.float64)
     K   = get_kernel(X, X, self.gamma, self.degree, self.coef0, self.kernel, 
                      self.kernel_params )
     model = ElasticNet(self.alpha, self.l1_ratio, self.fit_intercept,
                        self.normalize, self.precompute, self.max_iter,
                        self.copy_X, self.tol, self.warm_start, self.positive,
                        self.random_state, self.selection)
     self._model = model.fit(K,y)
     self.relevant_indices_ = np.where(self._model.coef_ != 0)[0]
     self.relevant_vectors_ = X[self.relevant_indices_,:]
     return self
开发者ID:Ferrine,项目名称:sklearn-bayes,代码行数:30,代码来源:kernel_models.py

示例15: test_check_input_false

# 需要导入模块: from sklearn.linear_model.coordinate_descent import ElasticNet [as 别名]
# 或者: from sklearn.linear_model.coordinate_descent.ElasticNet import fit [as 别名]
def test_check_input_false():
    X, y, _, _ = build_dataset(n_samples=20, n_features=10)
    X = check_array(X, order='F', dtype='float64')
    y = check_array(X, order='F', dtype='float64')
    clf = ElasticNet(selection='cyclic', tol=1e-8)
    # Check that no error is raised if data is provided in the right format
    clf.fit(X, y, check_input=False)
    X = check_array(X, order='F', dtype='float32')
    clf.fit(X, y, check_input=True)
    # Check that an error is raised if data is provided in the wrong dtype,
    # because of check bypassing
    assert_raises(ValueError, clf.fit, X, y, check_input=False)

    # With no input checking, providing X in C order should result in false
    # computation
    X = check_array(X, order='C', dtype='float64')
    assert_raises(ValueError, clf.fit, X, y, check_input=False)
开发者ID:chribsen,项目名称:simple-machine-learning-examples,代码行数:19,代码来源:test_coordinate_descent.py


注:本文中的sklearn.linear_model.coordinate_descent.ElasticNet.fit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。