Ridge Regression 岭回归

# coding:utf-8
import sklearn.datasets
import sklearn.linear_model
import numpy.random
import numpy.linalg
import matplotlib.pyplot

if __name__ == "__main__":
    # Load boston dataset
    boston = sklearn.datasets.load_boston()

    # Split the dataset with sampleRatio
    sampleRatio = 0.5
    n_samples = len(boston.target)
    sampleBoundary = int(n_samples * sampleRatio)

    # Shuffle the whole data
    shuffleIdx = range(n_samples)
    numpy.random.shuffle(shuffleIdx)

    # Make the training data
    train_features = boston.data[shuffleIdx[:sampleBoundary]]
    train_targets = boston.target[shuffleIdx[:sampleBoundary]]

    # Make the testing data
    test_features = boston.data[shuffleIdx[sampleBoundary:]]
    test_targets = boston.target[shuffleIdx[sampleBoundary:]]

    # Train with Cross Validation
    ridgeRegression = sklearn.linear_model.RidgeCV(alphas=[0.01, 0.05, 0.1, 0.5, 1.0, 10.0])
    # 这个地方使用RidgeCV 直接交叉验证出我需要试验的几个惩罚因子,它会帮我选择这些里面在集内测试表现最优的一个参数。后面的输出选择了0.1.

    ridgeRegression.fit(train_features, train_targets)
    print("Alpha = ", ridgeRegression.alpha_)

    # Predict
    predict_targets = ridgeRegression.predict(test_features)

    # Evaluation
    n_test_samples = len(test_targets)
    X = range(n_test_samples)
    error = numpy.linalg.norm(predict_targets - test_targets, ord = 1) / n_test_samples
    print("Ridge Regression (Boston) Error: %.2f" %(error))

    # Draw
    matplotlib.pyplot.plot(X, predict_targets, 'r--', label = 'Predict Price')
    matplotlib.pyplot.plot(X, test_targets, 'g:', label = 'True Price')
    legend = matplotlib.pyplot.legend()
    matplotlib.pyplot.title("Ridge Regression (Boston)")
    matplotlib.pyplot.ylabel("Price (1000 U.S.D)")
    matplotlib.pyplot.savefig("Ridge Regression (Boston).png", format= 'png')
    matplotlib.pyplot.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值