中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)頁設(shè)計(jì)職位優(yōu)化大師下載安裝app

網(wǎng)頁設(shè)計(jì)職位,優(yōu)化大師下載安裝app,網(wǎng)頁設(shè)計(jì)師證書報(bào)考條件,漳州 做網(wǎng)站一、說明 關(guān)于文本分類,文章已經(jīng)很多,本文這里有實(shí)操代碼,明確而清晰地表述這種過程,是實(shí)戰(zhàn)工程師所可以參照和依賴的案例版本。 本文是 2023 年 1 月的 WomenWhoCode 數(shù)據(jù)科學(xué)跟蹤活動(dòng)提供的會議系列文章中的一篇。 之前的文章在…

一、說明

關(guān)于文本分類,文章已經(jīng)很多,本文這里有實(shí)操代碼,明確而清晰地表述這種過程,是實(shí)戰(zhàn)工程師所可以參照和依賴的案例版本。 本文是 2023 年 1 月的 WomenWhoCode 數(shù)據(jù)科學(xué)跟蹤活動(dòng)提供的會議系列文章中的一篇。
????????之前的文章在這里:第 2 部分(涵蓋 NLP 簡介)、第?3?部分(涵蓋 NLTK 和 SpaCy 庫)、第 4?部分(涵蓋文本預(yù)處理技術(shù))、第 <> 部分(涵蓋文本表示技術(shù))。

二、什么是文本分類?

  • 文本分類是指將一段文本(例如,客戶評論、電子郵件、網(wǎng)頁、新聞文章)分類為一些預(yù)定義的類別或類。正面、負(fù)面或中立的評論評論、垃圾郵件或非垃圾郵件、作為個(gè)人或商業(yè)頁面的網(wǎng)頁、有關(guān)政治、體育或金融的新聞文章)
  • 即,文本分類是為給定文本分配標(biāo)簽或類的任務(wù)。例如,將電子郵件歸類為垃圾郵件。出于分類的目的,通常從輸入文本中識別出一些信息量很大的特征。
  • 監(jiān)督機(jī)器學(xué)習(xí)和無監(jiān)督學(xué)習(xí)可用于 NLP 中的文本分類。監(jiān)督學(xué)習(xí)涉及在標(biāo)記的數(shù)據(jù)集上訓(xùn)練分類器。無監(jiān)督學(xué)習(xí)不需要標(biāo)記的數(shù)據(jù)集,它使用數(shù)據(jù)的固有屬性(相似性)將數(shù)據(jù)聚類到組中。高質(zhì)量的標(biāo)記數(shù)據(jù),通常來自人類注釋者,對于監(jiān)督機(jī)器學(xué)習(xí)非常重要。標(biāo)記的數(shù)據(jù)通常分為 3 個(gè)部分,訓(xùn)練集、驗(yàn)證集和測試集。分類器的性能使用準(zhǔn)確率、精確度、召回率和 F1 分?jǐn)?shù)等指標(biāo)進(jìn)行評估。

三、文本分類的重要用例:

  1. 情緒分析
  2. POS 標(biāo)簽
  3. 自然語言推理 — 推斷兩段文本之間的關(guān)系——前提和假設(shè)。這種關(guān)系的類型——蘊(yùn)涵性、矛盾性和中性性。
  • 蘊(yùn)涵:假設(shè)由前提支持
  • 矛盾:假設(shè)被前提否定
  • 中性:假設(shè)和前提之間沒有關(guān)系 例如,
  • 前提:我現(xiàn)在正在看電影。
  • 假設(shè):我現(xiàn)在正在打板球。關(guān)系標(biāo)簽:矛盾

4. 檢查語法正確性:可接受/不可接受。

四、使用 NLTK 進(jìn)行文本分類

  • 對于文本分類的第一個(gè)示例,我們將使用?nltk?庫中內(nèi)置的movie_reviews語料庫。
  • 您可以使用 nltk.download 函數(shù)下載 movie_reviews 包:?import nltk nltk.download("movie_reviews")

????????fileids() 方法允許我們訪問 nltk.corpus 中數(shù)據(jù)集中所有文件的列表。movie_reviews數(shù)據(jù)集有 2000 個(gè)文本文件,每個(gè)文件都有一個(gè) fileid。這些文件中的每一個(gè)都包含對電影的評論。其中 1000 個(gè)文件包含負(fù)面評論,1000 個(gè)包含正面評論。負(fù)面文件位于名為“neg”的文件夾中,所有包含正面評論的文件都位于名為“pos”的文件夾中。

