郝 玉华

郝 玉华

1657276560

在 Python 中执行情感分析的 5 种方法

无论您说的是 Twitter、Goodreads 还是亚马逊——几乎没有一个数字空间不充满人们的意见。在当今世界,组织挖掘这些意见并获得有关其产品或服务的见解至关重要。然而,这些数据以如此惊人的数量存在,以至于手动测量它几乎是不可能的追求。这就是数据科学的另一个好处 ——情绪分析。在本文中,我们将探讨情感分析包含的内容以及在 Python 中实现它的各种方法。

什么是情绪分析?

情感分析自然语言处理 (NLP)的一个用例,属于文本分类的范畴。简而言之,情感分析涉及将文本分类为各种情感,如正面或负面、快乐、悲伤或中性等。因此,情感分析的最终目标是破译一个潜在的情绪、情绪或情绪。文本。这也称为意见挖掘

让我们看看快速谷歌搜索如何定义情绪分析:

情绪分析定义

通过情绪分析获得洞察力并做出决策

好吧,现在我想我们已经有点习惯了情绪分析是什么。但它的意义是什么?组织如何从中受益?让我们尝试用一个例子来探索一下。假设您创办了一家在在线平台上销售香水的公司。你推出了种类繁多的香水,很快顾客就蜂拥而至。一段时间后,你决定改变香水的定价策略——你计划提高流行香水的价格,同时为不受欢迎的香水提供折扣. 现在,为了确定哪些香水受欢迎,您开始查看所有香水的客户评论。但是你被困住了!它们是如此之多,以至于您无法在一生中将它们全部看完。这就是情绪分析可以让你摆脱困境的地方。

您只需将所有评论收集在一个地方并对其应用情绪分析。以下是对三种香水——薰衣草、玫瑰和柠檬的评论的情感分析示意图。(请注意,这些评论可能有不正确的拼写、语法和标点符号,就像在现实世界中一样)

情绪分析

从这些结果中,我们可以清楚地看到:

Fragrance-1(薰衣草)得到了客户的高度好评,这表明贵公司可以根据其受欢迎程度提高其价格。

Fragrance-2 (Rose)恰好在客户中持中立态度,这意味着贵公司不应改变其定价

Fragrance-3(柠檬)具有与之相关的整体负面情绪 - 因此,您的公司应考虑为其提供折扣以平衡规模。

这只是一个简单的示例,说明情绪分析如何帮助您深入了解您的产品/服务并帮助您的组织做出决策。

情绪分析用例

我们刚刚看到了情绪分析如何为组织提供洞察力,帮助他们做出数据驱动的决策。现在,让我们来看看更多情感分析的用例。

  1. 品牌管理的社交媒体监控:品牌可以使用情绪分析来衡量其品牌的公众形象。例如,公司可以收集所有带有公司提及或标签的推文,并执行情绪分析以了解公司的公众前景。
  2. 产品/服务分析:品牌/组织可以对客户评论进行情绪分析,以了解产品或服务在市场上的表现,并据此做出未来决策。
  3. 股价预测:预测一家公司的股票是涨还是跌,对投资者来说至关重要。可以通过对包含公司名称的文章的新闻标题进行情绪分析来确定相同的结果。如果与特定组织有关的新闻头条恰好具有积极情绪——其股价应该会上涨,反之亦然。

在 Python 中执行情感分析的方法

在执行数据科学任务时,Python 是最强大的工具之一——它提供了多种执行 情感分析的方法。这里列出了最受欢迎的:

  1. 使用文本 Blob
  2. 使用维达
  3. 使用基于词向量化的模型
  4. 使用基于 LSTM 的模型
  5. 使用基于 Transformer 的模型

让我们一一深入了解它们。

注意:为了演示方法 3 和 4(使用基于词向量化的模型和使用基于 LSTM 的模型)的情感分析。它包含 5000 多个标记为正面、负面或中性的文本摘录。该数据集位于知识共享许可下。

使用文本 Blob

Text Blob 是一个用于自然语言处理的 Python 库。使用 Text Blob 进行情绪分析非常简单。它将文本作为输入,并可以返回极性主观性作为输出。

极性决定了文本的情绪。它的值位于 [-1,1] 中,其中 -1 表示高度负面的情绪,1 表示高度正面的情绪。

主观性决定了文本输入是事实信息还是个人观点。它的值介于 [0,1] 之间,其中接近 0 的值表示一条事实信息,接近 1 的值表示个人意见。

安装

pip install textblob

导入文本块:

from textblob import TextBlob

使用文本 Blob 进行情感分析的代码实现:

使用 TextBlob 编写情绪分析代码相当简单。只需导入 TextBlob 对象并使用适当的属性传递要分析的文本,如下所示:

from textblob import TextBlob
text_1 = "The movie was so awesome."
text_2 = "The food here tastes terrible."#Determining the Polarity 
p_1 = TextBlob(text_1).sentiment.polarity
p_2 = TextBlob(text_2).sentiment.polarity#Determining the Subjectivity
s_1 = TextBlob(text_1).sentiment.subjectivity
s_2 = TextBlob(text_2).sentiment.subjectivityprint("Polarity of Text 1 is", p_1)
print("Polarity of Text 2 is", p_2)
print("Subjectivity of Text 1 is", s_1)
print("Subjectivity of Text 2 is", s_2)

输出:

Polarity of Text 1 is 1.0 
Polarity of Text 2 is -1.0 
Subjectivity of Text 1 is 1.0 
Subjectivity of Text 2 is 1.0

使用 VADER

VADER(Valence Aware Dictionary and sEntiment Reasoner)是一个基于规则的情感分析器,已经在社交媒体文本上进行了训练。就像 Text Blob 一样,它在 Python 中的使用非常简单。稍后我们将通过一个示例来了解它在代码实现中的用法。

安装:

pip install vaderSentiment

从 Vader 导入 SentimentIntensityAnalyzer 类:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

使用 Vader 进行情绪分析的代码:

首先,我们需要创建一个 SentimentIntensityAnalyzer 类的对象;然后我们需要将文本传递给对象的 polar_scores() 函数,如下所示:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sentiment = SentimentIntensityAnalyzer()
text_1 = "The book was a perfect balance between wrtiting style and plot."
text_2 =  "The pizza tastes terrible."
sent_1 = sentiment.polarity_scores(text_1)
sent_2 = sentiment.polarity_scores(text_2)
print("Sentiment of text 1:", sent_1)
print("Sentiment of text 2:", sent_2)

输出

Sentiment of text 1: {'neg': 0.0, 'neu': 0.73, 'pos': 0.27, 'compound': 0.5719} 
Sentiment of text 2: {'neg': 0.508, 'neu': 0.492, 'pos': 0.0, 'compound': -0.4767}

正如我们所见,VaderSentiment 对象返回要分析的文本的情绪分数字典。

使用基于词向量化的模型

在目前讨论的两种方法中,即 Text Blob 和 Vader,我们只是使用 Python 库来执行情绪分析。现在我们将讨论一种方法,在该方法中,我们将为该任务训练我们自己的模型。使用词袋向量化方法执行情感分析的步骤如下:

  1. 预处理训练数据的文本(文本预处理包括规范化、标记化、停用词去除和词干/词形还原。)
  2. 使用计数向量化或 TF-IDF 向量化方法为预处理的文本数据创建词袋。
  3. 在处理后的数据上训练合适的分类模型以进行情感分类。

使用词袋向量化方法进行情感分析的代码:

要使用 BOW 矢量化方法构建情绪分析模型,我们需要一个标记数据集。如前所述,用于此演示的数据集是从 Kaggle 获得的。我们简单地使用了 sklearn 的计数向量器来创建 BOW。之后,我们训练了一个多项朴素贝叶斯分类器,其准确度得分为 0.84。

数据集可以从这里获得。

#Loading the Dataset
import pandas as pd
data = pd.read_csv('Finance_data.csv')
#Pre-Prcoessing and Bag of Word Vectorization using Count Vectorizer
from sklearn.feature_extraction.text import CountVectorizer
from nltk.tokenize import RegexpTokenizer
token = RegexpTokenizer(r'[a-zA-Z0-9]+')
cv = CountVectorizer(stop_words='english',ngram_range = (1,1),tokenizer = token.tokenize)
text_counts = cv.fit_transform(data['sentences'])
#Splitting the data into trainig and testing
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(text_counts, data['feedback'], test_size=0.25, random_state=5)
#Training the model
from sklearn.naive_bayes import MultinomialNB
MNB = MultinomialNB()
MNB.fit(X_train, Y_train)
#Caluclating the accuracy score of the model
from sklearn import metrics
predicted = MNB.predict(X_test)
accuracy_score = metrics.accuracy_score(predicted, Y_test)
print("Accuracuy Score: ",accuracy_score)

输出

Accuracuy Score:  0.9111675126903553

经过训练的分类器可用于预测任何给定文本输入的情绪。

使用基于 LSTM 的模型

虽然我们能够使用词袋矢量化方法获得不错的准确度分数,但在处理更大的数据集时可能无法产生相同的结果。这就需要使用基于深度学习的模型来训练情感分析模型。

对于 NLP 任务,我们通常使用基于 RNN 的模型,因为它们旨在处理顺序数据。在这里,我们将使用TensorFlowKeras训练一个 LSTM(长短期记忆)模型。使用基于 LSTM 的模型执行情感分析的步骤如下:

  1. 预处理训练数据的文本(文本预处理包括规范化、标记化、停用词去除和词干/词形还原。)
  2. 从 Keras.preprocessing.text导入Tokenizer并创建它的对象。在整个训练文本上拟合标记器(以便标记器在训练数据词汇表上得到训练)。使用 Tokenizer 的 texts_to_sequence() 方法生成文本嵌入,并在将它们填充到相等长度后存储它们。(嵌入是文本的数字/矢量化表示。由于我们不能直接为模型提供文本数据,我们首先需要将它们转换为嵌入)
  3. 生成嵌入后,我们就可以构建模型了。我们使用 TensorFlow 构建模型——向其中添加输入、LSTM 和密集层。添加 dropout 并调整超参数以获得不错的准确度分数。通常,我们倾向于在 LSTM 模型的内层使用ReLULeakyReLU激活函数,因为它避免了梯度消失问题。在输出层,我们使用 Softmax 或 Sigmoid 激活函数。

使用基于 LSTM 的模型方法进行情感分析的代码:

在这里,我们使用了与 BOW 方法相同的数据集。获得了 0.90 的训练准确度。

#Importing necessary libraries
import nltk
import pandas as pd
from textblob import Word
from nltk.corpus import stopwords
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from keras.models import Sequential
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Embedding, LSTM, SpatialDropout1D
from sklearn.model_selection import train_test_split 
#Loading the dataset
data = pd.read_csv('Finance_data.csv')
#Pre-Processing the text 
def cleaning(df, stop_words):
    df['sentences'] = df['sentences'].apply(lambda x: ' '.join(x.lower() for x in x.split()))
    # Replacing the digits/numbers
    df['sentences'] = df['sentences'].str.replace('d', '')
    # Removing stop words
    df['sentences'] = df['sentences'].apply(lambda x: ' '.join(x for x in x.split() if x not in stop_words))
    # Lemmatization
    df['sentences'] = df['sentences'].apply(lambda x: ' '.join([Word(x).lemmatize() for x in x.split()]))
    return df
stop_words = stopwords.words('english')
data_cleaned = cleaning(data, stop_words)
#Generating Embeddings using tokenizer
tokenizer = Tokenizer(num_words=500, split=' ') 
tokenizer.fit_on_texts(data_cleaned['verified_reviews'].values)
X = tokenizer.texts_to_sequences(data_cleaned['verified_reviews'].values)
X = pad_sequences(X)
#Model Building
model = Sequential()
model.add(Embedding(500, 120, input_length = X.shape[1]))
model.add(SpatialDropout1D(0.4))
model.add(LSTM(704, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(352, activation='LeakyReLU'))
model.add(Dense(3, activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])
print(model.summary())
#Model Training
model.fit(X_train, y_train, epochs = 20, batch_size=32, verbose =1)
#Model Testing
model.evaluate(X_test,y_test)

使用基于 Transformer 的模型

基于 Transformer 的模型是最先进的自然语言处理技术之一。它们遵循基于编码器-解码器的架构,并采用自我注意的概念来产生令人印象深刻的结果。虽然总是可以从头开始构建变压器模型,但这是一项相当乏味的任务。因此,我们可以使用Hugging Face上可用的预训练变压器模型。Hugging Face 是一个开源 AI 社区,为 NLP 应用程序提供大量预训练模型。这些模型可以原样使用,也可以针对特定任务进行微调。

安装:

pip install transformers

从 Vader 导入 SentimentIntensityAnalyzer 类:

import transformers

使用基于 Transformer 的模型进行情绪分析的代码:

要使用转换器执行任何任务,我们首先需要从转换器导入管道功能。然后,创建管道函数的对象并将要执行的任务作为参数传递(即在我们的案例中进行情感分析)。我们还可以指定我们需要用来执行任务的模型。这里,由于我们没有提到要使用的模型,所以默认使用 distillery-base-uncased-finetuned-sst-2-English 模式进行情感分析。您可以在此处查看可用任务和模型的列表。

from transformers import pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
data = ["It was the best of times.", "t was the worst of times."]
sentiment_pipeline(data)Output:[{'label': 'POSITIVE', 'score': 0.999457061290741},  {'label': 'NEGATIVE', 'score': 0.9987301230430603}]

结论

在这个用户可以毫不费力地表达他们的观点并且在几分之一秒内生成多余的数据的时代——从这些数据中获得洞察力对于组织做出有效决策至关重要——而情绪分析被证明是拼图中缺失的部分!

到目前为止,我们已经非常详细地介绍了情感分析的确切含义以及可以用来在 Python 中执行它的各种方法。但这些只是一些基本的演示——你一定要继续摆弄模型,并在你自己的数据上进行尝试。

资料来源:https ://www.analyticsvidhya.com/blog/2022/07/sentiment-analysis-using-python/

#python 

What is GEEK

Buddha Community

在 Python 中执行情感分析的 5 种方法
Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Shardul Bhatt

Shardul Bhatt

1626775355

Why use Python for Software Development

No programming language is pretty much as diverse as Python. It enables building cutting edge applications effortlessly. Developers are as yet investigating the full capability of end-to-end Python development services in various areas. 

By areas, we mean FinTech, HealthTech, InsureTech, Cybersecurity, and that's just the beginning. These are New Economy areas, and Python has the ability to serve every one of them. The vast majority of them require massive computational abilities. Python's code is dynamic and powerful - equipped for taking care of the heavy traffic and substantial algorithmic capacities. 

Programming advancement is multidimensional today. Endeavor programming requires an intelligent application with AI and ML capacities. Shopper based applications require information examination to convey a superior client experience. Netflix, Trello, and Amazon are genuine instances of such applications. Python assists with building them effortlessly. 

5 Reasons to Utilize Python for Programming Web Apps 

Python can do such numerous things that developers can't discover enough reasons to admire it. Python application development isn't restricted to web and enterprise applications. It is exceptionally adaptable and superb for a wide range of uses.

Robust frameworks 

Python is known for its tools and frameworks. There's a structure for everything. Django is helpful for building web applications, venture applications, logical applications, and mathematical processing. Flask is another web improvement framework with no conditions. 

Web2Py, CherryPy, and Falcon offer incredible capabilities to customize Python development services. A large portion of them are open-source frameworks that allow quick turn of events. 

Simple to read and compose 

Python has an improved sentence structure - one that is like the English language. New engineers for Python can undoubtedly understand where they stand in the development process. The simplicity of composing allows quick application building. 

The motivation behind building Python, as said by its maker Guido Van Rossum, was to empower even beginner engineers to comprehend the programming language. The simple coding likewise permits developers to roll out speedy improvements without getting confused by pointless subtleties. 

Utilized by the best 

Alright - Python isn't simply one more programming language. It should have something, which is the reason the business giants use it. Furthermore, that too for different purposes. Developers at Google use Python to assemble framework organization systems, parallel information pusher, code audit, testing and QA, and substantially more. Netflix utilizes Python web development services for its recommendation algorithm and media player. 

Massive community support 

Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions. 

Today, numerous universities start with Python, adding to the quantity of individuals in the community. Frequently, Python designers team up on various tasks and help each other with algorithmic, utilitarian, and application critical thinking. 

Progressive applications 

Python is the greatest supporter of data science, Machine Learning, and Artificial Intelligence at any enterprise software development company. Its utilization cases in cutting edge applications are the most compelling motivation for its prosperity. Python is the second most well known tool after R for data analytics.

The simplicity of getting sorted out, overseeing, and visualizing information through unique libraries makes it ideal for data based applications. TensorFlow for neural networks and OpenCV for computer vision are two of Python's most well known use cases for Machine learning applications.

Summary

Thinking about the advances in programming and innovation, Python is a YES for an assorted scope of utilizations. Game development, web application development services, GUI advancement, ML and AI improvement, Enterprise and customer applications - every one of them uses Python to its full potential. 

The disadvantages of Python web improvement arrangements are regularly disregarded by developers and organizations because of the advantages it gives. They focus on quality over speed and performance over blunders. That is the reason it's a good idea to utilize Python for building the applications of the future.

#python development services #python development company #python app development #python development #python in web development #python software development

Art  Lind

Art Lind

1602968400

Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development

Art  Lind

Art Lind

1602666000

How to Remove all Duplicate Files on your Drive via Python

Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.

Intro

In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.

Heres a solution

Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.

But How do we do it?

If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?

The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.

There’s a variety of hashing algorithms out there such as

  • md5
  • sha1
  • sha224, sha256, sha384 and sha512

#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…

You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.

1. init

class AnyClass:
    def __init__():
        print("Init called on its own")
obj = AnyClass()

The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

Init called on its own

2. add

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

class AnyClass:
    def __init__(self, var):
        self.some_var = var
    def __add__(self, other_obj):
        print("Calling the add method")
        return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2

#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python