์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- numpy
- datascience
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- Lambda
- ADsP
- ๊ตฐ์งํ
- Python
- iloc
- t-test
- ๋์ํ๋ณธ
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ์๋ํด๋ผ์ฐ๋
- dataframe
- ํ ์คํธ๋ถ์
- LDA
- ํฌ๋กค๋ง
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ๋ ๋ฆฝํ๋ณธ
- pandas
- ๋ฐ์ดํฐ๋ถ์
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- ์ธ๋์ํ๋ง
- ADP
- DBSCAN
- ์ค๋ฒ์ํ๋ง
- PCA
- opencv
- ํ์ด์ฌ
- ๋น ๋ฐ์ดํฐ
Data Science LAB
[Python] ํ๊ธ ํ ์คํธ ์ฒ๋ฆฌ - ๋ค์ด๋ฒ ์ํ ํ์ ๊ฐ์ฑ ๋ถ์ ๋ณธ๋ฌธ
[Python] ํ๊ธ ํ ์คํธ ์ฒ๋ฆฌ - ๋ค์ด๋ฒ ์ํ ํ์ ๊ฐ์ฑ ๋ถ์
ใ ใ ใ ใ 2022. 2. 26. 19:59๋๋์ด Konlpy๋ฅผ ์ค์นํด์ ์ฃผํผํฐ ๋ ธํธ๋ถ์์ ์ฌ์ฉํ ์ ์๊ฒ ๋์๋ค!
https://suhye.tistory.com/entry/%E3%85%9C?category=1037658
[Python]KONLPy ์ค์น ๋ฐฉ๋ฒ ๋ฐ ์๋ฌ ํด๊ฒฐ
๋๋์ด๋๋์ด๋๋์ด! ์๋์ฝ๋ค์์ ์๋ฌ๋ฅผ ํด๊ฒฐํ๊ณ konlpy๋ฅผ ์คํํ ์ ์๊ฒ ๋์๋ค! konlpy ์คํ์ด ์๋ผ์ ํ๊ธ ํ ์คํธ ๋ถ์์ ์์ ๋ชปํ์๋๋ฐ ์ด์ ํ ์ ์๊ฒ ๋์๋ค(๊ฐ๊ฒฉ) Konlpy ์ค์น ๋ฐฉ๋ฒ 1. JA
suhye.tistory.com
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ๋ฐ์ดํฐ์ ๋ก๋ฉ
import konlpy
import pandas as pd
train = pd.read_csv(r'C:\Users\Naver\ratings_train.txt',sep='\t')
train.head()
๋จผ์ ํญ(\t)๋ก ์นผ๋ผ์ ๋ถ๋ฆฌํ๊ณ , ๋ฐ์ดํฐํ๋ ์ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค.
train['label'].value_counts()
ํ์ต ๋ฐ์ดํฐ ์ ์ 0๊ณผ 1์ label ๊ฐ์ด ๊ฐ๊ฐ 75173, 74827๋ก ๊ฑฐ์ ๋์ผํ๊ฒ ๋ถํฌ๋์ด์๋ ๊ฒ์ ํ์ธํจ
import re
train = train.fillna(' ')
train['document'] = train['document'].apply(lambda x : re.sub(r"\d+"," ",x))
test = pd.read_csv(r"C:\Users\suhye\Desktop\Machine Learning\Naver\ratings_test.txt",sep='\t')
test = test.fillna(' ')
test['document'] = test['document'].apply(lambda x : re.sub(r"\d+"," ",x))
#id์ปฌ๋ผ ์ญ์ ์ํ
train.drop('id',axis=1,inplace=True)
test.drop('id',axis=1, inplace=True)
์ ๊ท ํํ์์ ์ด์ฉํ์ฌ ์ซ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ณ๊ฒฝํ๊ณ ,
ํ ์คํธ ๋ฐ์ดํฐ ์ ์ ๋ก๋ฉํ๊ณ ๋์ผํ๊ฒ null๊ณผ ์ซ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ณํํ์๋ค.
๋ฌธ์ฅ์ ํํ์ ๋จ์ด ํํ๋ก ํ ํฐํํ์ฌ list ๊ฐ์ฒด ๋ฐํ
from konlpy.tag import Twitter
twitter = Twitter()
def tw_tokenizer(text):
tokens_ko = twitter.morphs(text)
return tokens_ko
Twitter ํด๋์ค๋ SNS ๋ถ์์ ์ ํฉํ๋ค.
Twitter์ morphs() ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ์ ๋ ฅ ์ธ์๋ก ๋ค์ด์จ ๋ฌธ์ฅ์ ํํ์ ๋จ์ด ํํ๋ก ํ ํฐํํ์ฌ ๋ฆฌ์คํธ ํํ๋ก ๋ฐํ์์ผ์ค๋ค.
TF-IDF ๋ชจ๋ธ ์์ฑ
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
#Twitter ๊ฐ์ฒด์ morphs()๊ฐ์ฒด๋ฅผ ์ด์ฉํ tokenizer ์ฌ์ฉ
tfidf_vect = TfidfVectorizer(tokenizer = tw_tokenizer,ngram_range=(1,2),min_df=3,max_df=0.9)
tfidf_matrix_train = tfidf_vect.fit_transform(train['document'])
์ฌ์ดํท๋ฐ์ TfidfVectorizer๋ฅผ ์ด์ฉํ์ฌ TF-IDF ํผ์ฒ ๋ชจ๋ธ์ ์์ฑํ์๋ค.
lr = LogisticRegression()
params = {'C':[1,3.5,4.5,5.5,10]}
grid = GridSearchCV(lr,param_grid=params,cv=3,scoring='accuracy',verbose=1)
grid.fit(tfidf_matrix_train,train['label'])
print("best params : {}".format(grid.best_params_))
print("best score : {:.3f}".format(grid.best_score_))
๋ก์ง์คํฑ ํ๊ท๋ถ์์ ์ด์ฉํ์ฌ ๋ถ๋ฅ ๊ธฐ๋ฐ์ ๊ฐ์ฑ ๋ถ์์ ์ํํ์๋ค.
์ ํ๋ ํฅ์์ ์ํด GridSearch๊น์ง ํด์ฃผ๋ฉด
from sklearn.metrics import accuracy_score
#TfidfVectorizer๋ฅผ ์ด์ฉํด ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ TF-IDF ๊ฐ์ผ๋ก ํผ์ฒ ๋ณํ
tfidf_matrix_test = tfidf_vect.transform(test['document'])
#classifier๋ ์ต์ classifier๋ฅผ ์ด์ฉ
best_estimator = grid.best_estimator_
preds = best_estimator.predict(tfidf_matrix_test)
print("Logistic Regression ์ ํ๋ : ",accuracy_score(test['label'],preds))
'๐ Machine Learning > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ๋ฌธ์ ์ ์ฌ๋ (0) | 2022.02.25 |
---|---|
[Python] ๋ฌธ์ ๊ตฐ์งํ (0) | 2022.02.24 |
[Python] ํ ํฝ ๋ชจ๋ธ๋ง (20 ๋ด์ค๊ทธ๋ฃน) (0) | 2022.02.22 |
[Python] SentiWordNet, VADER์ ์ด์ฉํ ์ํ ๊ฐ์ํ ๊ฐ์ฑ ๋ถ์ (0) | 2022.02.21 |
[Python] ๊ฐ์ฑ๋ถ์ - ๋น์ง๋ ํ์ต (0) | 2022.02.20 |