Machine Learning Pada Iris Data Set
Iris data set adalah dataset yang paling populer buat latihan machine learning. Dataset ini isinya tentang 3 macam spesies bunga beserta ukuran petal dan sepal.
1. sepal length in cm
2. sepal width in cm
3. petal length in cm
4. petal width in cm
5. class:
— Iris Setosa
— Iris Versicolour
— Iris Virginica
Disini ada 4 pembelajaran yang akan digunakan yaitu
- NaiveBayes
- Decision Tree
- kNN
- Neural Network MLP
1. NaiveBayes
Naive Bayes classifier (NBC) merupakan salah satu metoda pemelajaran mesin yang memanfaatkan perhitungan probabilitas dan statistik yang dikemukakan oleh ilmuwan Inggris Thomas Bayes, yaitu memprediksi probabilitas di masa depan berdasarkan pengalaman di masa sebelumnya.
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
print('Model Naive Bayes: \n', gnb)
print('\n')
y_pred_gnb = gnb.fit(iris_data.data, iris_data.target).predict(iris_data.data)
print('Hasil Prediksi: \n', y_pred_gnb)
2. DecisionTree
Decision tree adalah salah satu metode klasifikasi yang paling populer, karena mudah untuk diinterpretasi oleh manusia. Decision tree adalah model prediksi menggunakan struktur pohon atau struktur berhirarki.
from sklearn import tree
import graphviz
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris_data.data, iris_data.target)
print('Model Decision Tree: \n', clf)
print('\n')
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris_data.feature_names,
class_names=iris_data.target_names,
filled=True, rounded=True,
special_characters=True)
graph_dt = graphviz.Source(dot_data)
print('Hasil Prediksi: \n')
graph_dt
3. kNN
K-Nearest Neighbor (KNN) adalah suatu metode yang menggunakan algoritma supervised dimana hasil dari query instance yang baru diklasifikan berdasarkan mayoritas dari kategori pada KNN.
from matplotlib.colors import ListedColormap
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier as KNN
X = iris_data.data
Y = iris_data.target
knn = KNN()
print('Model Decision Tree: \n', knn)
print('\n')
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
h = .02
n_neighbors = 5
X = iris_data.data[:, :2]
for weights in ['uniform', 'distance']:
# we create an instance of Neighbours Classifier and fit the data.
clf = KNN(n_neighbors, weights=weights)
clf.fit(X, Y)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=cmap_bold,
edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, weights = '%s')"
% (n_neighbors, weights))
plt.show()
4. Neural Network MLP
from sklearn.neural_network import MLPClassifier as MLP
mlp_clf = MLP(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
mlp_clf.fit(iris_data.data, iris_data.target)
mlp_clf.coefs_
Penulis masih tahap belajar, masukan dari teman-teman pembaca sangat membantu penulis untuk berkembang :) Artikel selanjutnya bakal kita cobain dengans kema split train 90% dan test 10%, dan menampilkan kinerja serta confusion matrixnya.