多项式朴素贝叶斯分类器

In Analytics Vidhya, Hackathon, there was a problem statement for text prediction of topic/subject to which class it belongs basis on title and abstract. To solve this question of prediction problem I have applied Multinomial Naive Bayes classifier supervised algorithm.

在 Hackathon的Analytics Vidhya中,存在一个问题说明,用于根据标题和摘要对主题/主题进行文本预测。 为了解决这个预测问题,我应用了多项朴素贝叶斯分类器监督算法。

In this blog, I have covered the importance of the Naive Bayes classifier, its types, and the actual implementation of the algorithm for the given problem statement.

在此博客中,我介绍了朴素贝叶斯分类器的重要性,其类型以及针对给定问题陈述的算法的实际实现。

什么是朴素贝叶斯? 为什么? (What is Naive Bayes? and Why?)

Naive Bayes Classifier Algorithm is a family of probabilistic algorithms based on applying Bayes’ theorem with the “naive” on the basis of two following assumption:

朴素贝叶斯分类器算法是一系列概率算法,基于以下两个假设,将贝叶斯定理与“朴素”一起应用:

  1. Predictors are independent of each other.预测变量彼​​此独立。
  2. All features have an equal effect on the outcome.所有功能对结果都有同等的影响。

Bayes theorem calculates probability P(c|x) where c is the class of the possible outcomes and x is the given instance which has to be classified, representing some certain features.

贝叶斯定理计算概率P(c | x),其中c是可能结果的类别,x是必须分类的给定实例,代表某些特征。

P(c|x) = P(x|c) * P(c) / P(x)

P(c|x) = P(x|c) * P(c) / P(x)

Naive Bayes is mostly used in natural language processing (NLP) problems. Naive Bayes predicts the tag of a text. They calculate the probability of each tag for a given text and then output the tag with the highest one.

朴素贝叶斯主要用于自然语言处理(NLP)问题。 朴素贝叶斯(Naive Bayes)预测文本的标签。 他们计算给定文本的每个标签的概率,然后输出最高标签的标签。

朴素贝叶斯的类型 (Types of Naive Bayes)

  1. Multinomial Naive Bayes — Whether a document/topic belongs to a particular category. The features/predictors used by the classifier are the frequency of the words present in the document.多项式朴素贝叶斯-文档/主题是否属于特定类别。 分类器使用的功能/预测词是文档中出现的单词的频率。
  2. Bernoulli Naive Bayes- Similar to above, but only predicts the boolean variables, The parameters are used to predict the class variable yes or no, For example, a word occurs in the text or not.Bernoulli Naive Bayes-与上面类似,但仅预测布尔变量,该参数用于预测类变量yes或no,例如,文本中是否出现单词。
  3. Gaussian Naive Bayes: When the predictors take up the continuous value and are not discrete, we assume that values are sampled from Gaussian distribution.高斯朴素贝叶斯(Gaussian Naive Bayes):当预测变量采用连续值且不是离散值时,我们假设值是从高斯分布中采样的。

朴素贝叶斯的缺点: (Disadvantages of Naive Bayes:)

The requirement of predictors need to be independent.

预测变量的需求必须独立。

Hackathon问题: (Hackathon Problem:)

Given the abstract and title for a set of research articles, predict the topics for each article included in the test set. This can be read more into detail.

给定一组研究文章的摘要和标题,预测测试集中包含的每篇文章的主题。 这可以更详细地阅读。

So we have train.csv, test.csv, and sample_submission.csv. Now we have to build the model for prediction of the particular topic to which class it belongs.

因此,我们有了train.csv,test.csv和sample_submission.csv。 现在,我们必须构建模型以预测特定主题所属的类别。

Let’s see the actual implementation in detail. Code is available on Github

让我们详细了解实际的实现。 可以在Github上找到代码

import logging
import pandas as pd
import numpy as np
from numpy import random
import gensim
import nltk
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
import re
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
%matplotlib inline#Input the filename
train=pd.read_csv('train.csv',index_col=0)#Function to describe input data
def describe_data(df):print("Data Types:")print(df.dtypes)print("Rows and Columns:")print(df.shape)print("Column Names:")print(df.columns)print("Null Values:")print(df.apply(lambda x: sum(x.isnull()) / len(df)))describe_data(train)

In the below steps, we will train the model with text and for that, we need to convert it into the form of vectors.

在以下步骤中,我们将使用文本训练模型,为此,我们需要将其转换为向量形式。

#Consider the input values for X and y
X=train['TITLE']
y=train['Quantitative Finance']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)#Convert the text into vector form
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(strip_accents='ascii', token_pattern=u'(?ui)\\b\\w*[a-z]+\\w*\\b', lowercase=True, stop_words='english')
X_train_cv = cv.fit_transform(X_train)
X_test_cv = cv.transform(X_test)#Calculate the Word_freq count
word_freq_df = pd.DataFrame(X_train_cv.toarray(), columns=cv.get_feature_names())top_words_df = pd.DataFrame(word_freq_df.sum()).sort_values(0, ascending=False)
print(top_words_df)

