Artificial Intelligence For Learning Stock Investments

Plot byĀ Kirill EremenkoĀ &Ā Hadelin de PontevesĀ onĀ Udemy
Apa Itu Naive Bayes?
Naive Bayes merupakan teknik dalam Machine Learning yang digunakan untuk memisahkan data atau mengklasifikasikan data berdasarkan Bayes Theorem.
Bayes Theorem merupakan konsep dari statistik dan probabilitas yang berguna untuk menghitung probabilitas atau kemungkinan dari suatu kondisi yang berkolerasi atau terkait.
Dinamakan “naive” karena seharusnya teknik ini digunakan untuk menangani data yang independen atau tidak berkolerasi tetapi pada prakteknya dipakai untuk data yang berkolerasi atau berkaitan untuk menghasilkan hasil prediksi yang akurat juga.
Itulah kenapa disebut “naive” karena yang seharusnya model tersebut untuk menangani data yang independen tetapi tetap dipakai untuk data yang berkolerasi atau berkaitan satu sama lain antara sesama independen variabel.
Rumus Yang Ada Pada Model Naive Bayes
- Prior Probability
P(C) = Jumlah data dalam kelas C / Total data observasi
ā
Keterangan:
P(C) = Probabilitas kelas C dari semua data observasi
- Marginal Likelihood
P(X) = Jumlah data observasi dalam area yang sama atau punya kemiripan / Total data observasi
Keterangan:
P(X) = Probabilitas dari fitur X yang mempunyai kemiripan antara sesama fitur X
Note:
Marginal Likelihood didapatkan dengan cara menentukan seberapa besar area yang dibutuhkan dari data observasi baru yang akan diprediksi. Data observasi baru yang akan diprediksi ini diletakkan secara acak berdasarkan kebutuhan yang diinginkan.
Marginal Likelihood dilakukan untuk mencari kemiripan data observasi antara kelas – kelas yang terbentuk dari model tersebut supaya kita bisa memisahkan data observasi baru dengan mudah berdasarkan kemiripan ini atau dengan Marginal Likelihood
- Likelihood (Gaussian Naive Bayes)
P(X|C) = Data observasi yang mirip antara setiap kelas / Jumlah data dalam kelas C
keterangan:
P(X|C) = Probabilitas dari fitur X diberikan kelas C
- Posterior Probability
P(C|X) = ( P(X|C) * P(C) ) / P(X)
keterangan:
P(C|X) = Probabilitas dari kelas C diberikan fitur X (Independen Variabel)
Cara Kerja Naive Bayes
- Tentukan ada berapa dan apa saja klasifikasi yang terdapat pada data observasi,
- Hitung seberapa besar Prior Probability atau probabilitas dari suatu kelas dari semua data observasi,
- Hitung nilai dari Marginal Likelihood untuk mencari kemiripan data observasi,
- Hitung nilai dari Likelihood berdasarkan Marginal Likelihood dan data observasi dari kelas data yang dituju atau yang ingin dihitung,
- Setelah mendapatkan semua nilai yaitu nilai Prior Probability, Marginal Likelihood dan Likelihood maka hal terakhir yang dilakukan adalah mengkalkulasikannya untuk mendapatkan persentase probabilitas dari kelas yang dituju atau dihitung,
- Ulangi langkah nomor 1 sampai 5 untuk menghitung nilai dari kelas lainnya yang dituju atau yang dibutuhkan,
- Setelah mendapatkan nilai probabilitas dari semua kelas, maka hal selanjutnya adalah bandingkan semua nilai probabilitas tersebut, data observasi baru yang diprediksi akan ditempatkan di kelas dengan nilai probabilitas yang paling tinggi.
Kode Python Untuk Membuat Model Naive Bayes
- Impor librari
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
- Impor dataset
dataset = pd.read_csv(‘Social_Network_Ads.csv’)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
- Memisahkan data menjadi training set dan test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
- Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
- Melatih model Naive Bayes kedalam training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)
- Memprediksi hasil test set
y_pred = classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
- Memprediksi data observasi baru
print(classifier.predict(sc.transform([[30,87000]])))
- Membuat Confusion Matrix
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
- Visualisasi hasil training set
from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() – 10, stop = X_set[:, 0].max() + 10, step = 0.25),
np.arange(start = X_set[:, 1].min() – 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap((‘red’, ‘green’)))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap((‘red’, ‘green’))(i), label = j)
plt.title(‘Naive Bayes (Training set)’)
plt.xlabel(‘Age’)
plt.ylabel(‘Estimated Salary’)
plt.legend()
plt.show()

