Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from matplotlib.pyplot import plot as plt
- from sklearn.preprocessing import OneHotEncoder
- from sklearn.naive_bayes import CategoricalNB
- # NAIVE BAYES
- # Load data
- traffic = pd.read_csv("./traffic.txt")
- X = traffic.loc[:, ["Weather", "Day"]]
- y = traffic.loc[:, "HighTraffic"]
- print()
- print("****************************************************")
- print("**** NAIVE BAYES ****")
- print("Traffic = ")
- print(traffic)
- print()
- # Encoder
- encoder = OneHotEncoder(handle_unknown="ignore", sparse=False)
- encoder = encoder.fit(X)
- X = encoder.transform(X)
- # Classification with Naive-Bayes
- clf = CategoricalNB(alpha=0.0001)
- clf.fit(X, y)
- # Prediction
- new1 = {"Weather": ["Hot"], "Day": ["Vacation"]}
- new_data = pd.DataFrame(new1)
- transformed_new_data = encoder.transform(new_data)
- print("New1 = " + str(new1))
- print("Prediction = " + str(clf.predict(transformed_new_data)))
- print("Probabilities = " + str(clf.predict_proba(transformed_new_data)))
- print()
- new2 = {"Weather": ["Hot"], "Day": ["Weekend"]}
- new_data2 = pd.DataFrame(new2)
- transformed_new_data2 = encoder.transform(new_data2)
- print("New2 = " + str(new2))
- print("Prediction = " + str(clf.predict(transformed_new_data2)))
- print("Probabilities = " + str(clf.predict_proba(transformed_new_data2)))
- print()
- print()
- print()
- print()
- # LAPLACE SMOOTHING
- print("****************************************************")
- print("**** LAPLACE SMOOTHING ****")
- # Classification with Naive-Bayes
- clf = CategoricalNB(alpha=0.9999)
- clf.fit(X, y)
- new2 = {"Weather": ["Hot"], "Day": ["Weekend"]}
- new_data2 = pd.DataFrame(new2)
- transformed_new_data2 = encoder.transform(new_data2)
- print("New2 = " + str(new2))
- print("Prediction = " + str(clf.predict(transformed_new_data2)))
- print("Probabilities = " + str(clf.predict_proba(transformed_new_data2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement