2016-07-23 7 views
5

का उपयोग कर दस्तावेज़-फीचर मैट्रिक्स में फ़ीचर चयन प्राकृतिक भाषा प्रसंस्करण का उपयोग करके मैं टेक्स्टिंग खनन कर रहा हूं। मैंने दस्तावेज़-फीचर मैट्रिक्स (डीएफएम) उत्पन्न करने के लिए quanteda पैकेज का उपयोग किया। अब मैं ची-स्क्वायर टेस्ट का उपयोग करके फीचर चयन करना चाहता हूं। मुझे पता है कि पहले से ही बहुत से लोगों ने इस सवाल से पूछा था। हालांकि, मुझे इसके लिए प्रासंगिक कोड नहीं मिला।ची-स्क्वायर टेस्ट

मुझे लगता है कि मैं FSelector पैकेज में chi.squared इस्तेमाल कर सकते हैं, लेकिन मैं कैसे एक DFM वर्ग वस्तु (नीचे trainingtfidf) को यह समारोह लागू करने के लिए पता नहीं है सीखा है: (https://stats.stackexchange.com/questions/93101/how-can-i-perform-a-chi-square-test-to-do-feature-selection-in-r जवाब सिर्फ एक संक्षिप्त अवधारणा है, इस तरह दिया)। (मैनुअल में दिखाता है, यह predictor चर पर लागू होता है)

क्या कोई मुझे संकेत दे सकता है? मैं इसकी सराहना करता हूं!

उदाहरण कोड:

description <- c("From month 2 the AST and total bilirubine were not measured.", "16:OTHER - COMMENT REQUIRED IN COMMENT COLUMN;07/02/2004/GENOTYPING;SF- genotyping consent not offered until T4.", "M6 is 13 days out of the visit window") 
code <- c(4,3,6) 
example <- data.frame(description, code) 

library(quanteda) 
trainingcorpus <- corpus(example$description) 

trainingdfm <- dfm(trainingcorpus, verbose = TRUE, stem=TRUE, toLower=TRUE, removePunct= TRUE, removeSeparators=TRUE, language="english", ignoredFeatures = stopwords("english"), removeNumbers=TRUE, ngrams = 2) 

# tf-idf 
trainingtfidf <- tfidf(trainingdfm, normalize=TRUE) 

sessionInfo() 
R version 3.3.0 (2016-05-03) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows >= 8 x64 (build 9200) 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

उत्तर

3

यहाँ सुविधाओं के लिए कंप्यूटिंग ची-वर्ग मूल्यों के लिए एक सामान्य विधि है। यह आवश्यक है कि आपके पास कुछ वैरिएबल हैं जिसके खिलाफ एसोसिएशन बनाने के लिए, जो यहां कुछ वर्गीकरण चर हो सकता है जिसका उपयोग आप अपने वर्गीकृत प्रशिक्षण के लिए कर रहे हैं।

ध्यान दें कि मैं यह दिखा रहा हूं कि quanteda पैकेज में यह कैसे करें, लेकिन परिणाम अन्य टेक्स्ट पैकेज मैट्रिक्स ऑब्जेक्ट्स के लिए काम करने के लिए सामान्य होना चाहिए। यहां, मैं सहायक quantedaData पैकेज से डेटा का उपयोग कर रहा हूं जिसमें अमेरिकी राष्ट्रपति के संघ राज्य के सभी राज्य हैं।

data(data_corpus_sotu, package = "quanteda.corpora") 
table(docvars(data_corpus_sotu, "party")) 
## Democratic Democratic-Republican   Federalist   Independent 
##   90     28      4      8 
## Republican     Whig 
##   9      8 
sotuDemRep <- corpus_subset(data_corpus_sotu, party %in% c("Democratic", "Republican")) 

# make the document-feature matrix for just Reps and Dems 
sotuDfm <- dfm(sotuDemRep, remove = stopwords("english")) 

# compute chi-squared values for each feature 
chi2vals <- apply(sotuDfm, 2, function(x) { 
    chisq.test(as.numeric(x), docvars(sotuDemRep, "party"))$statistic 
}) 

head(sort(chi2vals, decreasing = TRUE), 10) 
## government  will  united  states  year  public congress  upon 
## 85.19783 74.55845 68.62642 66.57434 64.30859 63.19322 59.49949 57.83603 
##  war  people 
## 57.43142 57.38697 

इन्हें अब dfm_select() कमांड का उपयोग करके चुना जा सकता है। (नोट नाम से उस स्तंभ अनुक्रमण भी काम करेगा।)

# select just 100 top Chi^2 vals from dfm 
dfmTop100cs <- dfm_select(sotuDfm, names(head(sort(chi2vals, decreasing = TRUE), 100))) 
## kept 100 features, from 100 supplied (glob) feature types 

head(dfmTop100cs) 
## Document-feature matrix of: 182 documents, 100 features. 
## (showing first 6 documents and first 6 features) 
##    features 
## docs   citizens government upon duties constitution present 
## Jackson-1830  14   68 67  12   17  23 
## Jackson-1831  21   26 13  7   5  22 
## Jackson-1832  17   36 23  11   11  18 
## Jackson-1829  17   58 37  16   7  17 
## Jackson-1833  14   43 27  18   1  17 
## Jackson-1834  24   74 67  11   11  29 

जोड़ा गया:> = v0.9.9 के साथ इस textstat_keyness() समारोह का उपयोग किया जा सकता है।

# to avoid empty factors 
docvars(data_corpus_sotu, "party") <- as.character(docvars(data_corpus_sotu, "party")) 

# make the document-feature matrix for just Reps and Dems 
sotuDfm <- data_corpus_sotu %>% 
    corpus_subset(party %in% c("Democratic", "Republican")) %>% 
    dfm(remove = stopwords("english")) 

chi2vals <- dfm_group(sotuDfm, "party") %>% 
    textstat_keyness(measure = "chi2") 
head(chi2vals) 
# feature  chi2 p n_target n_reference 
# 1  - 221.6249 0  2418  1645 
# 2 mexico 181.0586 0  505   182 
# 3 bank 164.9412 0  283   60 
# 4  " 148.6333 0  1265   800 
# 5 million 132.3267 0  366   131 
# 6 texas 101.1991 0  174   37 

यह जानकारी इसके बाद के बाद ची^2 स्कोर के हस्ताक्षर निकाल दिया जाता है, सबसे भेदभाव सुविधाओं का चयन करने के लिए इस्तेमाल किया जा सकता।

# remove sign 
chi2vals$chi2 <- abs(chi2vals$chi2) 
# sort 
chi2vals <- chi2vals[order(chi2vals$chi2, decreasing = TRUE), ] 
head(chi2vals) 
#   feature  chi2 p n_target n_reference 
# 1    - 221.6249 0  2418  1645 
# 29044 commission 190.3010 0  175   588 
# 2   mexico 181.0586 0  505   182 
# 3   bank 164.9412 0  283   60 
# 4    " 148.6333 0  1265   800 
# 29043  law 137.8330 0  607  1178 


dfmTop100cs <- dfm_select(sotuDfm, chi2vals$feature) 
## kept 100 features, from 100 supplied (glob) feature types 

head(dfmTop100cs, nf = 6) 
Document-feature matrix of: 6 documents, 6 features (0% sparse). 
6 x 6 sparse Matrix of class "dfm" 
       features 
docs   fellow citizens senate house representatives : 
    Jackson-1829  5  17  2  3    5 1 
    Jackson-1830  6  14  4  6    9 3 
    Jackson-1831  9  21  3  1    4 1 
    Jackson-1832  6  17  4  1    2 1 
    Jackson-1833  2  14  7  4    6 1 
    Jackson-1834  3  24  5  1    3 5 
+1

आपको बहुत धन्यवाद केन! मेरा ईमेल भी जवाब देने के लिए धन्यवाद :) –