Sklearn库例子2:分类——线性回归分类(Line Regression )例子

时间:2022-07-25 20:35:00

线性回归:通过拟合线性模型的回归系数W =(w_1,…,w_p)来减少数据中观察到的结果和实际结果之间的残差平方和,并通过线性逼近进行预测。

从数学上讲,它解决了下面这个形式的问题:      Sklearn库例子2:分类——线性回归分类(Line Regression )例子

LinearRegression()模型在Sklearn.linear_model下,他主要是通过fit(x,y)的方法来训练模型,其中x为数据的属性,y为所属类型。线性模型的回归系数W会保存在他的coef_方法中。

例如:

>>> from sklearn import linear_model
>>> clf = linear_model.LinearRegression()
>>> clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
>>> clf.coef_
array([ 0.5, 0.5])

实例:

使用的数据集为Sklearn.dataset.load_diabetes()一个关于糖尿病的数据集。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx0AAAEWCAIAAACMo20uAAAgAElEQVR4nO3dz2rrxvvH8d+NGINXvZOAjbPpdQQORxjau8giEDvQTVff7fkuDpw46aJ3kFKyCIm+dNtVdoXCQb+F/j0zekYaWY/8L+8XoqSKPBqNJetzZsbK/2UAAACw8H+HrgAAAMCZIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFcBAADYIFf1k6bpCRU7noNU2HynJ9fsAIAjZ5Kr0vViNt9Y36IeVpPpantEZY5zmE6xj8l0ljxYlFof6WMyvVy31brvTkdoh+73xXynHQVuE6M3wljnu2n+QgBAD+SqeCeaqzqRqxq/21xOkke7fR2H9G6+uCNZAcCoxs5V6Xoxm0xnk+lsIj/T07v5tFzv3NQfk2Ll5XoTuNfqr31Mppfrh+pX8p/mnWWm60X+q+qFzWqLNcmjn0XqWOAV1VKrRvtUxW7U7QMt6VCPVHRUBJtO7lS2T2c7BCuWbi4nwaNufV+USjZ2Gjh/AjuNOQqvYlUjxJwbLYcT+3K15tpK2e2kFt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1UDeHda6K2mk+muZ8va4YSpOppc5A7vz0P3//QawPz23662fv+3rFaKAoswhe+eCjMm+93MCft65VBgCAMzfm9wG9aNV43MA2kTOrZAqptm99zkJbrnJ36n4BsPmorWbdhF65qpw+VT1nQd++c6dZ5j6xU85PkutFZhKPNqhzTPeccW86lLuxeIzCn9VApBxArHYafs6CCFXbxOipZgAAHCX+jg0AAIANchUAAIANchUAAIANchUAAIANchUAAIANchUAAIANchUAAIANg1z1BgAAcI4OkKve//n3XJfhjQMAAD4OchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAICNMXLVt6vpbOIvyZe4KPPl0+zq6+ETVVyuekyms8niLu3abpvMkofhLf2Y+K262g4v1S3fop4AAHxQe8tVs8n89ikiVE2m55artslsYpNX/NyzTaJS3c7lAwCAXsbLVbKDyl/zdLOs8laZol6u5yKEffpWrqle9e2qDmf5r5ZXn/JyltfP5cZfE6WH7Pn2Quk5a9YzJlel60VRVPLg5qqHlciRl+vU2XgynU2Sx8Bmvm0ym2+av2jknodV2WXl/spZf7l+uJs3d5eWK+tk9phMZ8lG2xgAAEQ4QK7KO6XkcnHzslOukj1h7pq6EK3/rChkh1zl5iQnlzQG6RZ3qZKr1M0iKf1V5f+25Kp6uFD0b1Xbp+tFleFCGwMAgCh7HAfMU07edVSPCTrhyR0HjMhVRXKq11zcvLz/8+973muVb+zvccA4YNHHUySPdHMZCEaNgKKPrzmbRWjOr3IiUShXKevrDfzytUIAAECUPeWqespUPU4nl+X18w65qnjVu7JGbuzVJ3YGvZKr8iG8fDgvy7T5VTL6tOQqZbMIfn9VurksR+v65ap0cxmKg+QqAAB2NvY4YJFpim6k0XOVurE3v8p7oVGuqqYrteeq8GYRmvPKqzXkKgAADm/8+VVlpinSUuuonJariv8tprrvlquCuzAbB8x/Lkbl3M1krmrZLIJZrmIcEACAMexj3nr57b/gvPUqANW/+vStdcs+ucrvrPL60ozmrTvf8lP6q4qOrvBmEbRxwKLbKV0vqo60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jjrL9dp9HEBAMLIVSeTq6o7nBgbqpf5Jk03l86Al5tOOl8ux4DE3VRdKe7ETpdGlUV2y1XVGN8sWHPtANuPqCNXBQ8hmCrCu9OPItDxE9tEjcYPtFXdL5W5Q3uN/OQfabG+67gAAN3IVaeZq7QBmh65qnV8p5x+5Mz4cVea56oiJRT/21LzUK6KGLDzj3FIrtJ3px/FgFwlatuYYea3lUmuYuAPAIYhV51ergreg1vHASNv4XKPjUTi34nNxgG9cBPIVdHjgO1HkWXR44D6KFhc+xuMA6rHEtpLVK7qNQ4IAOiNXHWCucqbUv2wmijzjuUk5biXy9tqdZNWV8bNW++Xq5y9ON9b9GoeOsCuBnFEzlsPzC6KbECnhykvSnS59ZxfFXxHyr3E5Sptprz7birNCACIRa46xVyVOd+Hd25+7mMIghFBf7l4AIH3jTNvZcxzFro7Y4oJPclj5j/7oKXTSD3A4BHJXUju86tCz1lombXd2YDyKOSTEeTTDbqbqOsdEXuJ7K9KVtqbpb2b7nEx0QoAYpCrTiJX7cSfOXR2zv4AjR3Pk0gB4GyRq84oVzkDN3t4mvnenf0BjotcBQCjI1edUa5yHwFwlpnj7A9wTOQqABgdueqschUAADggchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2CBXkasAAIANchW5CgAA2DDIVW8AAADn6AC5CgAAABm5CgAAwAq5CgAAwAa5CgAAwAa5CgAAwAa5CgAAwAa5CgAAwMb/A5fFRufCNYrrAAAAAElFTkSuQmCC" alt="" width="677" height="236" />

