首页 > 其他分享 >第13章 集成学习和随机森林

第13章 集成学习和随机森林

时间:2022-11-03 20:33:45浏览次数:51  
标签:集成 13 clf train 随机 test import voting sklearn

 

13-1什么是集成学习

 

 

 

 

 

 

 

 

 

Notbook 示例

 

 Notbook 源码

 

 1 集成学习
 2 [1]
 3 import numpy as np
 4 import matplotlib.pyplot as plt
 5 [2]
 6 from sklearn import datasets
 7 X, y = datasets.make_moons(n_samples=500,noise=0.3, random_state=42)
 8 [3]
 9 plt.scatter(X[y==0,0], X[y==0,1])
10 plt.scatter(X[y==1,0], X[y==1,1])
11 <matplotlib.collections.PathCollection at 0x2337fd4a160>
12 
13 [4]
14 from sklearn.model_selection import train_test_split
15 
16 X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
17 [5]
18 from sklearn.linear_model import LogisticRegression
19 
20 log_clf = LogisticRegression()
21 log_clf.fit(X_train, y_train)
22 log_clf.score(X_test, y_test)
23 0.864
24 [6]
25 from sklearn.svm import SVC
26 
27 svm_clf = SVC()
28 svm_clf.fit(X_train, y_train)
29 svm_clf.score(X_test, y_test)
30 0.896
31 [7]
32 from sklearn.tree import DecisionTreeClassifier
33 
34 dt_clf = DecisionTreeClassifier()
35 dt_clf.fit(X_train, y_train)
36 dt_clf.score(X_test, y_test)
37 0.84
38 [8]
39 y_predict1 = log_clf.predict(X_test)
40 y_predict2 = svm_clf.predict(X_test)
41 y_predict3 = dt_clf.predict(X_test)
42 [9]
43 y_predict = np.array( (y_predict1 + y_predict2 + y_predict3) >= 2, dtype='int')
44 [10]
45 y_predict[:10]
46 array([1, 0, 0, 1, 1, 1, 0, 0, 0, 0])
47 [11]
48 from sklearn.metrics import accuracy_score
49 
50 accuracy_score(y_test, y_predict)
51 0.904
52 Voting Classifier
53 [12]
54 from sklearn.ensemble import VotingClassifier
55 
56 voting_clf = VotingClassifier(estimators=[
57     ('log_clf', LogisticRegression()),
58     ('svm_clf',SVC()),
59     ('dt_clf', DecisionTreeClassifier())
60 ],voting='hard')
61 [13]
62 voting_clf.fit(X_train, y_train)
63 VotingClassifier(estimators=[('log_clf', LogisticRegression()),
64                              ('svm_clf', SVC()),
65                              ('dt_clf', DecisionTreeClassifier())])
66 [14]
67 voting_clf.score(X_test,y_test)
68 0.912

 

13-2 SoftVoting Classifier

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Notbook 示例

 

 

Notbook 源码

 1 Soft Voting
 2 [1]
 3 import numpy as np
 4 import matplotlib.pyplot as plt
 5 [2]
 6 from sklearn import datasets
 7 X, y = datasets.make_moons(n_samples=500,noise=0.3, random_state=42)
 8 [3]
 9 plt.scatter(X[y==0,0], X[y==0,1])
10 plt.scatter(X[y==1,0], X[y==1,1])
11 <matplotlib.collections.PathCollection at 0x1dbd04d9190>
12 
13 [4]
14 from sklearn.model_selection import train_test_split
15 
16 X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
17 Hard Voting Classifier
18 [5]
19 from sklearn.linear_model import LogisticRegression
20 from sklearn.svm import SVC
21 from sklearn.tree import DecisionTreeClassifier
22 from sklearn.ensemble import VotingClassifier
23 
24 voting_clf = VotingClassifier(estimators=[
25     ('log_clf', LogisticRegression()),
26     ('svm_clf',SVC()),
27     ('dt_clf', DecisionTreeClassifier())
28 ],voting='hard')
29 [6]
30 voting_clf.fit(X_train, y_train)
31 voting_clf.score(X_test,y_test)
32 0.912
33 Soft Voting Classifier
34 [7]
35 voting_clf2 = VotingClassifier(estimators=[
36     ('log_clf', LogisticRegression()),
37     ('svm_clf',SVC(probability=True)),
38     ('dt_clf', DecisionTreeClassifier())
39 ],voting='soft')
40 [8]
41 voting_clf2.fit(X_train, y_train)
42 voting_clf2.score(X_test,y_test)
43 0.92

 

 

 

标签:集成,13,clf,train,随机,test,import,voting,sklearn
From: https://www.cnblogs.com/Cai-Gbro/p/16855753.html

相关文章

  • 打印一个随机数。
    #include<stdio.h> #include<stdlib.h> #include<time.h>//引入一个时间函数。 intmain() { inta; srand((unsigned)time(NULL));//通过时间的变化......
  • go-gin集成jwt认证
    今天趁热打铁,把cookie/session/jwt集成go-gin框架的认证方式,一鼓作气全code一遍,lifeisshort,showyouthecode.示例目录结构:项目入口:packagemainimport( "gin......
  • 字节跳动开源数据集成引擎 BitSail 的演进历程与能力解析
    导读BitSail是字节跳动开源数据集成引擎,支持多种异构数据源间的数据同步,并提供离线、实时、全量、增量场景下全域数据集成解决方案,目前支撑了字节内部和火山引擎多个客户的......
  • 20201302姬正坤第十二章学习笔记
    Linux第十二章——块设备I/O和缓冲区管理块设备I/O缓冲区读写普通文件的算法依赖于两个关键操作,即:get_block和put_block,这两个操作将磁盘块读写到内存缓冲区中。I/O缓......
  • 【113】
    1668. 最大重复子字符串 给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word 的 ......
  • spring-boot-2.0.3之quartz集成,最佳实践
    开心一刻快过年了,大街上,爷爷在给孙子示范摔炮怎么放,嘴里还不停念叨:要像这样,用劲甩才能响。示范了一个,两个,三个...孙子终于忍不住了,抱着爷爷的腿哭起来:爷呀,你给我剩......
  • spring-boot-2.0.3之quartz集成,数据源问题,源码探究
    开心一刻着火了,他报警说:119吗,我家发生火灾了119问:在哪里?他说:在我家119问:具体点他说:在我家的厨房里119问:我说你现在的位置他说:我趴在桌子底下......
  • kindeditor集成Word导入
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action......
  • ORA-03113: end-of-file on communication channel Serial number: 5
    ORA-03113:end-of-fileoncommunicationchannel ProcessID:41880  SessionID:762Serialnumber:5---step1: 查看Alert日志 1.1Alter日志的路径查看[orac......
  • 集成无线收发器和 8 位 RISC MCU 的 SOC 芯片CI2454/CI2451参数-遥控玩具汽车方案
    前面小编给大家介绍了一款集成无线收发器和8位RISC(精简指令集)MCU的SOC芯片-CI2454/CI2451,今天就来讲讲它的优劣势和应用方案。优势1、它拥有RISC精简指令集架构,可以......