You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

627 lines
20 KiB

5 years ago
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>调参 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="prev" href="fit and predict.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
简介
</a>
</li>
5 years ago
<li class="chapter " data-level="1.2" data-path="../machine_learning.html">
<a href="../machine_learning.html">
机器学习概述
</a>
</li>
<li class="chapter " data-level="1.3" data-path="../algorithm.html">
<a href="../algorithm.html">
常见机器学习算法
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../kNN.html">
<a href="../kNN.html">
近朱者赤近墨者黑-kNN
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../linear_regression.html">
<a href="../linear_regression.html">
最简单的回归算法-线性回归
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../logistic_regression.html">
<a href="../logistic_regression.html">
使用回归的思想进行分类-逻辑回归
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="../decision_tree.html">
<a href="../decision_tree.html">
最接近人类思维的算法-决策树
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="../random_forest.html">
<a href="../random_forest.html">
群众的力量是伟大的-随机森林
</a>
</li>
<li class="chapter " data-level="1.3.6" data-path="../kMeans.html">
<a href="../kMeans.html">
物以类聚人以群分-kMeans
</a>
</li>
<li class="chapter " data-level="1.3.7" data-path="../AGNES.html">
<a href="../AGNES.html">
以距离为尺-AGNES
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../metrics.html">
<a href="../metrics.html">
模型评估指标
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../classification_metrics.html">
<a href="../classification_metrics.html">
分类性能评估指标
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../regression_metrics.html">
<a href="../regression_metrics.html">
回归性能评估指标
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../cluster_metrics.html">
<a href="../cluster_metrics.html">
聚类性能评估指标
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../sklearn.html">
<a href="../sklearn.html">
使用sklearn进行机器学习
</a>
</li>
<li class="chapter " data-level="1.6" >
<span>
综合实战案例
</span>
<ul class="articles">
<li class="chapter " data-level="1.6.1" >
5 years ago
<span>
泰坦尼克生还预测
</span>
<ul class="articles">
5 years ago
<li class="chapter " data-level="1.6.1.1" data-path="introduction.html">
5 years ago
<a href="introduction.html">
简介
</a>
</li>
5 years ago
<li class="chapter " data-level="1.6.1.2" data-path="EDA.html">
5 years ago
<a href="EDA.html">
探索性数据分析(EDA)
</a>
</li>
5 years ago
<li class="chapter " data-level="1.6.1.3" data-path="feature engerning.html">
5 years ago
<a href="feature engerning.html">
特征工程
</a>
</li>
5 years ago
<li class="chapter " data-level="1.6.1.4" data-path="fit and predict.html">
5 years ago
<a href="fit and predict.html">
构建模型进行预测
</a>
</li>
5 years ago
<li class="chapter active" data-level="1.6.1.5" data-path="tuning.html">
5 years ago
<a href="tuning.html">
调参
</a>
</li>
</ul>
</li>
5 years ago
<li class="chapter " data-level="1.6.2" >
5 years ago
<span>
使用强化学习玩乒乓球游戏
</span>
<ul class="articles">
5 years ago
<li class="chapter " data-level="1.6.2.1" data-path="../pingpong/what is reinforce learning.html">
5 years ago
<a href="../pingpong/what is reinforce learning.html">
什么是强化学习
</a>
</li>
5 years ago
<li class="chapter " data-level="1.6.2.2" data-path="../pingpong/Policy Gradient.html">
5 years ago
<a href="../pingpong/Policy Gradient.html">
Policy Gradient原理
</a>
</li>
5 years ago
<li class="chapter " data-level="1.6.2.3" data-path="../pingpong/coding.html">
5 years ago
<a href="../pingpong/coding.html">
使用Policy Gradient玩乒乓球游戏
</a>
</li>
</ul>
</li>
5 years ago
</ul>
</li>
<li class="chapter " data-level="1.7" data-path="../recommand.html">
<a href="../recommand.html">
实训推荐
</a>
</li>
5 years ago
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >调参</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="&#x8C03;&#x53C2;">&#x8C03;&#x53C2;</h1>
<p>&#x5F88;&#x591A;&#x673A;&#x5668;&#x5B66;&#x4E60;&#x7B97;&#x6CD5;&#x6709;&#x5F88;&#x591A;&#x53EF;&#x4EE5;&#x8C03;&#x6574;&#x7684;&#x53C2;&#x6570;(&#x5373;&#x8D85;&#x53C2;&#x6570;)&#xFF0C;&#x4F8B;&#x5982;&#x6211;&#x4EEC;&#x7528;&#x7684;&#x968F;&#x673A;&#x68EE;&#x6797;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x6307;&#x5B9A;&#x68EE;&#x6797;&#x4E2D;&#x6709;&#x591A;&#x5C11;&#x68F5;&#x51B3;&#x7B56;&#x6811;&#xFF0C;&#x6CA1;&#x68F5;&#x51B3;&#x7B56;&#x6811;&#x7684;&#x6700;&#x5927;&#x6DF1;&#x5EA6;&#x7B49;&#x3002;&#x8FD9;&#x4E9B;&#x8D85;&#x53C2;&#x6570;&#x90FD;&#x6216;&#x591A;&#x6216;&#x5C11;&#x7684;&#x4F1A;&#x5F71;&#x54CD;&#x8FD9;&#x6A21;&#x578B;&#x7684;&#x6027;&#x80FD;&#x3002;&#x90A3;&#x4E48;&#x600E;&#x6837;&#x624D;&#x80FD;&#x627E;&#x5230;&#x5408;&#x9002;&#x7684;&#x8D85;&#x53C2;&#x6570;&#xFF0C;&#x6765;&#x8BA9;&#x6211;&#x4EEC;&#x7684;&#x6A21;&#x578B;&#x6027;&#x80FD;&#x8FBE;&#x5230;&#x6BD4;&#x8F83;&#x597D;&#x7684;&#x6548;&#x679C;&#x5462;&#xFF1F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7F51;&#x683C;&#x641C;&#x7D22;!</p>
<p>&#x7F51;&#x683C;&#x641C;&#x7D22;&#x7684;&#x610F;&#x601D;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x904D;&#x5386;&#x6240;&#x6709;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x5C1D;&#x8BD5;&#x7684;&#x53C2;&#x6570;&#x7EC4;&#x5408;&#xFF0C;&#x770B;&#x770B;&#x54EA;&#x4E2A;&#x53C2;&#x6570;&#x7EC4;&#x5408;&#x7684;&#x6027;&#x80FD;&#x6700;&#x9AD8;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x7EC4;&#x53C2;&#x6570;&#x7EC4;&#x5408;&#x5C31;&#x662F;&#x6A21;&#x578B;&#x7684;&#x6700;&#x4F73;&#x53C2;&#x6570;&#x3002;</p>
<p>sklearn &#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x7F51;&#x683C;&#x641C;&#x7D22;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x5F88;&#x65B9;&#x4FBF;&#x7684;&#x8FDB;&#x884C;&#x7F51;&#x683C;&#x641C;&#x7D22;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.model_selection <span class="hljs-keyword">import</span> GridSearchCV
<span class="hljs-comment"># &#x60F3;&#x8981;&#x8C03;&#x6574;&#x7684;&#x53C2;&#x6570;&#x7684;&#x5B57;&#x5178;&#xFF0C;&#x5B57;&#x5178;&#x7684;key&#x4E3A;&#x53C2;&#x6570;&#x540D;&#x5B57;&#xFF0C;value&#x4E3A;&#x60F3;&#x8981;&#x5C1D;&#x8BD5;&#x53C2;&#x6570;&#x503C;</span>
param_grid = {<span class="hljs-string">&apos;n_estimators&apos;</span>: [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">50</span>, <span class="hljs-number">100</span>, <span class="hljs-number">150</span>, <span class="hljs-number">200</span>],<span class="hljs-string">&apos;max_depth&apos;</span>: [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>, <span class="hljs-number">25</span>, <span class="hljs-number">30</span>]}
<span class="hljs-comment"># &#x91C7;&#x7528;5&#x6298;&#x9A8C;&#x8BC1;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x7F51;&#x683C;&#x641C;&#x7D22;&#xFF0C;&#x5206;&#x7C7B;&#x5668;&#x4E3A;&#x968F;&#x673A;&#x68EE;&#x6797;</span>
grid_search = GridSearchCV(RandomForestClassifier(), param_grid, cv=<span class="hljs-number">5</span>)
grid_search.fit(X_train, Y_train)
<span class="hljs-comment"># &#x6253;&#x5370;&#x6700;&#x4F73;&#x53C2;&#x6570;&#x7EC4;&#x5408;</span>
print(grid_search.best_params_)
<span class="hljs-comment"># &#x6253;&#x5370;&#x6700;&#x4F73;&#x53C2;&#x6570;&#x7EC4;&#x5408;&#x65F6;&#x6A21;&#x578B;&#x7684;&#x6700;&#x4F73;&#x6027;&#x80FD;</span>
print(grid_search.best_score_)
</code></pre>
<p><img src="../img/58.jpg" alt=""></p>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x7ECF;&#x8FC7;&#x8C03;&#x53C2;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x968F;&#x673A;&#x68EE;&#x6797;&#x6A21;&#x578B;&#x7684;&#x6027;&#x80FD;&#x63D0;&#x9AD8;&#x5230;&#x4E86; 0.8323 &#xFF0C;&#x63D0;&#x5347;&#x4E86;&#x63A5;&#x8FD1; 1% &#x7684;&#x51C6;&#x786E;&#x7387;&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6700;&#x4F73;&#x53C2;&#x6570;&#x6784;&#x9020;&#x968F;&#x673A;&#x68EE;&#x6797;&#xFF0C;&#x5E76;&#x5BF9;&#x6D4B;&#x8BD5;&#x96C6;&#x6D4B;&#x8BD5;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;&#x6D4B;&#x8BD5;&#x96C6;&#x7684;&#x51C6;&#x786E;&#x7387;&#x8FBE;&#x5230;&#x4E86; 0.8525&#x3002;</p>
<pre><code class="lang-python">Y_train = data[<span class="hljs-string">&apos;Survived&apos;</span>]
X_train = data.drop([<span class="hljs-string">&apos;Survived&apos;</span>], axis=<span class="hljs-number">1</span>)
Y_test = test_data[<span class="hljs-string">&apos;Survived&apos;</span>]
X_test = test_data.drop([<span class="hljs-string">&apos;Survived&apos;</span>], axis=<span class="hljs-number">1</span>)
clf = RandomForestClassifier(n_estimators=<span class="hljs-number">50</span>, max_depth=<span class="hljs-number">5</span>)
clf.fit(X_train, Y_train)
predict = clf.predict(X_test)
print(accuracy_score(Y_test, predict))
</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="fit and predict.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 构建模型进行预测">
<i class="fa fa-angle-left"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
5 years ago
gitbook.page.hasChanged({"page":{"title":"调参","level":"1.6.1.5","depth":3,"next":{"title":"使用强化学习玩乒乓球游戏","level":"1.6.2","depth":2,"ref":"","articles":[{"title":"什么是强化学习","level":"1.6.2.1","depth":3,"path":"pingpong/what is reinforce learning.md","ref":"./pingpong/what is reinforce learning.md","articles":[]},{"title":"Policy Gradient原理","level":"1.6.2.2","depth":3,"path":"pingpong/Policy Gradient.md","ref":"./pingpong/Policy Gradient.md","articles":[]},{"title":"使用Policy Gradient玩乒乓球游戏","level":"1.6.2.3","depth":3,"path":"pingpong/coding.md","ref":"./pingpong/coding.md","articles":[]}]},"previous":{"title":"构建模型进行预测","level":"1.6.1.4","depth":3,"path":"titanic/fit and predict.md","ref":"./titanic/fit and predict.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["katex"],"pluginsConfig":{"katex":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"titanic/tuning.md","mtime":"2019-07-05T01:25:15.418Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-06T07:31:21.537Z"},"basePath":"..","book":{"language":""}});
5 years ago
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>