什么是自然語言(NLP),就是網(wǎng)絡中的一些書面文本。對于醫(yī)療方面,例如醫(yī)療記錄、病人反饋、醫(yī)生業(yè)績評估和社交媒體評論,可以成為幫助臨床決策和提高質量的豐富數(shù)據(jù)來源。如互聯(lián)網(wǎng)上有基于文本的數(shù)據(jù)(例如,對醫(yī)療保健提供者的社交媒體評論),這些數(shù)據(jù)我們可以直接下載,有些可以通過爬蟲抓取。例如:在病人論壇上發(fā)表對疾病或藥物的評論,可以將它們存儲在數(shù)據(jù)庫中,然后進行分析。
在這個之前需要了解什么是情緒分析,情緒分析是指賦予詞語、短語或其他文本單位主觀意義的過程。情緒可以簡單地分為正面或負面,也可以與更詳細的主題有關,比如某些詞語所反映的情緒。簡單來說就是從語言從提取患者態(tài)度或者情緒的詞語,然后進行分析,比如患者對這個藥物的療效,她說好,有用,我們提取出這些關鍵詞來進行分析。
自然語言(NLP)進行機器學習分為無監(jiān)督學習和有監(jiān)督學習,本期咱們先來介紹無監(jiān)督學習。咱們先導入R包和數(shù)據(jù)
library(tm)
library(data.table)
library(tidytext)
library(dplyr)
library(tidyr)
library(topicmodels)
library(performanceEstimation)
library(rsample)
library(recipes)
library(parsnip)
library(workflows)
library(tune)
library(dials)
library(kernlab)
library(ggplot2)
training_data <- as.data.frame(fread("E:/r/test/drugsComTrain_raw.tsv"))
咱們先來看一下數(shù)據(jù)
這是一個患者對藥物評價的數(shù)據(jù),該數(shù)據(jù)集提供了患者對特定藥物及相關疾病的評估,以及10星級患者評級,反映了整體患者滿意度。這些數(shù)據(jù)是通過爬取在線醫(yī)藥評論網(wǎng)站獲得的。公眾號回復:藥物評論數(shù)據(jù),可以獲得該數(shù)據(jù),我們先來看一下數(shù)據(jù)的構成,drugName:藥物名稱,condition (categorical)條件類別,多指患者的一些疾病類別,review:患者對藥物的評論,rating患者對藥物的打分,date (date)患者評論的日期,usefulCount發(fā)現(xiàn)評論有用的數(shù)據(jù),代表瀏覽者支持這個觀點。
這個數(shù)據(jù)有16萬行,非常大,為了演示方便,我們只取5000個來演示
set.seed(123)
sample <- sample(nrow(training_data),5000)
data <- training_data[sample,]
dim(data)
因為這是網(wǎng)頁抓取的數(shù)據(jù),會存在一些亂碼,所以咱們在分析前先要進行數(shù)據(jù)的清洗,編寫一個簡單的數(shù)據(jù)清洗程序,就是一些簡單的正則式小知識
cleanText <- function(rawtext) {
rawtext <- gsub("'", "?", rawtext)
# Expand contractions
rawtext <- gsub("n?t", " not", rawtext)
rawtext <- gsub("won?t", "will not", rawtext)
rawtext <- gsub("wont", "will not", rawtext)
rawtext <- gsub("?ll", " will", rawtext)
rawtext <- gsub("can?t", "can not", rawtext)
rawtext <- gsub("cant", "can not", rawtext)
rawtext <- gsub("didn?t", "did not", rawtext)
rawtext <- gsub("didnt", "did not", rawtext)
rawtext <- gsub("?re", " are", rawtext)
rawtext <- gsub("?ve", " have", rawtext)
rawtext <- gsub("?d", " would", rawtext)
rawtext <- gsub("?m", " am", rawtext)
rawtext <- gsub("?s", "", rawtext)
# Remove non-alphanumeric characters.
rawtext <- gsub("[^a-zA-Z0-9 ]", " ", rawtext)
# Convert all text to lower case.
rawtext <- tolower(rawtext)
# Stem words
rawtext <- stemDocument(rawtext, language = "english")
return(rawtext)
}
這個小程序我簡單介紹一下,第一行就是就是把文字中的"'"
全部改成“?”
,其他也是差不多的,第二行就是把"n?t"改成" not".接下來gsub("[^a-zA-Z0-9 ]", " ", rawtext)
這句前面有個^,表示把沒有數(shù)據(jù)和字母的字符的字符串定義為缺失。tolower(rawtext)
是把數(shù)據(jù)轉成小寫。
寫好程序后咱們運行一下
data$review <- sapply(data$review, cleanText)
這樣數(shù)據(jù)就被清洗一遍了,接下來咱們需要使用tidytext包中的unnest_tokens函數(shù)先把評論打散,變成一個個的單詞,然后把含有stop的單詞去掉,再把每行重復的詞去掉,最后選擇大于3個字符的詞
tidydata <- data %>%
unnest_tokens(word, review) %>% #將句子打散變成單個詞
anti_join(stop_words) %>% #Joining with `by = join_by(word)` remove stop words
distinct() %>% #去除重復
filter(nchar(word) > 3)
我們看下整理后的數(shù)據(jù),我們可以看到同一行被拆成多個詞,當然數(shù)據(jù)也比原來大了很多
接下來咱們需要使用get_sentiments函數(shù)來對文本進行分析,它自帶有很多字典咱們這次使用"bing"字典進行分析,咱們先來看下什么是"bing"字典
head(get_sentiments("bing"),20)
我們可以看到字典就是對應的字符串,假如匹配到abnormal 這個詞,函數(shù)就會返回負面的negative,假如是abound這個詞,函數(shù)就會返回正面的positive
tidydata %>%
inner_join(get_sentiments("bing")) #使用"bing"的字典進行情感分析
咱們看到數(shù)據(jù)很大,咱們只取其中的4種藥物來分析"Levothyroxine",“Vyvanse”,“Xiidra”,“Oseltamivir”,并且計算出每種藥物的評價數(shù)量和百分比
drug_polarity <- tidydata %>%
inner_join(get_sentiments("bing")) %>% #使用"bing"的字典進行情感分析
filter(drugName == "Levothyroxine" | #選定4種藥物
drugName == "Vyvanse" |
drugName == "Xiidra" |
drugName == "Oseltamivir") %>%
count(sentiment, drugName) %>% #對情感進行計數(shù)
pivot_wider(names_from = sentiment, #選擇要訪問的列
values_from = n, #輸出列的名字
values_fill = 0) %>% #如果缺失的話默認填0
mutate(polarity = positive - negative, #評分
percent_positive = positive/(positive+negative) * 100) %>% #計算百分比
arrange(desc(percent_positive))
上圖對顯示出患者對藥物的一些基本反饋。
下面咱們準備開始進行無監(jiān)督學習,先要建立矩陣(DTM),
drug_as_doc_dtm <- tidydata %>%
count(drugName, word, sort = TRUE) %>% #每種藥物的評價詞語的個數(shù)
ungroup() %>%
cast_dtm(drugName, word, n) %>% #將數(shù)據(jù)幀轉換為tm包中DocumentTermMatrix,TermDocumentMatrix或dfm
removeSparseTerms(0.995)
我們看一下這個矩陣
inspect(drug_as_doc_dtm)
建立好矩陣后主要是通過topicmodels包的LDA函數(shù)來進行無監(jiān)督學習,這里的K表示你想要分成幾組,control這里可以設置一個種子
lda<- LDA(drug_as_doc_dtm, k = 3,
control = list(seed = 123))
接著咱們對數(shù)據(jù)進行進一步提取
top_terms_per_topic <- lda %>%
tidy(matrix = "beta") %>% #獲取系數(shù)
group_by(topic) %>% #分組
arrange(topic, desc(beta)) %>% #排序
slice(seq_len(10)) # Number of words to display per topic
看下提取后的數(shù)據(jù),第一個是組別,第二個是它的名字,第三個是它的beta
接下來咱們可以做一些簡單的可視化,加入咱們想看這些詞的幾率
ggplot(top_terms_per_topic, aes(x = beta, y = term, fill = term)) +
geom_bar(stat = "identity", color = "black")
或者做個詞云圖
library(wordcloud)
wordcloud(top_terms_per_topic$term,top_terms_per_topic$beta,scale=c(3,0.3),min.freq=-Inf,
max.words=Inf,colors=brewer.pal(8,'Set1'),random.order=F,random.color=F,ordered.colors=F)
本期先介紹到這里,下期繼續(xù)介紹有監(jiān)督學習,未完待續(xù)。文章來源:http://www.zghlxwxcb.cn/news/detail-758087.html
參考文獻:文章來源地址http://www.zghlxwxcb.cn/news/detail-758087.html
- tm包文檔
- tidytext包文檔
- topicmodels包文檔
- Harrison, C.J., Sidey-Gibbons, C.J. Machine learning in medicine: a practical introduction to natural language processing. BMC Med Res Methodol 21, 158 (2021).
- https://www.cnblogs.com/jiangxinyang/p/9358339.html
- https://blog.csdn.net/sinat_26917383/article/details/51547298
到了這里,關于R語言對醫(yī)學中的自然語言(NLP)進行機器學習處理(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!