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. PM2的参数配置

    https://github.com/jawil/blog/issues/7 配置项: name  应用进程名称:script  启动脚本路径:cwd  应用启动的路径,关于script与cwd的区别 ...

  2. 如何在K3查找BOS单据在哪个子系统中

    select FFunctionID,* from ICClassType where FName_CHS like '%采购订单%'select FSubSysID,* from t_DataFlo ...

  3. Python 之路Day13

    匿名函数 一行函数 lambda == def -- 关键字 lambda x:x x 是普通函数的形参(位置,关键字……)可以不接收参数,可以不写 :x 是普通函数的函数值(只能返回一个数据类型), ...

  4. flask入门(三)

    表单 request.form 能获取POST 请求中提交的表单数据.但是这样不太安全,容易受到恶意攻击.对此,flask有一个flask-wtf扩展,用于避免这一情况 在虚拟环境下用pip inst ...

  5. vue学习指南:第十四篇(详细) - Vue的 路由 第四篇 ( 路由的导航守卫 )

    导航守卫 一.全局导航守卫 1. 全局导航守卫,把方法给 router,只要路由发生改变跳转都会触发这个函数 2. 每个路由 都有一个 meta 3. 全局导航守卫分两种: 1. 全局前置导航守卫:路 ...

  6. AcWing 1010. 拦截导弹

    //贪心加dp #include<iostream> using namespace std ; ; int n; int q[N]; int f[N]; int g[N];//存每个序列 ...

  7. EF CodeFirst 之 Fluent API

    如何访问Fluent API: 在自定义上下文类中重写OnModelCreating方法,在方法内调用. 注:用法基本一样,配置类中的this就相当于modelBuilder.Entity<Pe ...

  8. [BJOI2012]连连看

    Description Luogu4134 Solution \(l,r \le 1000\),暴力枚举是否能匹配.这是一个选匹配的问题,所以直接网络流,原图不一定是二分图咋办?拆点啊!然后直接做就行 ...

  9. Mybaits的中的对象映射(包含仅有基本数据类型的属性的和对象类型的属性的)

    转:https://blog.csdn.net/cjt20100/article/details/46547617. 1 constructor – 用来将结果反射给一个实例化好的类的构造器    a ...

  10. Windows7自定义主题

    一.破解主题限制 Windows系统默认只能允许用户使用系统自带主题(非壁纸),即使用户安装了第三方主题,Windows也会限制很多地方,导致第三方主题用起来怪怪的. 故此,想要一个可以自定义主题的W ...