Plot byĀ Kirill EremenkoĀ &Ā Hadelin de PontevesĀ onĀ Udemy
- Visualisasi hasil test set
from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() – 10, stop = X_set[:, 0].max() + 10, step = 0.25),
np.arange(start = X_set[:, 1].min() – 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap((‘red’, ‘green’)))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap((‘red’, ‘green’))(i), label = j)
plt.title(‘Naive Bayes (Test set)’)
plt.xlabel(‘Age’)
plt.ylabel(‘Estimated Salary’)
plt.legend()
plt.show()

Plot byĀ Kirill EremenkoĀ &Ā Hadelin de PontevesĀ onĀ Udemy
Penjelasan diatas menjelaskan apa itu Naive Bayes, apa saja rumus yang diperlukan untuk membuat model Naive Bayes, bagaimana cara kerja dari model Naive Bayes dan bagaimana cara membuat modelnya dengan kode python. Pelajari hal yang diperlukan diatas supaya Anda menjadi handal dalam membuat model Naive Bayes.
Bagi anda yang ingin memberikan komentar pada website ini, silahkan tulis komentar Anda dengan mengisi nama dan alamat email Anda. Anda dapat membaca blog kami sebelumnya mengenai kernel SVM. Nantikan konten blog kami selanjutnya yang ga kalah menarik.
[…] Bagi anda yang ingin memberikan komentar pada website ini, silahkan tulis komentar Anda dengan mengisi nama dan alamat email Anda. Anda dapat membaca blog kami sebelumnya mengenai Support Vector Machine dan blog kami selanjutnya mengenai Naive Bayes. […]
mantul
I do not even know how I ended up here but I thought this post was great I do not know who you are but certainly youre going to a famous blogger if you are not already Cheers
I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks
Your blog is a treasure trove of valuable insights and thought-provoking commentary. Your dedication to your craft is evident in every word you write. Keep up the fantastic work!
I loved as much as youll receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get bought an nervousness over that you wish be delivering the following unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike
Thanks I have just been looking for information about this subject for a long time and yours is the best Ive discovered till now However what in regards to the bottom line Are you certain in regards to the supply
I loved as much as you will receive carried out right here The sketch is tasteful your authored subject matter stylish nonetheless you command get got an edginess over that you wish be delivering the following unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike
I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike
I was recommended this website by my cousin I am not sure whether this post is written by him as nobody else know such detailed about my trouble You are amazing Thanks
Fantastic site A lot of helpful info here Im sending it to some buddies ans additionally sharing in delicious And naturally thanks on your sweat
Your blog is a breath of fresh air in the often stagnant world of online content. Your thoughtful analysis and insightful commentary never fail to leave a lasting impression. Thank you for sharing your wisdom with us.
Thank you for the good writeup It in fact was a amusement account it Look advanced to far added agreeable from you However how could we communicate
I have read some excellent stuff here Definitely value bookmarking for revisiting I wonder how much effort you put to make the sort of excellent informative website
I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts
you are in reality a just right webmaster The site loading velocity is incredible It seems that you are doing any unique trick In addition The contents are masterwork you have performed a wonderful task on this topic
Your writing has a way of resonating with me on a deep level. It’s clear that you put a lot of thought and effort into each piece, and it certainly doesn’t go unnoticed.
Hi Neat post There is a problem along with your website in internet explorer would test this IE still is the market chief and a good section of other folks will pass over your magnificent writing due to this problem
Your passion for your subject matter shines through in every post. It’s clear that you genuinely care about sharing knowledge and making a positive impact on your readers. Kudos to you!
Your writing has a way of resonating with me on a deep level. I appreciate the honesty and authenticity you bring to every post. Thank you for sharing your journey with us.
I was recommended this website by my cousin I am not sure whether this post is written by him as nobody else know such detailed about my difficulty You are wonderful Thanks
Your articles never fail to captivate me. Each one is a testament to your expertise and dedication to your craft. Thank you for sharing your wisdom with the world.
I do agree with all the ideas you have introduced on your post They are very convincing and will definitely work Still the posts are very short for newbies May just you please prolong them a little from subsequent time Thank you for the post
My brother recommended I might like this web site He was totally right This post actually made my day You cannt imagine just how much time I had spent for this information Thanks
I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts