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

本文涉及的产品
NLP自然语言处理_基础版,每接口每天50万次
NLP 自学习平台,3个模型定制额度 1个月
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
}

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

相关文章
|
6小时前
|
算法 搜索推荐 开发者
别再让复杂度拖你后腿!Python 算法设计与分析实战,教你如何精准评估与优化!
【7月更文挑战第23天】在Python编程中,掌握算法复杂度—时间与空间消耗,是提升程序效能的关键。算法如冒泡排序($O(n^2)$时间/$O(1)$空间),或使用Python内置函数找最大值($O(n)$时间),需精确诊断与优化。数据结构如哈希表可将查找从$O(n)$降至$O(1)$。运用`timeit`模块评估性能,深入理解数据结构和算法,使Python代码更高效。持续实践与学习,精通复杂度管理。
15 9
|
1天前
|
数据可视化 数据挖掘 Linux
震撼发布!Python数据分析师必学,Matplotlib与Seaborn数据可视化实战全攻略!
【7月更文挑战第22天】数据科学中,Matplotlib和Seaborn是Python的可视化主力。Matplotlib用于基础图表,如示例中的折线图;Seaborn则强化统计图形,如分布图。两者结合能创建复杂的可视化,如显示趋势与分布的同一图表。通过学习和运用这些工具,数据分析师能提升效率,更好地讲述数据故事。
11 2
|
1天前
|
存储 JSON API
实战派教程!Python Web开发中RESTful API的设计哲学与实现技巧,一网打尽!
【7月更文挑战第22天】构建RESTful API实战:**使用Python Flask设计图书管理API,遵循REST原则,通过GET/POST/PUT/DELETE操作处理/books及/books/&lt;id&gt;。示例代码展示资源定义、请求响应交互。关键点包括HTTP状态码的使用、版本控制、错误处理和文档化。本文深入探讨设计哲学与实现技巧,助力理解RESTful API开发。
10 0
|
2天前
|
机器学习/深度学习 自然语言处理 机器人
使用Python实现深度学习模型:自然语言理解与问答系统
【7月更文挑战第20天】 使用Python实现深度学习模型:自然语言理解与问答系统
10 0
使用Python实现深度学习模型:自然语言理解与问答系统
|
2天前
|
算法 数据处理 索引
告别低效搜索!Python中Trie树与Suffix Tree的实战应用秘籍!
【7月更文挑战第21天】探索Python中的字符串搜索效率提升:使用Trie树与Suffix Tree。Trie树优化单词查询,插入和删除,示例展示其插入与搜索功能。Suffix Tree,复杂但强大,适用于快速查找、LCP查询。安装[pysuffixtree](https://pypi.org/project/pysuffixtree/)库后,演示查找子串及最长公共后缀。两者在字符串处理中发挥关键作用,提升数据处理效率。**
|
3天前
|
缓存 中间件 网络架构
Python Web开发实战:高效利用路由与中间件提升应用性能
【7月更文挑战第20天】在Python Web开发中,路由与中间件是构建高效应用的核心。路由通过装饰器如`@app.route()`在Flask中映射请求至处理函数;中间件(如`@app.before_request`, `@app.after_request`)则在请求流程中插入自定义逻辑。优化路由包括减少冲突、利用动态参数及蓝图;中间件可用于缓存响应、请求验证和异常处理,显著提升性能和可维护性。良好设计是关键,示例代码展示了如何在Flask中实现这些策略。
13 0
|
4天前
|
大数据 UED 开发者
实战演练:利用Python的Trie树优化搜索算法,性能飙升不是梦!
【7月更文挑战第19天】Trie树,又称前缀树,是优化字符串搜索的高效数据结构。通过利用公共前缀,Trie树能快速插入、删除和查找字符串。
20 2
|
5天前
|
监控 前端开发 API
实战指南:使用Python Flask与WebSocket实现高效的前后端分离实时系统
【7月更文挑战第18天】构建实时Web应用,如聊天室,可借助Python的Flask和WebSocket。安装Flask及Flask-SocketIO库,创建Flask应用,处理WebSocket事件。前端模板通过Socket.IO库连接服务器,发送和接收消息。运行应用,实现实时通信。此示例展现了Flask结合WebSocket实现前后端实时交互的能力。
|
6天前
|
算法 数据挖掘 计算机视觉
Python并查集实战宝典:从入门到精通,让你的数据结构技能无懈可击!
【7月更文挑战第17天】并查集,如同瑞士军刀,是解决元素分组问题的利器,应用于好友关系、像素聚类、碰撞检测和连通性分析等场景。本文从基础到实战,介绍并查集的初始化、查找与路径压缩、按秩合并,以及在Kruskal算法中的应用。通过并查集,实现高效动态集合操作,对比哈希表和平衡树,其在合并与查找上的性能尤为突出。学习并查集,提升算法解决复杂问题的能力。