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.

657 lines
25 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="next" href="tuning.html" />
<link rel="prev" href="feature engerning.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 active" 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 " 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="&#x6784;&#x5EFA;&#x6A21;&#x578B;&#x8FDB;&#x884C;&#x9884;&#x6D4B;">&#x6784;&#x5EFA;&#x6A21;&#x578B;&#x8FDB;&#x884C;&#x9884;&#x6D4B;</h1>
<p>&#x505A;&#x597D;&#x6570;&#x636E;&#x9884;&#x5904;&#x7406;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x6570;&#x636E;&#x5582;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x673A;&#x5668;&#x5B66;&#x4E60;&#x6A21;&#x578B;&#x6765;&#x8FDB;&#x884C;&#x8BAD;&#x7EC3;&#x548C;&#x9884;&#x6D4B;&#x4E86;&#x3002;&#x4E0D;&#x8FC7;&#x5728;&#x6784;&#x5EFA;&#x6A21;&#x578B;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528;&#x5904;&#x7406;&#x8BAD;&#x7EC3;&#x96C6;&#x6570;&#x636E;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5904;&#x7406;&#x6D4B;&#x8BD5;&#x96C6;&#x3002;</p>
<pre><code class="lang-python">test_data=pd.read_csv(<span class="hljs-string">&apos;./Titanic/test.csv&apos;</span>)
test_data[<span class="hljs-string">&apos;Initial&apos;</span>]=<span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> test_data:
test_data.loc[:, <span class="hljs-string">&apos;Initial&apos;</span>] = test_data.Name.str.extract(<span class="hljs-string">&apos;([A-Za-z]+)\.&apos;</span>,expand=<span class="hljs-keyword">False</span>) <span class="hljs-comment">#lets extract the Salutations</span>
test_data.loc[:, <span class="hljs-string">&apos;Initial&apos;</span>].replace([<span class="hljs-string">&apos;Mlle&apos;</span>,<span class="hljs-string">&apos;Mme&apos;</span>,<span class="hljs-string">&apos;Ms&apos;</span>,<span class="hljs-string">&apos;Dr&apos;</span>,<span class="hljs-string">&apos;Major&apos;</span>,<span class="hljs-string">&apos;Lady&apos;</span>,<span class="hljs-string">&apos;Countess&apos;</span>,<span class="hljs-string">&apos;Jonkheer&apos;</span>,<span class="hljs-string">&apos;Col&apos;</span>,<span class="hljs-string">&apos;Rev&apos;</span>,<span class="hljs-string">&apos;Capt&apos;</span>,<span class="hljs-string">&apos;Sir&apos;</span>,<span class="hljs-string">&apos;Don&apos;</span>],[<span class="hljs-string">&apos;Miss&apos;</span>,<span class="hljs-string">&apos;Miss&apos;</span>,<span class="hljs-string">&apos;Miss&apos;</span>,<span class="hljs-string">&apos;Other&apos;</span>,<span class="hljs-string">&apos;Mr&apos;</span>,<span class="hljs-string">&apos;Mrs&apos;</span>,<span class="hljs-string">&apos;Mrs&apos;</span>,<span class="hljs-string">&apos;Other&apos;</span>,<span class="hljs-string">&apos;Other&apos;</span>,<span class="hljs-string">&apos;Other&apos;</span>,<span class="hljs-string">&apos;Mr&apos;</span>,<span class="hljs-string">&apos;Mr&apos;</span>,<span class="hljs-string">&apos;Mr&apos;</span>],inplace=<span class="hljs-keyword">True</span>)
test_data.loc[(test_data.Age.isnull())&amp;(test_data.Initial==<span class="hljs-string">&apos;Mr&apos;</span>),<span class="hljs-string">&apos;Age&apos;</span>]=<span class="hljs-number">33</span>
test_data.loc[(test_data.Age.isnull())&amp;(test_data.Initial==<span class="hljs-string">&apos;Mrs&apos;</span>),<span class="hljs-string">&apos;Age&apos;</span>]=<span class="hljs-number">36</span>
test_data.loc[(test_data.Age.isnull())&amp;(test_data.Initial==<span class="hljs-string">&apos;Miss&apos;</span>),<span class="hljs-string">&apos;Age&apos;</span>]=<span class="hljs-number">22</span>
test_data.loc[(test_data.Age.isnull())&amp;(test_data.Initial==<span class="hljs-string">&apos;Other&apos;</span>),<span class="hljs-string">&apos;Age&apos;</span>]=<span class="hljs-number">46</span>
test_data[<span class="hljs-string">&apos;Embarked&apos;</span>].fillna(<span class="hljs-string">&apos;S&apos;</span>, inplace=<span class="hljs-keyword">True</span>)
test_data[<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">0</span>
test_data.loc[test_data[<span class="hljs-string">&apos;Age&apos;</span>]&lt;=<span class="hljs-number">16</span>,<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">0</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&gt;<span class="hljs-number">16</span>)&amp;(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&lt;=<span class="hljs-number">32</span>),<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">1</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&gt;<span class="hljs-number">32</span>)&amp;(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&lt;=<span class="hljs-number">48</span>),<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">2</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&gt;<span class="hljs-number">48</span>)&amp;(test_data[<span class="hljs-string">&apos;Age&apos;</span>]&lt;=<span class="hljs-number">64</span>),<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">3</span>
test_data.loc[test_data[<span class="hljs-string">&apos;Age&apos;</span>]&gt;<span class="hljs-number">64</span>,<span class="hljs-string">&apos;Age_band&apos;</span>]=<span class="hljs-number">4</span>
test_data[<span class="hljs-string">&apos;Family_Size&apos;</span>]=<span class="hljs-number">0</span>
test_data[<span class="hljs-string">&apos;Family_Size&apos;</span>]=test_data[<span class="hljs-string">&apos;Parch&apos;</span>]+test_data[<span class="hljs-string">&apos;SibSp&apos;</span>]+<span class="hljs-number">1</span>
test_data[<span class="hljs-string">&apos;Alone&apos;</span>]=<span class="hljs-number">0</span>
test_data.loc[test_data.Family_Size==<span class="hljs-number">1</span>,<span class="hljs-string">&apos;Alone&apos;</span>]=<span class="hljs-number">1</span>
test_data[<span class="hljs-string">&apos;Fare_cat&apos;</span>]=<span class="hljs-number">0</span>
test_data.loc[test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&lt;=<span class="hljs-number">7.91</span>,<span class="hljs-string">&apos;Fare_cat&apos;</span>]=<span class="hljs-number">0</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&gt;<span class="hljs-number">7.91</span>)&amp;(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&lt;=<span class="hljs-number">14.454</span>),<span class="hljs-string">&apos;Fare_cat&apos;</span>]=<span class="hljs-number">1</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&gt;<span class="hljs-number">14.454</span>)&amp;(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&lt;=<span class="hljs-number">31</span>),<span class="hljs-string">&apos;Fare_cat&apos;</span>]=<span class="hljs-number">2</span>
test_data.loc[(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&gt;<span class="hljs-number">31</span>)&amp;(test_data[<span class="hljs-string">&apos;Fare&apos;</span>]&lt;=<span class="hljs-number">513</span>),<span class="hljs-string">&apos;Fare_cat&apos;</span>]=<span class="hljs-number">3</span>
test_data[<span class="hljs-string">&apos;Sex&apos;</span>].replace([<span class="hljs-string">&apos;male&apos;</span>,<span class="hljs-string">&apos;female&apos;</span>],[<span class="hljs-number">0</span>,<span class="hljs-number">1</span>],inplace=<span class="hljs-keyword">True</span>)
test_data[<span class="hljs-string">&apos;Embarked&apos;</span>].replace([<span class="hljs-string">&apos;S&apos;</span>,<span class="hljs-string">&apos;C&apos;</span>,<span class="hljs-string">&apos;Q&apos;</span>],[<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],inplace=<span class="hljs-keyword">True</span>)
test_data[<span class="hljs-string">&apos;Initial&apos;</span>].replace([<span class="hljs-string">&apos;Mr&apos;</span>,<span class="hljs-string">&apos;Mrs&apos;</span>,<span class="hljs-string">&apos;Miss&apos;</span>,<span class="hljs-string">&apos;Master&apos;</span>,<span class="hljs-string">&apos;Other&apos;</span>],[<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],inplace=<span class="hljs-keyword">True</span>)
test_data[<span class="hljs-string">&apos;Cabin&apos;</span>].replace([<span class="hljs-string">&apos;A&apos;</span>, <span class="hljs-string">&apos;B&apos;</span>, <span class="hljs-string">&apos;C&apos;</span>, <span class="hljs-string">&apos;D&apos;</span>, <span class="hljs-string">&apos;E&apos;</span>, <span class="hljs-string">&apos;F&apos;</span>, <span class="hljs-string">&apos;G&apos;</span>, <span class="hljs-string">&apos;T&apos;</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>], inplace=<span class="hljs-keyword">True</span>)
test_data.drop([<span class="hljs-string">&apos;Name&apos;</span>,<span class="hljs-string">&apos;Age&apos;</span>,<span class="hljs-string">&apos;Ticket&apos;</span>,<span class="hljs-string">&apos;Fare&apos;</span>,<span class="hljs-string">&apos;Fare_Range&apos;</span>,<span class="hljs-string">&apos;PassengerId&apos;</span>],axis=<span class="hljs-number">1</span>,inplace=<span class="hljs-keyword">True</span>)
</code></pre>
<p>&#x7136;&#x540E;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x673A;&#x5668;&#x5B66;&#x4E60;&#x6A21;&#x578B;&#x6765;&#x8BAD;&#x7EC3;&#x5E76;&#x9884;&#x6D4B;&#x4E86;&#xFF0C;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x968F;&#x673A;&#x68EE;&#x6797;&#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">10</span>)
clf.fit(X_train, Y_train)
predict = clf.predict(X_test)
print(accuracy_score(Y_test, predict))
</code></pre>
<p>&#x6B64;&#x65F6;&#x770B;&#x5230;&#x9884;&#x6D4B;&#x7684;&#x51C6;&#x786E;&#x7387;&#x8FBE;&#x5230;&#x4E86; 0.8275 &#x3002;</p>
</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="feature engerning.html" class="navigation navigation-prev " aria-label="Previous page: 特征工程">
<i class="fa fa-angle-left"></i>
</a>
<a href="tuning.html" class="navigation navigation-next " aria-label="Next page: 调参">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
5 years ago
gitbook.page.hasChanged({"page":{"title":"构建模型进行预测","level":"1.6.1.4","depth":3,"next":{"title":"调参","level":"1.6.1.5","depth":3,"path":"titanic/tuning.md","ref":"./titanic/tuning.md","articles":[]},"previous":{"title":"特征工程","level":"1.6.1.3","depth":3,"path":"titanic/feature engerning.md","ref":"./titanic/feature engerning.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/fit and predict.md","mtime":"2019-07-05T01:21:38.971Z","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>