#Required imports
import nltk
import random
from nltk.corpus import movie_reviews#Total no. of review files in corpus
##There are 1000 negative reviews, and 1000 positive reviews (one review per file)
len(movie_reviews.fileids())#separating filenames in two lists one for positive reviews, one for negative reviews(based on which folder they exists in corpus)
negative_fileids = movie_reviews.fileids('neg')
positive_fileids = movie_reviews.fileids('pos')# Now we will load all reviews and their labels (i.e., folder name pos or neg in which the review file is present)reviewswithcategory = [(list(movie_reviews.words(fileid)), category) for category in movie_reviews.categories()for fileid in movie_reviews.fileids(category)]
random.shuffle(reviewswithcategory)

接下來,讓我們做一些文本預(yù)處理:

# Text pre-processing - lower casing, removing stop words and punctuation marks
import string
from nltk.corpus import stopwords
stop_words = stopwords.words('english')
def text_preprocessing(review):review = [w.lower() for w in review]review = [w.translate(str.maketrans('', '', string.punctuation)) for w in review]review = [w for w in review if w not in stop_words]review = list(filter(None, review)) #Remove empty stringsreturn reviewcleaned_reviewswithcategory = []
for review, cat in reviewswithcategory:cleanreview = text_preprocessing(review)cleaned_reviewswithcategory.append((cleanreview, cat))# Our variable cleaned_reviewswithcategory is a list of tuples, 
# each tuple in it has a list of words and category label# here we all getting all words from all tuples into a single iterable
allcleanwords = list(itertools.chain(*cleaned_reviewswithcategory))
allcleanwordslist = []
for m in range(len(allcleanwords)):# traversing the inner listsfor n in range (len(allcleanwords[m])):# Add each element to the result listallcleanwordslist.append(allcleanwords[m][n])

接下來,我們從電影評論數(shù)據(jù)中清理過的單詞列表中確定 5000 個(gè)最常見的單詞

# Using NLTK FreqDist for computing word frequencies
freqd = nltk.FreqDist(allcleanwordslist)
# Identifying 5000 most frequent words from Frequency Distribution
frequent_words = list(freqd.keys())[:5000]

現(xiàn)在,我們將只使用這 5000 個(gè)單詞。對于正面和負(fù)面類別中的每條評論,特征向量將包含這些常用詞和一個(gè)布爾值 True(如果該詞存在于該評論中),否則為 False。

# Identify the presence of these most frequent words in out positive and negative reviews.
# This function returns word and True if word is present, else it returns word and False. def extract_frequentwordfeatures(text):words = set(text) # computing all unique words (vocabulary) in input textfeatures = {}for w in frequent_words:features[w] = (w in words)return featuresreview_features = [(extract_frequentwordfeatures(review), category) for (review, category) in cleaned_reviewswithcategory]

????????現(xiàn)在,每個(gè)評論都由其特征表示,該特征是一個(gè)單詞列表和一個(gè)布爾值 True 或 False,指示該單詞是否存在于評論中。列表中共有 2000 條評論。接下來,我們將評審功能拆分為訓(xùn)練和測試部分。在 2000 條評論中,我們將使用 1800 條來訓(xùn)練來自 NLTK 的樸素貝葉斯分類器,并使用 200 條來測試其性能。

# Splitting the documents into training and test portions
train_data = review_features[:1800]
# set that we'll test against.
test_data = review_features[1800:]# use Naive Bayes classifier from NLTK to train 
clf_nb = nltk.NaiveBayesClassifier.train(train_data)# After training, let us see the accuracy
print(" Accuracy:",(nltk.classify.accuracy(clf_nb, test_data)))

五、使用 sklearn 分類器對上述評論數(shù)據(jù)進(jìn)行分類

from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score, confusion_matrixnames = ['Logistic Regression','Multinomial Naive Bayes', 'Support Vector Machine']classifiers = [LogisticRegression(),MultinomialNB(),SVC(kernel='linear')]
models = zip(names, classifiers)text_features, labels = zip(*test_data)for name, model in models:nltk_clf = SklearnClassifier(model)nltk_clf.train(train_data)accuracy = nltk.classify.accuracy(nltk_clf, test_data)print("\n{} Classifier Accuracy: {}".format(name, accuracy))  

六、使用 Keras 進(jìn)行文本分類

????????在這部分,我們將使用來自 UCI 存儲庫的 Sentiment Labelled Sentences 數(shù)據(jù)集。此數(shù)據(jù)集包含標(biāo)有正面或負(fù)面情緒的句子。情緒是分?jǐn)?shù)的形式,分?jǐn)?shù)是 1 表示積極情緒,0 表示消極情緒。這些句子來自三個(gè)不同的網(wǎng)站:imdb.com、amazon.com yelp.com 對于每個(gè)網(wǎng)站,存在 500 個(gè)正面句子和 500 個(gè)負(fù)面句子。在這里的示例中,我們將僅使用文件amazon_cells_labelled.txt中的亞馬遜評論。

