import scipy

from sklearn.datasets import load_digits
from sklearn.metrics import classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV #模型选择参数优化随机搜索寻优RandomizedSearchCV模型
def test_RandomizedSearchCV():
'''
测试 RandomizedSearchCV 的用法。使用 LogisticRegression 作为分类器,主要优化 C、multi_class 等参数。其中 C 的分布函数为指数分布
'''
### 加载数据
digits = load_digits()
X_train,X_test,y_train,y_test=train_test_split(digits.data, digits.target,test_size=0.25,random_state=0,stratify=digits.target)
#### 参数优化 ######
tuned_parameters ={ 'C': scipy.stats.expon(scale=100), # 指数分布
'multi_class': ['ovr','multinomial']}
clf=RandomizedSearchCV(LogisticRegression(penalty='l2',solver='lbfgs',tol=1e-6),tuned_parameters,cv=10,scoring="accuracy",n_iter=100)
clf.fit(X_train,y_train)
print("Best parameters set found:",clf.best_params_)
print("Randomized Grid scores:")
# for params, mean_score, scores in clf.fit_params,clf.mean_score,clf.score:
# print("\t%0.3f (+/-%0.03f) for %s" % (mean_score, scores() * 2, params))
# print("\t%0.3f (+/-%0.03f) for %s" % (clf.mean_score,clf.score * 2, clf.fit_params))
print(clf) print("Optimized Score:",clf.score(X_test,y_test))
print("Detailed classification report:")
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred)) #调用RandomizedSearchCV()
test_RandomizedSearchCV()

吴裕雄 python 机器学习——模型选择参数优化随机搜索寻优RandomizedSearchCV模型的更多相关文章

  1. 吴裕雄 python 机器学习——模型选择参数优化暴力搜索寻优GridSearchCV模型

    import scipy from sklearn.datasets import load_digits from sklearn.metrics import classification_rep ...

  2. 吴裕雄 python 机器学习——模型选择验证曲线validation_curve模型

    import numpy as np import matplotlib.pyplot as plt from sklearn.svm import LinearSVC from sklearn.da ...

  3. 吴裕雄 python 机器学习——模型选择学习曲线learning_curve模型

    import numpy as np import matplotlib.pyplot as plt from sklearn.svm import LinearSVC from sklearn.da ...

  4. 吴裕雄 python 机器学习——模型选择回归问题性能度量

    from sklearn.metrics import mean_absolute_error,mean_squared_error #模型选择回归问题性能度量mean_absolute_error模 ...

  5. 吴裕雄 python 机器学习——模型选择分类问题性能度量

    import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC from sklearn.datasets ...

  6. 吴裕雄 python 机器学习——模型选择数据集切分

    import numpy as np from sklearn.model_selection import train_test_split,KFold,StratifiedKFold,LeaveO ...

  7. 吴裕雄 python 机器学习——模型选择损失函数模型

    from sklearn.metrics import zero_one_loss,log_loss def test_zero_one_loss(): y_true=[1,1,1,1,1,0,0,0 ...

  8. 吴裕雄 python 机器学习——分类决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

  9. 吴裕雄 python 机器学习——K均值聚类KMeans模型

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

随机推荐

  1. JAVA 注解教程(四)Java 预置的注解

    @Deprecated 这个元素是用来标记过时的元素,想必大家在日常开发中经常碰到.编译器在编译阶段遇到这个注解时会发出提醒警告,告诉开发者正在调用一个过时的元素比如过时的方法.过时的类.过时的成员变 ...

  2. 实用 SQL 语句

    1. 创建 1.1 创建数据库 语法:create database db_name 示例:创建应用数据库 awesome_app sqlcreate database `awesome_app` 复 ...

  3. 记一次和“N+1”的擦肩而过

    这周五,就是昨天,部门走了3个人.他们是"被离职"的,从被通知到走人,只过了一周左右.一开始经理和他们谈的时候,说有没有赔偿不能确定,然后到周四左右,上面的人可能是等急了,才说年终 ...

  4. BigInteger和BigDecimal的基本用法

    整型大数 BigInteger: import java.math.BigInteger; import java.util.Scanner; public class Main { public s ...

  5. HTML 5 视频直播一站式扫盲(转载)

    http://www.alloyteam.com/2016/05/h5-camera-literacy/

  6. linux中systemctl详细理解及常用命令

    linux中systemctl详细理解及常用命令 https://blog.csdn.net/skh2015java/article/details/94012643 一.systemctl理解 Li ...

  7. jsp页面直接读取mysql数据库数据显示

    jsp页面直接读取mysql数据库数据显示: <%@page import="java.sql.ResultSet"%> <%@page import=" ...

  8. idea中运行ssm 或springboot项目时,project Structure的配置

    ctrl+alt+shift+s进入 project Structure 首先是project选项 Modules 标明source testsource 以及 resource 和 testreso ...

  9. 【转载】Win7下如何使用GCC编译器

    转自:http://jingyan.baidu.com/article/c275f6bacc0126e33c756771.html 双击GCC安装包,mingw-get-setup.exe,点击Ins ...

  10. python接口自动化测试 - requests库的post请求进行文件下载

    前言 之前讲了文件上传,当然就有文件下载啦 文件下载操作步骤 极其简单,将二进制格式的响应内容存进本地文件中,根据需要下载的文件的格式来写文件名即可 down_url = 'https://www.i ...