<jupyter_start><jupyter_text>Naive Bayes  Import and Prepare the Data<jupyter_code>import sklearn
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
%matplotlib inline
import pandas
import numpy as np
import mglearn
from collections import Counter

df = pandas.read_excel('house_price_label.xlsx')
# combine multipl columns into a 2D array
# also convert the integer data to float data
X = np.column_stack((df.built_in.astype(float),df.price.astype(float))) 
y = df.house_type
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size =0.3,stratify = y, random_state=0) 

# for classification, make sure a stratify splitting method is selected
mglearn.discrete_scatter(X[:,0],X[:,1],y) # use mglearn to visualize data

plt.legend(y,loc='best')
plt.xlabel('house age')
plt.ylabel('house price')
plt.show()<jupyter_output><empty_output><jupyter_text>Classification<jupyter_code>from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import cohen_kappa_score

gnb = GaussianNB()
gnb.fit(X_train,y_train)

print("Training set accuracy: {:.2f}".format(gnb.score(X_train, y_train)))
print ("Training Kappa: {:.3f}".format(cohen_kappa_score(y_train,gnb.predict(X_train))))
print("Test set accuracy: {:.2f}".format(gnb.score(X_test, y_test)))
print ("Test Kappa: {:.3f}".format(cohen_kappa_score(y_test,gnb.predict(X_test))))<jupyter_output>Training set accuracy: 0.74
Training Kappa: 0.419
Test set accuracy: 0.75
Test Kappa: 0.426<jupyter_text>Visualize the Result<jupyter_code>fig, axes = plt.subplots(1, 2, figsize=(20, 6))
for ax,data in zip(axes,[X_train,X_test]):
    mglearn.discrete_scatter(data[:,0],data[:,1],gnb.predict(data),ax=ax) # use mglearn to visualize data

    ax.set_title("{}".format('Predicted House Type'))
    ax.set_xlabel("house age")
    ax.set_ylabel("house price")
    ax.legend(loc='best')
df.groupby('house_type').hist(figsize=(14,2),column=['price','built_in'])
import scipy.stats

house_type = ['condo','land and lot','single-family','townhouse']
house_feature =['huilt_in','price']
fig, axes = plt.subplots(4, 2, figsize=(12, 10))
for j in range(4):
    for i in range(2):
        mu = gnb.theta_[j,i] # get mean value of each feature for each class
        sigma=np.sqrt(gnb.sigma_ [j,i])# get std value of each feature for each class
        x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
        axes[j][i].plot(x,scipy.stats.norm.pdf(x, mu, sigma) )
        axes[j][i].set_title("{}".format(house_type[j]))
        axes[j][i].set_xlabel(house_feature[i])
plt.subplots_adjust(hspace=0.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14767932489451477, 'fraction_numerical': 0.018795550441120062, 'mean_word_length': 6.048648648648649, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '436738', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 959, 'n_words': 207, 'path': 'Lab+7.ipynb', 'repo_name': 'alexa-chris/480', 'size': '2607'}