문서 분류
import pandas as pd
df = pd.read_excel('yelp.xlsx')
문서 단어 행렬 만들기
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features=1000, stop_words='english')
dtm = cv.fit_transform(df.review)
x와 y를 지정
x = dtm
y = df.sentiment
데이터 분할
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
x,
y,
test_size=0.2, # 20%의 데이터를 테스트용으로 유보
random_state=42) # 유사난수의 씨앗값 seed을 42로 설정
나이브 베이즈 분류
from sklearn.naive_bayes import BernoulliNB
model = BernoulliNB() # 모델 만들기
model.fit(x_train, y_train) # 학습
model.score(x_train, y_train) # 훈련 데이터로 정확도(accuracy) 평가
테스트
model.score(x_test, y_test) # 테스트 데이터로 정확도(accuracy) 평가
단어별 확률
prob_df = pd.DataFrame({
'단어': cv.get_feature_names_out(),
'비율': model.feature_log_prob_[1] - model.feature_log_prob_[0]
})
상대적으로 긍정 문장에서 많이 나오는 단어
prob_df.sort_values('비율').tail(10)
상대적으로 부정 문장에서 많이 나오는 단어
prob_df.sort_values('비율').head(10)
로지스틱 회귀분석
from sklearn.linear_model import LogisticRegressionCV
model = LogisticRegressionCV(
penalty='elasticnet', solver='saga', random_state=42,
Cs=[0.1, 1, 10], l1_ratios=[0, 0.5, 1], max_iter=4000)
model.fit(x_train, y_train)
가장 좋은 C
model.C_
가장 좋은 L1의 비율
model.l1_ratio_
훈련 데이터에서 정확도
model.score(x_train, y_train)
테스트 데이터에서 정확도
model.score(x_test, y_test)
단어별 가중치
word_coef = pd.DataFrame({
'단어': cv.get_feature_names_out(),
'가중치': model.coef_.flat
})
긍정 단어 보기
word_coef.sort_values('가중치').tail(10)
부정 단어 보기
word_coef.sort_values('가중치').head(10)
예측
y_pred = model.predict(x_test)
확률로 예측
probs = model.predict_proba(x_test)
긍정 확률만
prob = probs[:, 1]
문턱값에 따라 다르게 예측
threshold = 0.5 # 문턱값
y_pred = np.where(prob > threshold, 1, 0)
혼동 행렬
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
정확도
from sklearn.metrics import *
accuracy_score(y_test, y_pred)
정밀도
precision_score(y_test, y_pred)
재현도
recall_score(y_test, y_pred)
ROC 곡선
from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt
시각화
fpr, tpr, threshold = roc_curve(y_test, prob)
plt.plot(fpr, tpr)
AUC
roc_auc_score(y_test, prob)