【Python自然语言处理】使用逻辑回归(logistic)对电影评论情感分析实战(超详细 附源码)

本文涉及的产品
NLP 自学习平台,3个模型定制额度 1个月
NLP自然语言处理_高级版,每接口累计50万次
NLP自然语言处理_基础版,每接口每天50万次
简介: 【Python自然语言处理】使用逻辑回归(logistic)对电影评论情感分析实战(超详细 附源码)

需要源码和数据集请点赞关注收藏后评论区留言私信~~~

一、舆情分析

舆情分析很多情况下涉及到用户的情感分析,或者亦称为观点挖掘,是指用自然语言处理技术、文本挖掘以及计算机语言学等方法来正确识别和提取文本素材中的主观信息,通过对带有情感因素主观性文本进行分析,以确定该文本的情感倾向。

文本情感分析的途径: 关键词识别 词汇关联 统计方法 概念级技术

目前主流的情感分析方法主要有两种:基于情感词典的分析法和基于机器学习的分析法

1、 基于情感词典的情感分析

是指根据已构建的专家情感词典,针对对象分析文本进行文本处理抽取关键情感词,计算对象文本的情感倾向。最终分类质量很大程度上取决于专家情感词典的完善度和准确度。目前比较具有代表性的中文情感词典,包括知网情感分析用词语集,台湾大学情感词典,清华大学褒贬词词典等。

2、基于机器学习的情感分析

情感分析本质上也是个二分类的问题,可以采用机器学习的方法识别,选取文本中的情感词作为特征词,将文本矩阵化,利用逻辑回归( logistic Regression )、 朴素贝叶斯(Naive Bayes)、支持向量机(SVM)、K-means以及K-means++等方法进行分类。最终分类效果取决于训练文本的选择以及正确的情感标注。

K-means算法的基本步骤为:

(1)指定聚类数量K(可以通过最优算法获得)。

(2)从数据集中任意随机选取k个对象作为初始聚类中心点或者平均值。

(3)将每个数据点指派给距离其最近的中心点,计算欧几里得距离。

(4)基于k种聚类,计算聚类数据点的新平均值,更新其聚类中心点, 第K个聚类的中心点是一个矢量,该矢量包含此聚类种所有观察点变量的平均值的长度。

(5)迭代计算并最小化总的聚类平方和。重复执行步骤3和步骤4,直到聚类分类结果不再变化或者达到最大迭代次数。

对于K-Means算法可以参见我以下这两篇博客

k-means银行客户画像分组

k-means物流分配实战

二、电影评论情感分析实战

下面我们以基于用户的电影评论为基础,使用IMDB电影评论数据,进行主观情感分析,原始数据总共包括正面和负面评价各25000条。

先导入库文件 主要包括Sklearn里面的一些模块 代码如下

#导入库
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelBinarizer
import numpy as np
import pandas as pds
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from nltk.tokenize.toktok import ToktokTokenizer
from nltk.stem import LancasterStemmer,WordNetLemmatizer
from sklearn.linear_model import LogisticRegression,SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from textblob import TextBlob
from textblob import Word
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize,sent_tokenize
from bs4 import BeautifulSoup
import spacy
import re,string,unicodedata
import seaborn as sns

读入数据 打印正面、负面情感的数值统计结果 各自包含25000条记录

划分训练集和测试机数据,关注评论详细信息以及正面评价或者负面评价的标识信息,分别选定数据的最初45000条记录作为训练集,而剩余的5000条作为测试集,因此测试数据的比例为百分之十,此处可以适当调整,评估的结果也会发生相应变化

接下来用词袋模型和词频-逆文档模型将文本向量化,然后使用逻辑回归模型执行回归处理此处不再赘述 下面直接展示结果

评论的统计结果如下 可以大部分评论在100词左右 也符合实际情况

接下来看看两种模型的准确性评估