????????首先,我們將使用 pandas 將數(shù)據(jù)讀入 pandas 數(shù)據(jù)幀。此數(shù)據(jù)有兩列 — 句子和標(biāo)簽。句子是產(chǎn)品評論,標(biāo)簽是 0 或 1。標(biāo)簽 1 表示積極情緒,0 表示消極情緒。

import pandas as pd
df = pd.read_csv('amazon_cells_labelled.txt', names=['sentence', 'label'], sep='\t')
df.head()

接下來,我們對句子列中的文本進(jìn)行預(yù)處理

# This process_text() function returns list of cleaned tokens of the text
import numpy
import re
import string
import unicodedata
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
stop_words = stopwords.words('english')
lemmatizer = WordNetLemmatizer()def process_text(text):text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode('utf-8', 'ignore')text = re.sub(r'[^a-zA-Z\s]', '', text)text = text.translate(str.maketrans('', '', string.punctuation))text = text.lower()text = " ".join([word for word in str(text).split() if word not in stop_words])text = " ".join([lemmatizer.lemmatize(word) for word in text.split()])return text
df['sentence'] = df['sentence'].apply(process_text)
df['sentence']

????????現(xiàn)在我們將數(shù)據(jù)分為訓(xùn)練和測試部分,在此之前,讓我們將“句子”列中的文本和“標(biāo)簽”列中的標(biāo)簽分為兩個(gè)pandas系列——“句子”和“標(biāo)簽”。


# Taking cleaned text and sentiment labels in two separate variables
sentences = df['sentence']
labels = df['label']# Splitting into train-test portions
from sklearn.model_selection import train_test_split
sentences_train, sentences_test, labels_train, labels_test = train_test_split(sentences, labels, test_size=0.25, random_state=1000)

接下來,我們使用 sklearn 中帶有 CountVectorizer 的詞袋模型以向量形式表示句子中的文本

# Converting sentences to vectors using Bag of words model with CountVectorizer
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer()
cv.fit(sentences_train)vc_traindata = cv.transform(sentences_train)
vc_testdata  = cv.transform(sentences_test)
vc_traindata

????????現(xiàn)在我們將使用 Keras 進(jìn)行分類,因此應(yīng)該在我們的計(jì)算機(jī)上安裝 TensorFlow 和 Keras 庫。可以使用 pip 命令從 Jupyter Notebook 中安裝它們

!pip install tensorflow
!pip install keras

首先,我們將使用 Keras Tokenizer 來計(jì)算文本的單詞嵌入

# Using Keras Tokenizer to compute embeddings for text
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(df['sentence'])# Take the sentence text in a variable X and labels in y.
X = df['sentence']
y = df['label']#Splitting X and y into train and test portions
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1000)#converting sentences into vector sequences
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)# Total number of unique words in our sentences
vocab_size = len(tokenizer.word_index) + 1  # Adding 1 because of reserved 0 index
print('Vocabulary size:' , vocab_size)

????????然后,我們將填充所有向量(代表產(chǎn)品評論的文本)的長度相同 — 50

# padding vector sequences to make them all of same length
from keras_preprocessing.sequence import pad_sequences
maxlen = 50
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
print(X_train[4, :])

七、使用 Keras 的嵌入層

????????我們現(xiàn)在將使用 Keras 的嵌入層,它采用先前計(jì)算的整數(shù)并將它們映射到嵌入的密集向量。它需要以下參數(shù):

  • input_dim:詞匯量的大小
  • output_dim: the size of the dense vector
  • input_length: the length of the sequence

????????我們可以獲取嵌入層的輸出并將其插入密集層。為此,我們需要在中間添加一個(gè) Flatten 層,為 Dense 層準(zhǔn)備順序輸入。

????????在訓(xùn)練期間,要訓(xùn)練的參數(shù)數(shù)vacab_size乘以embedding_dim。嵌入層的權(quán)重是隨機(jī)初始化的,然后在訓(xùn)練期間使用反向傳播進(jìn)行微調(diào)。該模型將按句子順序出現(xiàn)的單詞作為輸入向量

????????在下面的代碼中,我們使用嵌入層 GlobalMaxPool1D 進(jìn)行扁平化,以及由 15 個(gè)神經(jīng)元和一個(gè)輸出層組成的密集層。我們編譯了 keras 模型。

