Skip to content

Commit

Permalink
modified: post/2024-05-20-klasifikasi-decision-tree-sklearn/index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
sasongko26 committed May 28, 2024
1 parent fe89d20 commit 7c26ff7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions post/2024-05-20-klasifikasi-decision-tree-sklearn/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,22 @@ <h2></h2>
5 Species 150 non-null object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB
</code></pre><p>Selanjutnya menentukan mana yang merupakan jenis (hasil klasifikasi/label) dan mana yang merupakan kriterianya (atribut).</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>kriteria <span style="color:#f92672">=</span> iris[[<span style="color:#e6db74">&#39;SepalLengthCm&#39;</span>, <span style="color:#e6db74">&#39;SepalWidthCm&#39;</span>,<span style="color:#e6db74">&#39;PetalLengthCm&#39;</span>, <span style="color:#e6db74">&#39;PetalWidthCm&#39;</span>]]
</span></span><span style="display:flex;"><span>jenis <span style="color:#f92672">=</span> iris[<span style="color:#e6db74">&#39;Species&#39;</span>]
</code></pre><p>Selanjutnya menentukan mana yang merupakan y (jenis/hasil klasifikasi/label) dan mana yang merupakan X nya (kriteria/atribut).</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>X <span style="color:#f92672">=</span> iris[[<span style="color:#e6db74">&#39;SepalLengthCm&#39;</span>, <span style="color:#e6db74">&#39;SepalWidthCm&#39;</span>,<span style="color:#e6db74">&#39;PetalLengthCm&#39;</span>, <span style="color:#e6db74">&#39;PetalWidthCm&#39;</span>]]
</span></span><span style="display:flex;"><span>y <span style="color:#f92672">=</span> iris[<span style="color:#e6db74">&#39;Species&#39;</span>]
</span></span></code></pre></div><p>Setelah itu membuat model <em>decision tree</em>, melatih model dengan fungsi fit()</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> sklearn.tree <span style="color:#f92672">import</span> DecisionTreeClassifier
</span></span><span style="display:flex;"><span>tree_model <span style="color:#f92672">=</span> DecisionTreeClassifier()
</span></span><span style="display:flex;"><span>tree_model <span style="color:#f92672">=</span> tree_model<span style="color:#f92672">.</span>fit(kriteria_train, jenis_train)
</span></span><span style="display:flex;"><span>tree_model <span style="color:#f92672">=</span> tree_model<span style="color:#f92672">.</span>fit(X_train, y_train)
</span></span></code></pre></div><p>Setelah modelnya klasifikasinya jadi, kita bisa gunakan model itu untuk mengklasifikasi. Misal akan dicari jenis bunga iris dengan panjang sepal 6,5 cm, lebar sepal 3,2 cm, panjang petal 6 cm dan lebar petal 2,5 cm. Kriteria ini kita jadikan sebagai sebuah variabel cari_jenis.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>cari_jenis <span style="color:#f92672">=</span> [<span style="color:#ae81ff">6.5</span>, <span style="color:#ae81ff">3.2</span>, <span style="color:#ae81ff">6.0</span>, <span style="color:#ae81ff">2.5</span>]
</span></span><span style="display:flex;"><span>print(tree_model<span style="color:#f92672">.</span>predict([cari_jenis])[<span style="color:#ae81ff">0</span>])
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>cari_y <span style="color:#f92672">=</span> [<span style="color:#ae81ff">6.5</span>, <span style="color:#ae81ff">3.2</span>, <span style="color:#ae81ff">6.0</span>, <span style="color:#ae81ff">2.5</span>]
</span></span><span style="display:flex;"><span>print(tree_model<span style="color:#f92672">.</span>predict([cari_y])[<span style="color:#ae81ff">0</span>])
</span></span></code></pre></div><p>Hasilnya?</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>Iris<span style="color:#f92672">-</span>virginica
</span></span></code></pre></div><p>Yes, iris viriginica. Lalu, apakah ini akurat? Kita tes akurasinya</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> sklearn.metrics <span style="color:#f92672">import</span> accuracy_score
</span></span><span style="display:flex;"><span>jenis_pred <span style="color:#f92672">=</span> tree_model<span style="color:#f92672">.</span>predict(kriteria_test)
</span></span><span style="display:flex;"><span>akurasi <span style="color:#f92672">=</span> accuracy_score(jenis_pred, jenis_test)
</span></span><span style="display:flex;"><span>y_pred <span style="color:#f92672">=</span> tree_model<span style="color:#f92672">.</span>predict(X_test)
</span></span><span style="display:flex;"><span>akurasi <span style="color:#f92672">=</span> accuracy_score(y_pred, y_test)
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">&#39;Akurasi : &#39;</span>, <span style="color:#ae81ff">100</span><span style="color:#f92672">*</span>akurasi,<span style="color:#e6db74">&#39;%&#39;</span>)
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>Akurasi : <span style="color:#ae81ff">93.33333333333333</span> <span style="color:#f92672">%</span>
</span></span></code></pre></div>
Expand Down

0 comments on commit 7c26ff7

Please sign in to comment.