Now, we will define the Multinomial model and train it. Also print classification report, Precision score, Recall, and accuracy.

现在,我们将定义多项式模型并对其进行训练。 还可以打印分类报告,精度得分,召回率和准确性。

#Training the model
from sklearn.naive_bayes import MultinomialNB
naive_bayes = MultinomialNB()
naive_bayes.fit(X_train_cv, y_train)
predictions = naive_bayes.predict(X_test_cv)#Accuracy and Classification report
from sklearn.metrics import accuracy_score, precision_score, recall_score,classification_report
print('Accuracy score: ', accuracy_score(y_test, predictions))
print('Precision score: ', precision_score(y_test, predictions))
print('Classification_report',classification_report(y_test,predictions))
print('Recall score: ', recall_score(y_test, predictions))from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
cm = confusion_matrix(y_test, predictions)
sns.heatmap(cm, square=True, annot=True, cmap='RdBu', cbar=False,
xticklabels=['0', '1'], yticklabels=['0', '1'])
plt.xlabel('true label')
plt.ylabel('predicted label')testing_predictions = []
for i in range(len(X_test)):if predictions[i] == 1:testing_predictions.append('1')else:testing_predictions.append('0')
check_df = pd.DataFrame({'actual_label': list(y_test), 'prediction': testing_predictions, 'TITLE':list(X_test)})
Accuracy score:  0.9876042908224076Precision score:  0.75Classification_report               precision    recall  f1-score   support           0       0.99      1.00      0.99      4141           1       0.75      0.06      0.10        54    accuracy                           0.99      4195   macro avg       0.87      0.53      0.55      4195weighted avg       0.98      0.99      0.98      4195Recall score:  0.05555555555555555
Predictions of labels
标签的预测

Now it’s time to apply the prediction for unknown data i.e test file. For that, we will pickle the model initially and then will load the model and save the predictions to the .csv file.

现在是时候对未知数据(即测试文件)应用预测了。 为此,我们将首先对模型进行酸洗,然后加载模型并将预测结果保存到.csv文件中。

#import the model in form of pickle
import pickle
with open('text_classifier', 'wb') as picklefile:pickle.dump(naive_bayes,picklefile)#Load the model
with open('text_classifier', 'rb') as training_model:model = pickle.load(training_model)#input the test file for prediction
test=pd.read_csv('n5.csv',error_bad_lines=False,skipinitialspace=False)
testlabel=test['TITLE']#transform the text into label
new_test = cv.transform(label)predictions2=model.predict(new_test)testing_predictions2 = []
for i in range(len(label)):check_df2 = pd.DataFrame({'Quantitative Finance':predictions2})test = test.set_index(check_df2.index)test['Quantitative Finance']=check_df2
testtest.to_csv('Submission.csv',index=False)

In the end, the submissions are submitted in the prescribed format.

最后,提交内容以规定的格式提交。

Enjoy predicting!!!

享受预测!

翻译自: https://medium.com/@monicamundada5/prediction-of-topics-using-multinomial-naive-bayes-classifier-2fb6f88e836f

多项式朴素贝叶斯分类器


http://www.taodudu.cc/news/show-4817720.html

相关文章:

  • 朴素贝叶斯分类器 详细解析
  • 朴素贝叶斯分类器算法通俗讲解
  • 数学建模-朴素贝叶斯分类器
  • python朴素贝叶斯分类器实现_用scikit-learn实现朴素贝叶斯分类器
  • 朴素贝叶斯分类器之分类实操
  • 朴素贝叶斯分类器NBC
  • python朴素贝叶斯分类示例_Python实现的朴素贝叶斯分类器示例
  • 机器学习算法——贝叶斯分类器3(朴素贝叶斯分类器)
  • php tp3.2 添加表内容,数据创建 · ThinkPHP3.2.3完全开发手册 · 看云
  • 力扣每日一题:878. 第 N 个神奇数字【二分法】
  • LeetCode-878. 第 N 个神奇数字【数学,二分查找,找规律】
  • 【每日一题Day35】LC878第N个神奇数字 | 二分查找 找规律 + 数学
  • 878. 第 N 个神奇数字 数学+二分
  • 神奇的数学之回文数(不论开始是什么正整数,在经过有限次正序数和倒序数相加的步骤后,都会得到一个回文数)
  • Java 7 使用TWR(Try-with-resources)完成文件copy
  • UWB测距 方法,双向双边测距法(DS-TWR)(四)
  • DWM1000 测距原理简单分析 之 SS-TWR
  • 利用Try-with-resources(TWR)读取Excel文件
  • linux启动时acpi错误,PE R720安装linux后报ACPI错误,请高手答疑
  • 一个UTILS判断来实现if else的equals多种判断结果
  • smcsuperio黑苹果_基于OpenCore0.6.1的黑苹果安装,小白也能看
  • 七、Docker:DockerFile
  • ubuntu18.04重装R
  • HDU - 5965
  • 集线器(Hub)、网线、网卡、交换机、路由器分别工作在OSI参考模型的哪一层?
  • singalrhub
  • Docker Hub官方地址
  • zoom走了,谁的机会来了
  • Linux 虚拟网络设备详解之 Bridge 网桥
  • 关于5G无线网络设备之间的连线总结

