Skip to content

감성분석

실습 준비

파일 열기

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)

훈련 데이터로 정확도(accuracy) 평가

model.score(x_train, y_train)

테스트 데이터로 정확도 평가

model.score(x_test, y_test)

단어별 확률

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

엘라스틱넷으로 C는 0.001, 0.01, 0.1 세 가지, L1의 비율은 0, 0.5, 1 세 가지를 시도 총 9가지 조합을 시도하여 성능이 가장 좋은 조합을 찾음

model = LogisticRegressionCV(
    penalty='elasticnet', solver='saga', random_state=42,
    Cs=[0.001, 0.01, 0.1], l1_ratios=[0, 0.5, 1])

학습

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)