import pandas as pd # 데이터 전처리를 위함
import seaborn as sns # 시각화를 위함
df = pd.read_csv('d:\\data\\mushrooms.csv')
df = pd.get_dummies(df)
#print(df.shape) # (8124, 23)
print(df)
print(df.shape) # (8124, 119)
# get_dummies 함수를 이용해서 값의 종류에 따라 전부 0 아니면 1로 변환함
# DataFrame 확인
print(df.shape) # (8124, 23)
print(df.info()) # 전부 object (문자)형으로 되어있음
print(df.describe())
# X = 전체 행, 마지막 열 제외한 모든 열 데이터 -> n차원 공간의 포인트
X = df.iloc[:,2:].to_numpy()
y = df.iloc[:,1].to_numpy()
print(X)
print(y)
print(df.shape) # (8124, 119)
print(len(X)) # 8124
print(len(y)) # 8124
#from sklearn import preprocessing
#X=preprocessing.StandardScaler().fit(X).transform(X)
#X=preprocessing.MinMaxScaler().fit(X).transform(X)
from sklearn.model_selection import train_test_split
# 훈련 데이터 75, 테스트 데이터 25으로 나눔.
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state = 10)
print(X_train.shape) # (6093, 22)
print(y_train.shape) # (6093,)
# 학습/예측(Training/Pradiction)
from sklearn.naive_bayes import GaussianNB
# 나이브베이즈 분류기를 생성
classifier = GaussianNB(var_smoothing=0.004) #
# 분류기 학습
classifier.fit(X_train, y_train)
# 예측
y_pred= classifier.predict(X_test)
print(y_pred)
# 작은 이원교차표
from sklearn.metrics import confusion_matrix
conf_matrix= confusion_matrix(y_test, y_pred)
print(conf_matrix)
# 정밀도 , 재현율, f1 score 확인
from sklearn.metrics import classification_report
report = classification_report(y_test, y_pred)
print(report)
# 정확도 확인하는 코드
from sklearn.metrics import accuracy_score
accuracy = accuracy_score( y_test, y_pred)
print(accuracy) # 0.9497 MultinomialNB
# 0.9615 GaussianNB
# 0.9350 BernoulliNB
#%%
# 최적의 라플라스 값 찾기 + 시각화 코드
import numpy as np
import matplotlib.pyplot as plt
errors = []
for i in np.arange(0.001, 0.01 , 0.001):
nb = GaussianNB(var_smoothing=i)
nb.fit(X_train, y_train)
pred_i = nb.predict(X_test)
errors.append(np.mean(pred_i != y_test))
print(errors)
for k, i in zip(np.arange(0.001, 0.01 , 0.001),errors):
print (k, '--->', i)
plt.plot( np.arange(0.001, 0.01, 0.001), errors, marker='o')
plt.title('Mean error with laplace-Value')
plt.xlabel('laplace')
plt.ylabel('mean error')
plt.show()
#결과 :
[0.009354997538158542, 0.008862629246676515, 0.008862629246676515, 0.008370260955194485, 0.008370260955194485, 0.008370260955194485, 0.008370260955194485, 0.008370260955194485, 0.008370260955194485]
0.001 ---> 0.009354997538158542
0.002 ---> 0.008862629246676515
0.003 ---> 0.008862629246676515
0.004 ---> 0.008370260955194485
0.005 ---> 0.008370260955194485
0.006 ---> 0.008370260955194485
0.007 ---> 0.008370260955194485
0.008 ---> 0.008370260955194485
0.009000000000000001 ---> 0.008370260955194485
-> 라플라스 값이 0.004 부터 정확도가 가장 높다.
그래프: