2017-12-05 7 views
-1

私は、分類モデルのための他の数値機能と共に、テキストデータを使用しています。ランダムフォレストを使用した分類モデルの単語の袋で次元削減を行う方法

どのようにして類似した単語の袋を教師付き分類モデルでグループ化できますか。私はcountvectorizingの後に類似の単語をどのようにグループ化できますか、私は言葉の袋の次元を減らしたいです。すべてのヘルプは高く評価され

#Cleaning the Address Data 
stopwords =nltk.corpus.stopwords.words('english') 
data['Clean_addr'] = data['Adj_Addr'].apply(lambda x:"".join([item for item in x if item not in stopwords])) 
data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item for item in x if not item.isdigit()])) 
data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item for item in x if item not in string.punctuation])) 

#CountVectorizing the Address Data and fitting the sparse matrix to the Dataframe 

cv = CountVectorizer(max_features = 1000,analyzer='word') 
cv_addr = cv.fit_transform(data.pop('Clean_addr')) 
for i, col in enumerate(cv.get_feature_names()): 
    data[col] = pd.SparseSeries(cv_addr[:, i].toarray().ravel(), fill_value=0) 

#LabelEncoding -Converting Catergocial to Numerical 
data['Resi'] = LabelEncoder().fit_transform(data['Resi']) 
data['Resi_Area'] = LabelEncoder().fit_transform(data['Resi_Area']) 
data['Product'] = LabelEncoder().fit_transform(data['Product']) 
data['Phone_Type'] = LabelEncoder().fit_transform(data['Phone_Type']) 
data['Co_Name_FLag'] = LabelEncoder().fit_transform(data['Co_Name_FLag']) 

#Classification 
X_train, X_test, y_train, y_test = train_test_split(train, Y, test_size=0.3,random_state =8) 
rus = RandomUnderSampler(random_state=42) 
X_train_res, y_train_res = rus.fit_sample(X_train, y_train) 
rf=RandomForestClassifier(n_estimators=1000,oob_score=True) 

fit_rf=rf.fit(X_train_res,y_train_res) 

私のコード。

+0

類似の単語が意味するものの戦略を定義する必要があります。長さが同じであるべきであることを意味するのか、または省略形は完全な形などとみなされるべきであることを意味しますか?これは、それ自体が非常に広い問題であり、活発な研究の下にあります。たぶんword2vecが役に立ちます。 word2vecは、同様の単語または近い単語の配列値を類似した単語に提供します。 –

+0

また、類似した単語の辞書を作成し、テキスト内の他の単語のすべての出現箇所を代表単語に置き換えて、TfidfVectorizerを呼び出すこともできます。しかし、それはお勧めしません。 –

+0

@VivekKumar、私は略語を見ていて、一緒に限られたものと一致しています –

答えて

0

あなたの鞄の身長を減らしたい場合は、sklearnのSelectPercentileを使用してください。ここではアイリスデータのexempleは次のとおりです。

from sklearn.feature_selection import SelectPercentile 
from sklearn.feature_selection import chi2 
import numpy 
iris = load_iris() 
X, y = iris.data, iris.target 
selector = SelectPercentile(score_func=chi2, percentile=50) 
X_reduced = selector.fit_transform(X, y) 

あなたが簡単にあなたの例では言葉のバッグにそれを拡張することができます

cv = CountVectorizer(max_features = 1000,analyzer='word') 
cv_addr = cv.fit_transform(data.pop('Clean_addr')) 
selector = SelectPercentile(score_func=chi2, percentile=50) 
X_reduced = selector.fit_transform(cv_addr, Y) 

した後、あなたが作品をパーセンタイルかを確認するために、異なる臨床試験を行うことができます、ということ最高の、そして最終的にはパーセンタイルで得点をプロットするだけでなく、関連する彼らの用語頻度と高い得点の単語をプロットは、ここでは、バーグラフの一例である:

enter image description here

幸運。

+0

これは、Limited&Ltdのような言葉で似たような単語をまとめて扱うことができますか?グッド&ニースなど –

+0

mmhは本当にありません。同様の単語をグループ化する場合は、文書レベルでtfidfの重み付けを行うか、単語の埋め込みを行ってください。カイ二乗選択は、分類のための差別的勢力に従って言葉だけを評価する。 – Elliot

関連する問題