import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm
from sklearn.model_selection import train_test_split def load_data_regression():
'''
加载用于回归问题的数据集
'''
diabetes = datasets.load_diabetes() #使用 scikit-learn 自带的一个糖尿病病人的数据集
# 拆分成训练集和测试集,测试集大小为原始数据集大小的 1/4
return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) #支持向量机线性回归SVR模型
def test_LinearSVR(*data):
X_train,X_test,y_train,y_test=data
regr=svm.LinearSVR()
regr.fit(X_train,y_train)
print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
print('Score: %.2f' % regr.score(X_test, y_test)) # 生成用于回归问题的数据集
X_train,X_test,y_train,y_test=load_data_regression()
# 调用 test_LinearSVR
test_LinearSVR(X_train,X_test,y_train,y_test)

def test_LinearSVR_loss(*data):
'''
测试 LinearSVR 的预测性能随不同损失函数的影响
'''
X_train,X_test,y_train,y_test=data
losses=['epsilon_insensitive','squared_epsilon_insensitive']
for loss in losses:
regr=svm.LinearSVR(loss=loss)
regr.fit(X_train,y_train)
print("loss:%s"%loss)
print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
print('Score: %.2f' % regr.score(X_test, y_test)) # 调用 test_LinearSVR_loss
test_LinearSVR_loss(X_train,X_test,y_train,y_test)

def test_LinearSVR_epsilon(*data):
'''
测试 LinearSVR 的预测性能随 epsilon 参数的影响
'''
X_train,X_test,y_train,y_test=data
epsilons=np.logspace(-2,2)
train_scores=[]
test_scores=[]
for epsilon in epsilons:
regr=svm.LinearSVR(epsilon=epsilon,loss='squared_epsilon_insensitive')
regr.fit(X_train,y_train)
train_scores.append(regr.score(X_train, y_train))
test_scores.append(regr.score(X_test, y_test))
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(epsilons,train_scores,label="Training score ",marker='+' )
ax.plot(epsilons,test_scores,label= " Testing score ",marker='o' )
ax.set_title( "LinearSVR_epsilon ")
ax.set_xscale("log")
ax.set_xlabel(r"$\epsilon$")
ax.set_ylabel("score")
ax.set_ylim(-1,1.05)
ax.legend(loc="best",framealpha=0.5)
plt.show() # 调用 test_LinearSVR_epsilon
test_LinearSVR_epsilon(X_train,X_test,y_train,y_test)

def test_LinearSVR_C(*data):
'''
测试 LinearSVR 的预测性能随 C 参数的影响
'''
X_train,X_test,y_train,y_test=data
Cs=np.logspace(-1,2)
train_scores=[]
test_scores=[]
for C in Cs:
regr=svm.LinearSVR(epsilon=0.1,loss='squared_epsilon_insensitive',C=C)
regr.fit(X_train,y_train)
train_scores.append(regr.score(X_train, y_train))
test_scores.append(regr.score(X_test, y_test))
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(Cs,train_scores,label="Training score ",marker='+' )
ax.plot(Cs,test_scores,label= " Testing score ",marker='o' )
ax.set_title( "LinearSVR_C ")
ax.set_xscale("log")
ax.set_xlabel(r"C")
ax.set_ylabel("score")
ax.set_ylim(-1,1.05)
ax.legend(loc="best",framealpha=0.5)
plt.show() # 调用 test_LinearSVR_C
test_LinearSVR_C(X_train,X_test,y_train,y_test)

吴裕雄 python 机器学习——支持向量机线性回归SVR模型的更多相关文章

  1. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  2. 吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  3. 吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  4. 吴裕雄 python 机器学习——层次聚类AgglomerativeClustering模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  5. 吴裕雄 python 机器学习——密度聚类DBSCAN模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  6. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  7. 吴裕雄 python 机器学习——KNN分类KNeighborsClassifier模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  8. 吴裕雄 python 机器学习——半监督学习LabelSpreading模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...

  9. 吴裕雄 python 机器学习——线性回归模型

    import numpy as np from sklearn import datasets,linear_model from sklearn.model_selection import tra ...

随机推荐

  1. 假期学习【九】首都之窗百姓信件爬取代码优化以及处理 2020.2.7 Python

    今天对爬取程序进行了优化,修正了错误. 遇到了两个问题与错误: 1.忘记了对文件的读写设置格式,导致导入Hive数据库无法正常显示以及写入. 2.文件的读写操作有误导致数据量仅有应该有的1/2 不完整 ...

  2. 视频中“5s后可跳过广告” 设计目的

    来源:https://wen.woshipm.com/question/detail/0quoes.html 1.保证你在看.用户关掉广告这5秒内,他的眼睛会盯着屏幕,因为他知道5秒之后就能跳过广告. ...

  3. 【Python】1.PyQT5界面初尝试

    1->通过pycharm打开QTdesigner.创建新Form. 2->选择Widget创建 3->不添加任何控件 点击保存 pycharm列表如下 后缀名位ui的  即QTdes ...

  4. url 获取 geoserver 中对应的style

    http://userName:password@127.0.0.1:7093/geoserver/rest/workspaces/FAST/styles/AVG_RSRP.sld

  5. vue 截取字符串

    let str = 'abcdef'; str = str.slice();//返回整个字符串 abcdef str = str.substring();//返回整个字符串 abcdef str = ...

  6. DOM操作节点对象集合

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. SpringBoot整合WEB开发--(七)注册拦截器

    1.创建一个拦截器类实现HandlerInterceptor接口,重写其中的3个方法,这拦截器中方法的执行顺序为:preHandle--Controller--postHandle--afterCom ...

  8. batchsize用法 以及实现前向运算的矩阵乘法方式

    batchsize用法 以及实现前向运算的矩阵乘法方式 待办 使用batchsize可以把矩阵缩小,采用矩阵乘法的形式可以一次计算多个经过神经网络的结果,然后通过交叉熵进行计算总的损失得到结果. 所以 ...

  9. go语言 RSA数字签名和验证签名

    package main import ( "crypto" "crypto/rand" "crypto/rsa" "crypto ...

  10. 常用的 19 条 MySQL 优化

    一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...