可见相差不大 两种模型的准确性评估结果为0.75-0.77  维持在大致相当的水平,用户的正面评价和负面评价的指标分析结果没有发生很大差异

三、代码

代码如下 数据集请点赞关注收藏后评论区留言私信~~~

代码主要是jupyter notebook格式 需要python文件格式点赞关注收藏后评论区留言私信即可~

classification_report,confusion_matrix,accuracy_score\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.preprocessing import LabelBinarizer\n",
    "import numpy as np\n",
    "import pandas as pds\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import nltk\n",
    "from nltk.tokenize.toktok import ToktokTokenizer\n",
    "from nltk.stem import LancasterStemmer,WordNetLemmatizer\n",
    "from sklearn.linear_model import LogisticRegression,SGDClassifier\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.svm import SVC\n",
    "from textblob import TextBlob\n",
    "from textblob import Word\n",
    "from nltk.corpus import stopwords\n",
    "from nltk.stem.porter import PorterStemmer\n",
    "from nltk.stem import WordNetLemmatizer\n",
    "from nltk.tokenize import word_tokenize,sent_tokenize\n",
    "from bs4 import BeautifulSoup\n",
    "import spacy\n",
    "import re,string,unicodedata\n",
    "import seaborn as sns\n",
    "\n",
    "\n",
    "\n"
25000\n",
       "Name: sentiment, dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读入数据\n",
    "data=pds.read_csv('data/IMDB Dataset.csv')\n",
    "data.head(5)\n",
    "#统计情感信息\n",
    "data['sentiment'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "_uuid": "d3aaabff555e07feb11c72cc3a6e457615975ffe"
   },
   "outputs": [],
   "source": [
    "# 划分训练集和测试集数据\n",
    "train_evaluate=data.review[:45000]\n",
    "test_evaluate=data.review[45000:]\n",
    "\n",
    "train_flag=data.sentiment[:45000]\n",
    "test_flag=data.sentiment[45000:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "_uuid": "f000c43d91f68f6668539f089c6a54c5ce3bd819"
   },
   "outputs": [],
   "source": [
    "#文本分词\n",
    "#tokenizer=ToktokTokenizer()\n",
    "#设置停用词\n",
    "#stopword=nltk.corpus.stopwords.words('english')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "_uuid": "219da72b025121fd98081df50ae0fcaace10cc9d"
   },
   "outputs": [],
   "source": [
    "#去除特殊字符\n",
    "def regex_character_remove(info, remove_digits=True):\n",
    "    regx=r'[^A-za-z0-9#@$\\s]'\n",
    "    outcome=re.sub(regx,'',info)\n",
    "    return outcome\n",
    "#针对评论数据删除特殊字符\n",
    "data['review']=data['review'].apply(regex_character_remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "_uuid": "2295f2946e0ab74c220ad538d0e7adc04d23f697"
   },
   "outputs": [],
   "source": [
    "#分词\n",
    "def stem(info):\n",
    "    ps=nltk.porter.PorterStemmer()\n",
    "    outcome= ' '.join([ps.stem(k) for k in info.split()])\n",
    "    return outcome\n",
    "data['review']=data['review'].apply(stem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "_uuid": "5dbff82b4d2d188d8777b273a75d8ac714d38885"
   },
   "outputs": [],
   "source": [
    "#删除停用词\n",
    "stopwords=set(stopwords.words('english'))\n",
    "tokenizer=ToktokTokenizer()\n",
    "\n",
    "def remove_stopwords(info, is_lower_case=False):\n",
    "    tks = tokenizer.tokenize(info)\n",
    "    tks = [tk.strip() for tk in tks]\n",
    "    if is_lower_case:\n",
    "        ftk = [tk for tk in tks if tk not in stopwords]\n",
    "    else:\n",
    "        ftk = [tk for tk in tks if tk.lower() not in stopwords]\n",
    "    filter_info = ' '.join(ftk)    \n",
    "    return filter_info\n",
    "\n",
    "data['review']=data['review'].apply(remove_stopwords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "_kg_hide-output": true,
    "_uuid": "b20c242bd091929ca896ea2c6e936ca00efe6ecf"
   },
   "outputs": [],
   "source": [
    "\n",
    "n_train_reviews=data.review[:45000]\n",
    "n_test_reviews=data.review[45000:]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "1c2a872ffcb6b8076fdbbba641af12081b6022ef"
   },
   "source": [
    "**Bags of words model **\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "_uuid": "35cf9dcefb40b2dc520c5b0d559695324c46cc04"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词袋模型训练数据: (45000, 6813632)\n",
      "词袋模型测试数据: (5000, 6813632)\n"
     ]
    }
   ],
   "source": [
    "#词袋模型向量化\n",
    "cvr=CountVectorizer(min_df=0,max_df=1,binary=False,ngram_range=(1,3))\n",
    "cvr_train_reviews=cvr.fit_transform(n_train_reviews)\n",
    "cvr_test_reviews=cvr.transform(n_test_reviews)\n",
    "\n",
    "print('词袋模型训练数据:',cvr_train_reviews.shape)\n",
    "print('词袋模型测试数据:',cvr_test_reviews.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "52371868f05ff9cf157280c5acf0f5bc71ee176d"
   },
   "source": [
    "**Term Frequency-Inverse Document Frequency model (TFIDF)**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "_uuid": "afe6de957339921e05a6faeaf731f2272fd31946",
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "#TF-IDF向量化\n",
    "td=TfidfVectorizer(min_df=0,max_df=1,use_idf=True,ngram_range=(1,3))\n",
    "td_train_reviews=td.fit_transform(n_train_reviews)\n",
    "td_test_reviews=td.transform(n_test_reviews)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "_uuid": "60f5d496ce4109d1cdbf08f4284d4d26efd93922"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50000, 1)\n"
     ]
    }
   ],
   "source": [
    "#标注情感数据\n",
    "lb=LabelBinarizer()\n",
    "sm_data=lb.fit_transform(data['sentiment'])\n",
    "print(sm_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "_kg_hide-output": true,
    "_uuid": "ca1e4cc917265ac98a72c37cffe57f27e9897408"
   },
   "outputs": [],
   "source": [
    "#分割电影评论数据\n",
    "train_sm=sm_data[:45000]\n",
    "test_sm=sm_data[45000:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "_uuid": "142d007421900550079a12ae8655bcae678ebaad"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\ichiro\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\utils\\validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LogisticRegression(C=1, class_weight=None, dual=False, fit_intercept=True,\n",
      "          intercept_scaling=1, max_iter=500, multi_class='ovr', n_jobs=1,\n",
      "          penalty='l2', random_state=42, solver='liblinear', tol=0.0001,\n",
      "          verbose=0, warm_start=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\ichiro\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\utils\\validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LogisticRegression(C=1, class_weight=None, dual=False, fit_intercept=True,\n",
      "          intercept_scaling=1, max_iter=500, multi_class='ovr', n_jobs=1,\n",
      "          penalty='l2', random_state=42, solver='liblinear', tol=0.0001,\n",
      "          verbose=0, warm_start=False)\n"
     ]
    }
   ],
   "source": [
    "#使用逻辑回归模型\n",
    "lr=LogisticRegression(penalty='l2',max_iter=500,C=1,random_state=42)\n",
    "#基于词袋模型拟合\n",
    "lr_fit=lr.fit(cvr_train_reviews,train_sm)\n",
    "print(lr_fit)\n",
    "#基于TF-IDF模型拟合\n",
    "lr_tfidf=lr.fit(td_train_reviews,train_sm)\n",
    "print(lr_tfidf)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x1fdb01eda20>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAAFgCAYAAACFYaNMAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAczUlEQVR4nO3df5RfdX3n8eebmcyAiZWgMUuBc4ia7S7dsyIbEbW2VleItKfoOUrDcSWwaGqFVotrD+hu7Y/1nNpWKa6CRklFjwXRak0tiiki6qpIUOSnyPhrSQ4kUfyRGZqkmbz3j+/nG74MM5kf+d7vZ74zz8c53zP3fu693+97bmZeufO5935uZCaSpN47onYBkrRYGcCSVIkBLEmVGMCSVIkBLEmVDNYuoAlr167Nz33uc7XLkKS2mKxxQR4B//jHP65dgiRNa0EGsCT1AwNYkioxgCWpEgNYkioxgCWpEgNYkioxgCWpEgNYkioxgCWpEgNYkioxgCWpEgNYkioxgOcoMxkdHcVn6kmaKwN4jsbGxlh3+WcZGxurXYqkPmUAH4aBoaNqlyCpjxnAklRJYwEcEUdGxDci4tsRcXdE/FlpXxURt0TESER8LCKGSvtwmR8py0/seK9LS/t9EXFGUzVLUi81eQS8F3hRZj4TOBlYGxGnAe8ALsvMZwA/BS4o618A/LS0X1bWIyJOAtYBvwqsBa6IiIEG65aknmgsgLNltMwuKa8EXgR8orRfDbysTJ9V5inLXxwRUdqvzcy9mfkDYAQ4tam6JalXGu0DjoiBiLgd2AlsAb4H/Cwz95dVtgHHlenjgAcAyvKfA0/ubJ9km87P2hARWyNi665duxr4biSpuxoN4Mwcz8yTgeNpHbX+hwY/a2NmrsnMNStWrGjqYySpa3pyFURm/gy4CXgucHREDJZFxwPby/R24ASAsvxJwE862yfZRp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byA1h1Z76A1BsbJwLMj4mVl3aW0xo19Jq07qP6MVvD+Gq3xbtv+BDijrPc7jX4nWlAMYC0GtwH/JSJ+CdgLfI1WEL8A+BnwxWwNuNMezevXy3bjtAZMAnhOx3r7gI91vP//BT4UEa+lNeC4NCMGsBa8bI1k9gNaT7P4Kq0j4t8EnkFrvICp7MnM8Rm8/+uA/0lrVK3bIqKJMYC1ABnAWiy+DPwP4Etl+nXAt2gNmvMbEfGUcqLtHODmSba/paz35NJ98cr2goh4embekpl/Qmsw+RMm2V56nMHpV5EWhC/TGp3ra5k5FhF7gC9n5oPlwZA30Rox658z89MTNy7r/Smt7ouf0Rr5q+2vI2J12f5GWs85k6blaGiSVIldEJJUiQEsSZUYwJJUiQEsSZUYwJJUiQEsSZUYwJJUyf8Hl/42sRZtj6IAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "data['words'] = data['review'].apply(lambda y: len(y.split()))\n",
    "sns.displot(data=data, x=\"words\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "_uuid": "52ad86935b76117f97b79e6672a3ba12352b9461"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0 ... 0 1 1]\n",
      "[1 1 0 ... 0 1 1]\n"
     ]
    }
   ],
   "source": [
    "#词袋模型预测\n",
    "bow_pred=lr.predict(cvr_test_reviews)\n",
    "print(bow_pred)\n",
    "##TF-IDF模型预测\n",
    "tfidf_pred=lr.predict(td_test_reviews)\n",
    "print(tfidf_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词袋模型准确性: 0.7612\n",
      "TF-IDF模型准确性: 0.7608\n"
     ]
    }
   ],
   "source": [
    "#模型准确性评估\n",
    "bow_score=accuracy_score(test_sm,bow_pred)\n",
    "tfidf_score=accuracy_score(test_sm,tfidf_pred)\n",
    "print(\"词袋模型准确性:\",bow_score)\n",
    "print(\"TF-IDF模型准确性:\",tfidf_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词袋模型:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "   Positive      0.766     0.761     0.763      2530\n",
      "   Negative      0.757     0.762     0.759      2470\n",
      "\n",
      "avg / total      0.761     0.761     0.761      5000\n",
      "\n",
      "TD-IDF模型:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "   Positive      0.764     0.763     0.764      2530\n",
      "   Negative      0.758     0.758     0.758      2470\n",
      "\n",
      "avg / total      0.761     0.761     0.761      5000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#分类报告 \n",
    "bow_report=classification_report(test_sm,bow_pred,digits=3,  labels=None,sample_weight=None,target_names=['Positive','Negative'])\n",
    "tfidf_report=classification_report(test_sm,tfidf_pred,digits=3, labels=None, sample_weight=None,target_names=['Positive','Negative'])\n",
    "print('词袋模型:\\n',bow_report)\n",
    "print('TD-IDF模型:\\n',tfidf_report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.19.2'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sklearn\n",
    "sklearn.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "ac2ec8353acb5e0f548e1e4a590fbe6f34f4a686"
   },
   "source": [
    "**Print the classification report**"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
14天前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
|
14天前
|
小程序 开发者 Python
探索Python编程:从基础到实战
本文将引导你走进Python编程的世界,从基础语法开始,逐步深入到实战项目。我们将一起探讨如何在编程中发挥创意,解决问题,并分享一些实用的技巧和心得。无论你是编程新手还是有一定经验的开发者,这篇文章都将为你提供有价值的参考。让我们一起开启Python编程的探索之旅吧!
40 10
|
26天前
|
算法 Unix 数据库
Python编程入门:从基础到实战
本篇文章将带你进入Python编程的奇妙世界。我们将从最基础的概念开始,逐步深入,最后通过一个实际的项目案例,让你真正体验到Python编程的乐趣和实用性。无论你是编程新手,还是有一定基础的开发者,这篇文章都将为你提供有价值的信息和知识。让我们一起探索Python的世界吧!
|
28天前
|
数据处理 Python
探索Python中的异步编程:从基础到实战
在Python的世界中,“速度”不仅是赛车手的追求。本文将带你领略Python异步编程的魅力,从原理到实践,我们不单单是看代码,更通过实例感受它的威力。你将学会如何用更少的服务器资源做更多的事,就像是在厨房里同时烹饪多道菜而不让任何一道烧焦。准备好了吗?让我们开始这场技术烹饪之旅。
|
2月前
|
自然语言处理 算法 数据挖掘
探讨如何利用Python中的NLP工具,从被动收集到主动分析文本数据的过程
【10月更文挑战第11天】本文介绍了自然语言处理(NLP)在文本分析中的应用,从被动收集到主动分析的过程。通过Python代码示例,详细展示了文本预处理、特征提取、情感分析和主题建模等关键技术,帮助读者理解如何有效利用NLP工具进行文本数据分析。
55 2
|
机器学习/深度学习 人工智能 自然语言处理
python机器学习入门之自然语言处理(NLP)工具Jieba的使用及解析
python机器学习入门之自然语言处理(NLP)工具Jieba的使用及解析
248 0
python机器学习入门之自然语言处理(NLP)工具Jieba的使用及解析
|
自然语言处理 Python 算法
Python自然语言处理工具小结
作者:伏草惟存 来源:http://www.cnblogs.com/baiboy/p/nltk2.html Python 的几个自然语言处理工具 NLTK:NLTK 在用 Python 处理自然语言的工具中处于领先的地位。
2051 0
|
15天前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
|
2天前
|
Unix Linux 程序员
[oeasy]python053_学编程为什么从hello_world_开始
视频介绍了“Hello World”程序的由来及其在编程中的重要性。从贝尔实验室诞生的Unix系统和C语言说起,讲述了“Hello World”作为经典示例的起源和流传过程。文章还探讨了C语言对其他编程语言的影响,以及它在系统编程中的地位。最后总结了“Hello World”、print、小括号和双引号等编程概念的来源。
97 80