多项式朴素贝叶斯分类器_多项式朴素贝叶斯分类器的主题预测相关推荐

  1. 朴素贝叶斯 半朴素贝叶斯_使用朴素贝叶斯和N-Gram的Twitter情绪分析

    朴素贝叶斯 半朴素贝叶斯 In this article, we'll show you how to classify a tweet into either positive or negativ ...

  2. spring 两次进入拦截器_过滤器和拦截器的 6 个区别,别再傻傻分不清了

    点击上方 肉眼品世界,选择 设为星标 深度价值体系传递 作者 :程序员内点事 来源 :toutiao.com/i6834310440495874563 毕竟这两种工具开发中用到的频率都相当高,应用起来 ...

  3. 如何听节拍器_如何用节拍器卡节拍?节拍器的使用方法!

    大家好,我是雅风. 这篇文章讲节拍器如何去使用,如何用节拍器卡节拍? 节拍器一般分为两种,一种机械运转上发条,一种电子. 首先我来讲一下节拍器使用方法. 一.机械节拍器. 机械节拍器 这种节拍器不需要 ...

  4. java 解析器_高性能Java解析器实现过程详解

    如果你没有指定数据或语言标准的或开源的Java解析器, 可能经常要用Java实现你自己的数据或语言解析器.或者,可能有很多解析器可选,但是要么太慢,要么太耗内存,或者没有你需要的特定功能.或者开源解析 ...

  5. 如何听节拍器_怎么听节拍器视频

    今天的吉他培训班,来给大家说说.时间就是一切吉他手们最喜欢那些新推出的小玩意--像变调夹.连线.踏板.电子调音器和效果器等等,但是节拍器总是受到忽视.然而,正确地(甚至是创造性地)使用节拍器的技巧,比 ...

  6. 几何着色器着色器_使用金属着色器制作第一个圆圈

    几何着色器着色器 Metal Shaders? Render Pipeline? Vertex Shaders? Fragment Shaders? If you were anything like ...

  7. 多项式加法 java 链表_多项式加法,用单链表实现。

    ---恢复内容开始--- #include #include typedef struct PolyNode *Polynomial; struct PolyNode{ int coef;//系数 i ...

  8. nod32用户名获取器_内置调试器的nod​​ejs

    nod32用户名获取器 Let's imagine that I have a simple JavaScript file and I want to calculate the sum of in ...

  9. 文件管理器_苹果超强文件管理器,秒变安卓?

    苹果优质应用推荐:Phone Drive Phone Drive - 云储存管理和文件共享是一款强大实用的文件管理工具. 可以实现 iPhone 到 iPhone/iPod Touch.iPhone ...

最新文章

  1. mysql报错无效默认值1067_Mysql 报错:#1067 - Invalid default value for 'update_time
  2. qcustomplot删除一条曲线_微凉秋日的成熟风穿搭,选一条V领连衣裙搭配,优雅知性显身材...
  3. Mysql(1)——服务端与客户端建立连接
  4. scale 和 transform-origin 实现线条从左侧进入,右侧离开效果
  5. 牛客网-数据结构笔试题目(三)-博弈论圆圈游戏(Circle Game)(附源码)
  6. php 的html文件怎么打开,什么是html文件?html格式如何打开?(图)
  7. 从新获取jar_hadoop3.0新特性总结
  8. jquery学习--对象
  9. C语言自己编写头文件
  10. 概念区分:灰度发布、蓝绿发布、滚动发布
  11. Ubuntu息屏后唤醒的花屏问题
  12. radmi4a Android,红米4A(Redmi 4A 全网通)一键ROOT教程,看教程ROOT
  13. HBuilderX 下载安装教程
  14. 106-网络安全——第七章计算机病毒和手机病毒
  15. Elasticsearch 集群内应该设置多少个分片(shard)?
  16. 判断是否为回文字符串(Java)
  17. NOIP模拟赛 队爷的 Au Plan
  18. 每个前端都值得拥有自己的组件库,就像每个冬天都拥有春秋裤
  19. 使用命令行进行文件操作
  20. 单片机包络检波c语言,模拟电子系统设计指南:从半导体、分立元件到TI集成电路的分析与实现(基础篇)...

热门文章

  1. 【变量创建】CFPS应用及C刊变量复盘STATA实战2
  2. 青岛海尔2018年实现净利润74.4亿元 净利率约4.06%
  3. python有中文版吗-python有中文版
  4. Python写超级马里奥
  5. 立式大钢琴-Native Instruments The Giant v1.2.0 Kontakt
  6. 手把手教你 使用会声会影把DVD视频如何刻录到光盘
  7. 386款PSP游戏的迅雷高速下载
  8. Ahchlinux记录第2章 桌面环境的配置和常用软件的安装
  9. 《Effective C++》读书笔记(一)
  10. win7 在任务栏左侧加个显示桌面的图标