为了说明这个回归技术的一个二维图,例子仅仅使用了糖尿病数据集的第一个特征。

代码如下:

# -*- encoding:utf-8 -*-
"""
Line Regression Example
DataBase:diavetes
""" import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model
import time a=time.time()
####加载数据集
diabetes=datasets.load_diabetes() ####仅仅使用一个特征:
diabetes_X=diabetes.data[:,np.newaxis,2] ###s数据划分训练集和测试集
diabetes_X_train=diabetes_X[:-20]
diabetes_X_test=diabetes_X[-20:] ###目标划分为训练集和测试集
diabetes_y_train=diabetes.target[:-20]
diabetes_y_test=diabetes.target[-20:] ###训练模型
regr=linear_model.LogisticRegression()
regr.fit(diabetes_X_train,diabetes_y_train) ###回归系数
print('Coefficients:\n',regr.coef_) ###均方误差
print('the mean sqare error:%.2f' %np.mean((regr.predict(diabetes_X_test)-diabetes_y_test)**2))
print('Variance score:%.2f' %regr.score(diabetes_X_test,diabetes_y_test))
##散点图
plt.scatter(diabetes_X_test,diabetes_y_test,color='black')
plt.plot(diabetes_X_test,regr.predict(diabetes_X_test),color='blue',linewidth=3)
plt.xticks()
plt.yticks()
b=time.time()
print('the running time is %.2f' %(b-a))
plt.show()

实验结果:

Coefficients:
[ 938.23786125]
Residual sum of squares: 2548.07
Variance score: 0.47
the running time is 0.31

Sklearn库例子2:分类——线性回归分类(Line Regression )例子