from keras.models import Sequential
from keras import layersembedding_dim = 100
maxlen = 50
model = Sequential()
model.add(layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maxlen))
model.add(layers.GlobalMaxPool1D())
model.add(layers.Dense(15, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.summary()

????????接下來,我們將模型擬合在 50 個(gè) epoch 的訓(xùn)練數(shù)據(jù)上,并評估其性能。使用 matplotlib 繪制模型的準(zhǔn)確性

import matplotlib.pyplot as plt
plt.figure(figsize=(9, 5))history = model.fit(X_train, y_train,epochs=50, verbose=False,validation_data=(X_test, y_test), batch_size=10)
loss, accuracy = model.evaluate(X_train, y_train, verbose=False)
print("Training Accuracy: {:.4f}".format(accuracy))
loss, accuracy = model.evaluate(X_test, y_test, verbose=False)
print("Testing Accuracy:  {:.4f}".format(accuracy))acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
x = range(1, len(acc) + 1)
plt.plot(x, acc, 'b', label='Training acc')
plt.plot(x, val_acc, 'r', label='Validation acc')
plt.legend()
plt.title('Training and validation accuracy')
plt.show()

????????在下一篇文章中,我們將看看變形金剛。代碼出處尼姆里塔·庫爾

參考和引用:

  1. https://developers.google.com/machine-learning/guides/text-classification
  2. Text Classification with Python and Scikit-Learn
  3. https://towardsdatascience.com/a-practitioners-guide-to-natural-language-processing-part-i-processing-understanding-text-9f4abfd13e72
  4. Text Classification using NLTK | Foundations of AI & ML
  5. Practical Text Classification With Python and Keras – Real Python
  6. Text Classification with NLTK | Chan`s Jupyter
  7. https://www.analyticsvidhya.com/blog/2018/04/a-comprehensive-guide-to-understand-and-implement-text-classification-in-python/
  8. https://medium.com/analytics-vidhya/nlp-tutorial-for-text-classification-in-python-8f19cd17b49e
  9. Practical Text Classification With Python and Keras – Real Python
http://www.risenshineclean.com/news/34863.html

相關(guān)文章:

  • 四川華泰建設(shè)集團(tuán)網(wǎng)站免費(fèi)做網(wǎng)站網(wǎng)站
  • wordpress 評論框 提示網(wǎng)頁優(yōu)化方案
  • 英文網(wǎng)站建設(shè)方法網(wǎng)站怎么收錄到百度
  • 信陽網(wǎng)站建設(shè)策劃方案廣東今日最新疫情通報(bào)
  • 酒類網(wǎng)站建設(shè)方案海南seo排名優(yōu)化公司
  • 黃石做網(wǎng)站的公司網(wǎng)絡(luò)營銷實(shí)施計(jì)劃
  • wordpress主題下新建頁面網(wǎng)站seo站外優(yōu)化
  • 杭州百度推廣公司有幾家手機(jī)優(yōu)化軟件排行
  • 網(wǎng)站建設(shè)的公司哪家是上市公司電商培訓(xùn)基地
  • 網(wǎng)站建設(shè)公司怎么做業(yè)務(wù)aso優(yōu)化教程
  • 瀘州市住房與城鄉(xiāng)建設(shè)局網(wǎng)站google免費(fèi)入口
  • 珠海網(wǎng)站制作首頁上線了建站
  • 怎么購買網(wǎng)站空間免費(fèi)廣告發(fā)布平臺
  • 2023年企業(yè)年報(bào)入口推動(dòng)防控措施持續(xù)優(yōu)化
  • wordpress+4.5+多站點(diǎn)手機(jī)百度免費(fèi)下載
  • 網(wǎng)站設(shè)計(jì)怎么做創(chuàng)建自己的網(wǎng)站怎么弄
  • 閔行網(wǎng)站設(shè)計(jì)seo專家是什么意思
  • 六安建設(shè)局網(wǎng)站百度搜索關(guān)鍵詞數(shù)據(jù)
  • bec聽力哪個(gè)網(wǎng)站做的好網(wǎng)站制作公司排名
  • wordpress tag 別名北京優(yōu)化seo公司
  • 石家莊百度推廣家莊網(wǎng)站建設(shè)提高搜索引擎檢索效果的方法
  • 成都網(wǎng)站排名 生客seo自己搭建網(wǎng)站
  • 網(wǎng)站內(nèi)地圖位置怎么做制作app軟件平臺
  • wordpress如何上傳超過2m合肥seo網(wǎng)站排名
  • 公安廳網(wǎng)站 做10道相關(guān)題目2022年小學(xué)生新聞?wù)畻l
  • 河南網(wǎng)站制作線上銷售平臺有哪些
  • 貴州省網(wǎng)站節(jié)約化建設(shè)通知公司網(wǎng)址怎么制作
  • php網(wǎng)站開發(fā)需要什么軟件友情鏈接獲取的途徑有哪些
  • 網(wǎng)站后臺視頻app開發(fā)公司哪家好
  • 有關(guān)做聚合物電池公司的網(wǎng)站什么是網(wǎng)絡(luò)營銷渠道