美文网首页
线性回归,岭回归,套索回归代码实践

线性回归,岭回归,套索回归代码实践

作者: 万州客 | 来源:发表于2022-04-25 19:45 被阅读0次

    代码操练起来就好~~

    一,代码

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    from sklearn.datasets import make_regression
    from sklearn.model_selection import train_test_split
    from sklearn.datasets import load_diabetes
    from sklearn.datasets import load_boston
    from sklearn.linear_model import Ridge
    from sklearn.linear_model import Lasso
    from sklearn.linear_model import SGDClassifier
    
    '''
    
    X = [[1], [4], [3]]
    y = [3, 5, 3]
    lr = LinearRegression().fit(X, y)
    z = np.linspace(0, 5, 20)
    plt.scatter(X, y, s=80)
    plt.plot(z, lr.predict(z.reshape(-1, 1)), c='k')
    plt.title("Straight Line")
    plt.show()
    print('y = {:.3f}'.format(lr.coef_[0]), 'x', '+ {:.3f}'.format(lr.intercept_))
    
    X, y = make_regression(n_samples=50, n_features=1, n_informative=1,
                           noise=50, random_state=1)
    reg = LinearRegression()
    reg.fit(X, y)
    z = np.linspace(-3, 3, 200).reshape(-1, 1)
    plt.scatter(X, y, c='b', s=60)
    plt.plot(z, reg.predict(z), c='k')
    plt.title("Linear Regression")
    plt.show()
    print('直线的系数是: {:.2f}'.format(reg.coef_[0]))
    print('直线的截距是: {:.2f}'.format(reg.intercept_))
    
    X, y = make_regression(
        n_samples=100, n_features=2, n_informative=2, random_state=38
    )
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    lr = LinearRegression().fit(X_train, y_train)
    print('lr.coef_: {}'.format(lr.coef_[:]))
    print('or.interceft: {}'.format(lr.intercept_))
    print('训练数据得分:{:.2f}'.format(lr.score(X_train, y_train)))
    print('测试数据得分:{:.2f}'.format(lr.score(X_test, y_test)))
    X, y = load_diabetes().data, load_diabetes().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    lr = LinearRegression().fit(X_train, y_train)
    print('lr.coef_: {}'.format(lr.coef_[:]))
    print('or.interceft: {}'.format(lr.intercept_))
    print('训练数据得分:{:.2f}'.format(lr.score(X_train, y_train)))
    print('测试数据得分:{:.2f}'.format(lr.score(X_test, y_test)))
    X, y = load_boston().data, load_boston().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    ridge = Ridge(alpha=0.1).fit(X_train, y_train)
    print('岭回归训练数据得分:{:.2f}'.format(ridge.score(X_train, y_train)))
    print('岭回归测试数据得分:{:.2f}'.format(ridge.score(X_test, y_test)))
    '''
    X, y = load_diabetes().data, load_diabetes().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    lasso = Lasso(alpha=0.0001, max_iter=100000).fit(X_train, y_train)
    print('套索回归训练数据得分:{:.2f}'.format(lasso.score(X_train, y_train)))
    print('套索回归测试数据得分:{:.2f}'.format(lasso.score(X_test, y_test)))
    print('套索回归使用的特征数:{}'.format(np.sum(lasso.coef_ != 0)))
    
    

    二,输出

    C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/Code/Metis-Org/app/service/time_series_detector/algorithm/ai_test.py
    套索回归训练数据得分:0.53
    套索回归测试数据得分:0.46
    套索回归使用的特征数:10
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:线性回归,岭回归,套索回归代码实践

          本文链接:https://www.haomeiwen.com/subject/kzkqyrtx.html