使用Gemini, LangChain, Gradio打造一个书籍推荐系统 (第四部分)

article/2025/8/20 17:31:07

第四部分:为每本书加上情绪标签

import pandas as pd
books = pd.read_csv("books_with_categories.csv")
from transformers import pipeline
classifier = pipeline("text-classification",model="j-hartmann/emotion-english-distilroberta-base",top_k = None,device = 0)
classifier("I love this!")

transformers 是 Hugging Face 提供的自然语言处理工具包。
pipeline 是它的一个便捷工具,可以快速调用预训练的模型进行各种 NLP 任务(如文本分类、生成、翻译等)。

pipeline(“text-classification”, …) 表示创建一个 文本分类任务 的 pipeline。
model=“j-hartmann/emotion-english-distilroberta-base” 指定所使用的预训练模型,这个模型专门用于 情绪分析(识别情绪,如喜悦、悲伤、愤怒等)。
top_k=None 表示返回 所有可能的分类及其概率分数。
device=0 表示使用 GPU 0 加速计算(如果有 GPU)。如果没有 GPU,可以改为 device=-1,表示使用 CPU。

将字符串 “I love this!” 输入到模型中,让模型对这段文本进行情绪分类预测。
返回结果是一个列表,里面包含每个可能的情绪类别及其概率。

Device set to use cuda:0
[[{'label': 'joy', 'score': 0.9771687984466553},{'label': 'surprise', 'score': 0.00852868054062128},{'label': 'neutral', 'score': 0.005764591973274946},{'label': 'anger', 'score': 0.004419785924255848},{'label': 'sadness', 'score': 0.0020923891570419073},{'label': 'disgust', 'score': 0.001611991785466671},{'label': 'fear', 'score': 0.00041385178337804973}]]
books["description"][0]
A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives. John Ames is a preacher, the son of a preacher and the grandson (both maternal and paternal) of preachers. It’s 1956 in Gilead, Iowa, towards the end of the Reverend Ames’s life, and he is absorbed in recording his family’s story, a legacy for the young son he will never see grow up. Haunted by his grandfather’s presence, John tells of the rift between his grandfather and his father: the elder, an angry visionary who fought for the abolitionist cause, and his son, an ardent pacifist. He is troubled, too, by his prodigal namesake, Jack (John Ames) Boughton, his best friend’s lost son who returns to Gilead searching for forgiveness and redemption. Told in John Ames’s joyous, rambling voice that finds beauty, humour and truth in the smallest of life’s details, Gilead is a song of celebration and acceptance of the best and the worst the world has to offer. At its heart is a tale of the sacred bonds between fathers and sons, pitch-perfect in style and story, set to dazzle critics and readers alike.
classifier(books["description"][0])
[[{'label': 'fear', 'score': 0.6548413634300232},{'label': 'neutral', 'score': 0.16985207796096802},{'label': 'sadness', 'score': 0.11640888452529907},{'label': 'surprise', 'score': 0.02070062793791294},{'label': 'disgust', 'score': 0.019100705161690712},{'label': 'joy', 'score': 0.015161297284066677},{'label': 'anger', 'score': 0.003935146611183882}]]
classifier(books["description"][0].split("."))

books[“description”] 表示获取 books 表格中的 description(描述)这一列,返回一个 Series。
[0] 表示取这一列中的第一个元素,也就是第 0 行的 description。

.split(“.”),这是 字符串的 split() 方法,按 句号 . 拆分文本。
它会将描述文本按照句子分开,得到一个句子列表。

调用之前创建的 情绪分类模型。
它会将句子列表中的每个句子作为输入,分别进行情绪分类。
返回的是 每个句子的情绪预测结果列表,每个结果都是一组情绪及其分数。

[[{'label': 'surprise', 'score': 0.7296026945114136},{'label': 'neutral', 'score': 0.1403856873512268},{'label': 'fear', 'score': 0.06816219538450241},{'label': 'joy', 'score': 0.04794241115450859},{'label': 'anger', 'score': 0.009156348183751106},{'label': 'disgust', 'score': 0.00262847519479692},{'label': 'sadness', 'score': 0.0021221605129539967}],[{'label': 'neutral', 'score': 0.44937071204185486},{'label': 'disgust', 'score': 0.2735914885997772},{'label': 'joy', 'score': 0.10908304899930954},{'label': 'sadness', 'score': 0.09362724423408508},{'label': 'anger', 'score': 0.040478333830833435},{'label': 'surprise', 'score': 0.02697017975151539},{'label': 'fear', 'score': 0.006879060063511133}],[{'label': 'neutral', 'score': 0.6462162137031555},{'label': 'sadness', 'score': 0.2427332103252411},{'label': 'disgust', 'score': 0.04342261329293251},{'label': 'surprise', 'score': 0.028300540521740913},{'label': 'joy', 'score': 0.014211442321538925},{'label': 'fear', 'score': 0.014084079302847385},{'label': 'anger', 'score': 0.01103188470005989}],[{'label': 'fear', 'score': 0.928167998790741},{'label': 'anger', 'score': 0.03219102695584297},{'label': 'neutral', 'score': 0.012808729894459248},{'label': 'sadness', 'score': 0.008756889030337334},{'label': 'surprise', 'score': 0.008597911335527897},{'label': 'disgust', 'score': 0.008431846275925636},{'label': 'joy', 'score': 0.001045582932420075}],[{'label': 'sadness', 'score': 0.9671575427055359},{'label': 'neutral', 'score': 0.015104170888662338},{'label': 'disgust', 'score': 0.006480592768639326},{'label': 'fear', 'score': 0.005393994972109795},{'label': 'surprise', 'score': 0.0022869433742016554},{'label': 'anger', 'score': 0.0018428893527016044},{'label': 'joy', 'score': 0.0017338789766654372}],[{'label': 'joy', 'score': 0.9327971935272217},{'label': 'disgust', 'score': 0.03771771863102913},{'label': 'neutral', 'score': 0.01589190773665905},{'label': 'sadness', 'score': 0.006444551516324282},{'label': 'anger', 'score': 0.005025018472224474},{'label': 'surprise', 'score': 0.0015812073834240437},{'label': 'fear', 'score': 0.0005423100665211678}],[{'label': 'joy', 'score': 0.6528703570365906},{'label': 'neutral', 'score': 0.25427502393722534},{'label': 'surprise', 'score': 0.0680830255150795},{'label': 'sadness', 'score': 0.009908979758620262},{'label': 'disgust', 'score': 0.006512209307402372},{'label': 'anger', 'score': 0.00482131028547883},{'label': 'fear', 'score': 0.0035290152300149202}],[{'label': 'neutral', 'score': 0.5494765639305115},{'label': 'sadness', 'score': 0.1116902083158493},{'label': 'disgust', 'score': 0.10400670021772385},{'label': 'surprise', 'score': 0.07876556366682053},{'label': 'anger', 'score': 0.0641336739063263},{'label': 'fear', 'score': 0.05136282742023468},{'label': 'joy', 'score': 0.040564440190792084}]]
sentences = books["description"][0].split(".")
predictions = classifier(sentences)
sentences[0]
A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives
predictions[0]
[{'label': 'surprise', 'score': 0.7296026945114136},{'label': 'neutral', 'score': 0.1403856873512268},{'label': 'fear', 'score': 0.06816219538450241},{'label': 'joy', 'score': 0.04794241115450859},{'label': 'anger', 'score': 0.009156348183751106},{'label': 'disgust', 'score': 0.00262847519479692},{'label': 'sadness', 'score': 0.0021221605129539967}]
sentences[3]
 Haunted by his grandfather’s presence, John tells of the rift between his grandfather and his father: the elder, an angry visionary who fought for the abolitionist cause, and his son, an ardent pacifist
predictions[3]
[{'label': 'fear', 'score': 0.928167998790741},{'label': 'anger', 'score': 0.03219102695584297},{'label': 'neutral', 'score': 0.012808729894459248},{'label': 'sadness', 'score': 0.008756889030337334},{'label': 'surprise', 'score': 0.008597911335527897},{'label': 'disgust', 'score': 0.008431846275925636},{'label': 'joy', 'score': 0.001045582932420075}]
predictions
[[{'label': 'surprise', 'score': 0.7296026945114136},{'label': 'neutral', 'score': 0.1403856873512268},{'label': 'fear', 'score': 0.06816219538450241},{'label': 'joy', 'score': 0.04794241115450859},{'label': 'anger', 'score': 0.009156348183751106},{'label': 'disgust', 'score': 0.00262847519479692},{'label': 'sadness', 'score': 0.0021221605129539967}],[{'label': 'neutral', 'score': 0.44937071204185486},{'label': 'disgust', 'score': 0.2735914885997772},{'label': 'joy', 'score': 0.10908304899930954},{'label': 'sadness', 'score': 0.09362724423408508},{'label': 'anger', 'score': 0.040478333830833435},{'label': 'surprise', 'score': 0.02697017975151539},{'label': 'fear', 'score': 0.006879060063511133}],[{'label': 'neutral', 'score': 0.6462162137031555},{'label': 'sadness', 'score': 0.2427332103252411},{'label': 'disgust', 'score': 0.04342261329293251},{'label': 'surprise', 'score': 0.028300540521740913},{'label': 'joy', 'score': 0.014211442321538925},{'label': 'fear', 'score': 0.014084079302847385},{'label': 'anger', 'score': 0.01103188470005989}],[{'label': 'fear', 'score': 0.928167998790741},{'label': 'anger', 'score': 0.03219102695584297},{'label': 'neutral', 'score': 0.012808729894459248},{'label': 'sadness', 'score': 0.008756889030337334},{'label': 'surprise', 'score': 0.008597911335527897},{'label': 'disgust', 'score': 0.008431846275925636},{'label': 'joy', 'score': 0.001045582932420075}],[{'label': 'sadness', 'score': 0.9671575427055359},{'label': 'neutral', 'score': 0.015104170888662338},{'label': 'disgust', 'score': 0.006480592768639326},{'label': 'fear', 'score': 0.005393994972109795},{'label': 'surprise', 'score': 0.0022869433742016554},{'label': 'anger', 'score': 0.0018428893527016044},{'label': 'joy', 'score': 0.0017338789766654372}],[{'label': 'joy', 'score': 0.9327971935272217},{'label': 'disgust', 'score': 0.03771771863102913},{'label': 'neutral', 'score': 0.01589190773665905},{'label': 'sadness', 'score': 0.006444551516324282},{'label': 'anger', 'score': 0.005025018472224474},{'label': 'surprise', 'score': 0.0015812073834240437},{'label': 'fear', 'score': 0.0005423100665211678}],[{'label': 'joy', 'score': 0.6528703570365906},{'label': 'neutral', 'score': 0.25427502393722534},{'label': 'surprise', 'score': 0.0680830255150795},{'label': 'sadness', 'score': 0.009908979758620262},{'label': 'disgust', 'score': 0.006512209307402372},{'label': 'anger', 'score': 0.00482131028547883},{'label': 'fear', 'score': 0.0035290152300149202}],[{'label': 'neutral', 'score': 0.5494765639305115},{'label': 'sadness', 'score': 0.1116902083158493},{'label': 'disgust', 'score': 0.10400670021772385},{'label': 'surprise', 'score': 0.07876556366682053},{'label': 'anger', 'score': 0.0641336739063263},{'label': 'fear', 'score': 0.05136282742023468},{'label': 'joy', 'score': 0.040564440190792084}]]
sorted(predictions[0], key=lambda x: x["label"])

predictions[0] 是第一个预测结果,形式是一个 字典列表(list of dictionaries),每个字典表示一个标签(情绪)及其得分

sorted() 是 Python 内置的 排序函数,用于对列表中的元素排序,返回一个新的排序后的列表。
排序需要一个 排序依据(key),所以这里用了 key=lambda x: x[“label”]。

key 参数用于告诉 sorted() 按什么排序。
lambda x: x[“label”] 是一个 匿名函数(lambda表达式),表示“取 x(每个字典)的 ‘label’ 值”。
也就是,排序会按照每个标签的 字母顺序 进行。

[{'label': 'anger', 'score': 0.009156348183751106},{'label': 'disgust', 'score': 0.00262847519479692},{'label': 'fear', 'score': 0.06816219538450241},{'label': 'joy', 'score': 0.04794241115450859},{'label': 'neutral', 'score': 0.1403856873512268},{'label': 'sadness', 'score': 0.0021221605129539967},{'label': 'surprise', 'score': 0.7296026945114136}]
import numpy as npemotion_labels = ["anger", "disgust", "fear", "joy", "sadness", "surprise", "neutral"]
isbn = []
emotion_scores = {label: [] for label in emotion_labels}def calculate_max_emotion_scores(predictions):per_emotion_scores = {label: [] for label in emotion_labels}for prediction in predictions:sorted_predictions = sorted(prediction, key=lambda x: x["label"])for index, label in enumerate(emotion_labels):per_emotion_scores[label].append(sorted_predictions[index]["score"])return {label: np.max(scores) for label, scores in per_emotion_scores.items()}

定义了我们关注的情绪类别。
isbn 用于存放书籍编号
emotion_scores 是一个字典,用于存放每个情绪类别的分数列表。初始化为:
{‘anger’: [], ‘disgust’: [], ‘fear’: [], ‘joy’: [], ‘sadness’: [], ‘surprise’: [], ‘neutral’: []}

定义了一个函数,输入参数是 predictions(通常是一个列表,包含多段文本的情绪预测结果,每段是多个情绪及其分数的列表)。
同样初始化一个字典,用于存储 当前这组预测 中,每种情绪对应的分数。

每个 prediction 是一个情绪字典列表。
将每个 prediction 按 label 字母顺序排序,以便后续按照 emotion_labels 顺序索引。

通过 index 索引到排序后的 sorted_predictions,取出 score。
将该分数加到 per_emotion_scores[label] 对应的列表中。

对每个情绪的分数列表,取最大值 np.max(scores)。

for i in range(10):isbn.append(books["isbn13"][i])sentences = books["description"][i].split(".")predictions = classifier(sentences)max_scores = calculate_max_emotion_scores(predictions)for label in emotion_labels:emotion_scores[label].append(max_scores[label])

把第 i 本书的 ISBN 号码(isbn13 列中的值)添加到 isbn 列表中,方便后续关联。
将第 i 本书的 description(简介文本)按句号 . 分割成多个句子列表。
把所有句子 sentences 送入 classifier(模型),进行情绪分类。
结果 predictions 是一个列表,每个元素是一个句子的预测结果,通常像这样:
[
[{‘label’: ‘joy’, ‘score’: 0.85}, {‘label’: ‘sadness’, ‘score’: 0.10}, {‘label’: ‘anger’, ‘score’: 0.05}],
[{‘label’: ‘joy’, ‘score’: 0.65}, {‘label’: ‘sadness’, ‘score’: 0.30}, {‘label’: ‘anger’, ‘score’: 0.05}],

]
调用我们之前写的函数 calculate_max_emotion_scores(),统计每种情绪的最大分数。
遍历我们定义好的情绪标签(emotion_labels),把每种情绪的最大分数添加到 emotion_scores 字典的相应列表中。

emotion_scores
{'anger': [np.float64(0.0641336739063263),np.float64(0.6126185059547424),np.float64(0.0641336739063263),np.float64(0.35148391127586365),np.float64(0.08141247183084488),np.float64(0.2322249710559845),np.float64(0.5381842255592346),np.float64(0.0641336739063263),np.float64(0.30067017674446106),np.float64(0.0641336739063263)],'disgust': [np.float64(0.2735914885997772),np.float64(0.3482844829559326),np.float64(0.10400670021772385),np.float64(0.15072263777256012),np.float64(0.1844954937696457),np.float64(0.727174699306488),np.float64(0.15585491061210632),np.float64(0.10400670021772385),np.float64(0.2794813811779022),np.float64(0.1779276728630066)],'fear': [np.float64(0.928167998790741),np.float64(0.9425278306007385),np.float64(0.9723208546638489),np.float64(0.36070623993873596),np.float64(0.09504325687885284),np.float64(0.05136282742023468),np.float64(0.7474286556243896),np.float64(0.4044959247112274),np.float64(0.9155241250991821),np.float64(0.05136282742023468)],'joy': [np.float64(0.9327971935272217),np.float64(0.7044215202331543),np.float64(0.7672368884086609),np.float64(0.25188079476356506),np.float64(0.040564440190792084),np.float64(0.04337584972381592),np.float64(0.8725654482841492),np.float64(0.040564440190792084),np.float64(0.040564440190792084),np.float64(0.040564440190792084)],'sadness': [np.float64(0.6462162137031555),np.float64(0.887939453125),np.float64(0.5494765639305115),np.float64(0.732685387134552),np.float64(0.8843895196914673),np.float64(0.6213927268981934),np.float64(0.7121942639350891),np.float64(0.5494765639305115),np.float64(0.8402896523475647),np.float64(0.8603722453117371)],'surprise': [np.float64(0.9671575427055359),np.float64(0.1116902083158493),np.float64(0.1116902083158493),np.float64(0.1116902083158493),np.float64(0.4758807122707367),np.float64(0.1116902083158493),np.float64(0.40800026059150696),np.float64(0.820281982421875),np.float64(0.35446029901504517),np.float64(0.1116902083158493)],'neutral': [np.float64(0.7296026945114136),np.float64(0.2525450885295868),np.float64(0.07876556366682053),np.float64(0.07876556366682053),np.float64(0.07876556366682053),np.float64(0.27190276980400085),np.float64(0.07876556366682053),np.float64(0.23448744416236877),np.float64(0.13561409711837769),np.float64(0.07876556366682053)]}
from tqdm import tqdmemotion_labels = ["anger", "disgust", "fear", "joy", "sadness", "surprise", "neutral"]
isbn = []
emotion_scores = {label: [] for label in emotion_labels}for i in tqdm(range(len(books))):isbn.append(books["isbn13"][i])sentences = books["description"][i].split(".")predictions = classifier(sentences)max_scores = calculate_max_emotion_scores(predictions)for label in emotion_labels:emotion_scores[label].append(max_scores[label])

tqdm 是一个非常流行的进度条库,用来美化循环进度显示。这样可以直观看到代码运行到哪了,特别是处理大量数据时非常有用。
列出了情绪分类器支持的情绪类别,这些标签对应模型输出结果中的 label 字段。

isbn:用于存储每本书的 ISBN 编号。
emotion_scores:用于存储每个情绪标签的最高分数,每个标签对应一个列表。
初始化后的样子:
{
‘anger’: [],
‘disgust’: [],
‘fear’: [],
‘joy’: [],
‘sadness’: [],
‘surprise’: [],
‘neutral’: []
}

用 tqdm 包裹的 for 循环会显示漂亮的进度条。
range(len(books)) 意味着从第一本书到最后一本书,逐行遍历整个 books DataFrame。

把当前书的 ISBN 编号添加到 isbn 列表中。
把当前书的 description 按句号 . 切分为一个句子列表。

把句子列表 sentences 输入到 classifier(情绪分类模型)。
模型返回每个句子的情绪预测结果(每个句子会有多个情绪分数)。

调用之前定义的 calculate_max_emotion_scores 函数,统计当前书中每个情绪类别的 最高分数。

遍历每个情绪标签。
把当前书的每个情绪分数保存到 emotion_scores 字典对应的列表中。
这样,循环跑完后:
isbn 列表里会有所有书的 ISBN。
emotion_scores 字典会有所有书的情绪分数(每种情绪对应一个分数列表)。

emotions_df = pd.DataFrame(emotion_scores)
emotions_df["isbn13"] = isbn
emotions_df

在这里插入图片描述

books = pd.merge(books, emotions_df, on = "isbn13")
books

pd.merge() 是 Pandas 的一个函数,用来 合并(Join)两个 DataFrame,类似 SQL 中的 JOIN 操作。

合并的两个表:
左表(books):包含了书籍的基本信息,比如 isbn13、title、author、description、simple_categories 等。
右表(emotions_df):包含了情绪分析后的结果,比如每本书的 anger、joy、sadness、fear 等分数。

on = “isbn13”:
告诉 Pandas 用哪一列作为合并的依据。
因为每本书都有唯一的 ISBN 编号,所以选择 isbn13 作为合并键。
在这里插入图片描述

books.to_csv("books_with_emotions.csv", index = False)

http://www.hkcw.cn/article/LYdcPEIWkc.shtml

相关文章

JS逆向案例—喜马拉雅xm-sign详情页爬取

JS逆向案例——喜马拉雅xm-sign详情页爬取 声明网站流程分析总结 声明 本文章中所有内容仅供学习交流,抓包内容、敏感网址、数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权&am…

Java日志体系

前言:🐭🐭已经两年没更新了,主要原因是因为🐭🐭考研去了,前段时间读研和工作压力都比较大所以没时间更新,今后🐭🐭会慢慢恢复更新 1 流程和原理梳理 日志体…

【HW系列】—Windows日志与Linux日志分析

文章目录 一、Windows日志1. Windows事件日志2. 核心日志类型3. 事件日志分析实战详细分析步骤 二、Linux日志1. 常见日志文件2. 关键日志解析3. 登录爆破检测方法日志分析核心要点 一、Windows日志 1. Windows事件日志 介绍:记录系统、应用程序及安全事件&#x…

使用交叉编译工具提示stubs-32.h:7:11: fatal error: gnu/stubs-soft.h: 没有那个文件或目录的解决办法

0 前言 使用ST官方SDK提供的交叉编译工具、cmake生成Makefile,使用make命令生成可执行文件提示fatal error: gnu/stubs-soft.h: 没有那个文件或目录的解决办法,如下所示: 根据这一错误提示,按照网上的解决方案逐一尝试均以失败告…

苏超第三轮徐州2-1战胜连云港 端午假期迎首胜

北京时间5月31日,2025年江苏省城市足球联赛第3轮,徐州队主场以2-1战胜连云港队,迎来首胜。这场比赛正值端午假期,吸引了22198位球迷涌入徐州奥体中心观赛,上座人数甚至超过了部分中超比赛。目前,徐州队在先赛一场的情况下取得1胜2平积5分的成绩,暂时排名积分榜第三。而连…

富翁错失NASA局长提名 白宫:必须完全认同特朗普

亿万富翁错失NASA局长提名 白宫:必须完全认同特朗普当地时间5月31日,白宫表示,特朗普将很快宣布新的NASA局长提名人选。△贾里德艾萨克曼(资料图)白宫尚未解释原提名人贾里德艾萨克曼(Jared Isaacman)为何退出。据知情人士称,白宫已决定撤回艾萨克曼的提名。白宫发言人…

[USACO1.5] 八皇后 Checker Challenge Java

import java.util.*;public class Main {// 标记 对角线1,对角线2,所在x轴 是否存在棋子static boolean[] d1 new boolean[100], d2 new boolean[100], d new boolean[100]; static int n, ans 0;static int[] arr new int[14]; // 记录一轮棋子位置…

数据库核心技术深度剖析:事务、索引、锁与SQL优化实战指南(第四节)----从行级锁到死锁处理的系统梳理

Introduction:收纳技术相关的数据库知识 事务、索引、锁、SQL优化 等总结! 文章目录 数据库锁行级锁(Row-Level)属性锁共享锁(Shared Locks)排它锁(Exclusive Locks) 锁实现方式Record Lock(记录锁)Gap Lock(间隙锁)Next-Key Lock(临键锁) 加锁机制乐观锁…

79. 单词搜索-极致优化,可行性剪枝和顺序剪枝

给你一个目标字符串,和一个二维字符数组,判断在数组中是否能找到目标字符串。 例如,board [["A","B","C","E"],["S","F","C","S"],["A","…

VLAN的作用和原理

1. 为什么要有vlan? 分割广播域,避免广播风暴,造成网络资源的浪费 可以灵活的组网,便于管理,同时还有安全加固的功能 2. vlan是怎么实现的?端口的原理? 设置VLAN后,流量之间的转…

使用MCP和Ollama本地创建AI代理:实操教程

如果你在过去几个月没有与世隔绝的话,那么你很可能看到过多篇提到新的模型上下文协议(MCP)的文章。 MCP是Anthropic发布的一个新标准,旨在弥合大型语言模型(LLMs)与外部世界之间的差距。MCP提供了一种标准化的方式,让模型能够访问资源——比如数据和工具——来帮助它们…

美防长被中方代表质问后答非所问 回避东盟立场问题

在第22届香格里拉对话会上,国防大学代表团成员张弛向美国防长赫格塞思提问:“你提到盟友和伙伴很重要。但是,美国近年在本地区建立的多边联盟和框架,例如美日澳印四边机制和美英澳三边安全伙伴关系,都没有包括东盟国家。所以,如果美国的联盟和东盟之间产生分歧或争端,你…

吴恩达MCP课程(3):mcp_chatbot

原课程代码是用Anthropic写的,下面代码是用OpenAI改写的,模型则用阿里巴巴的模型做测试 .env 文件为: OPENAI_API_KEYsk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx OPENAI_API_BASEhttps://dashscope.aliyuncs.com/compatible-mode…

新视角!经济学顶刊QJE用文本分析探究新技术扩散

美国圣路易斯联邦储备银行Aakash Kalyani、美国斯坦福大学与国家经济研究局Nicholas Bloom、英国伦敦商学院Marcela Carvalho和其合作者们共同研究的“The Diffusion of New Technologies(新技术的扩散)”在顶刊The Quarterly Journal of Economics中发表…

动态规划-376.摆动序列-力扣(LeetCode)

一、题目解析 看着题目上的解释或许有点难以理解,这里一图流 只要形似上图的都可以是摆动序列,如左图,且仅含一个元素和两个元素的也算摆动序列,如右图 二、算法原理 1、状态表示 根据经验我们都是以i位置为结尾时&#xff0c…

【机器学习基础】机器学习入门核心算法:XGBoost 和 LightGBM

机器学习入门核心算法:XGBoost 和 LightGBM 一、算法逻辑XGBoost (eXtreme Gradient Boosting)LightGBM (Light Gradient Boosting Machine) 二、算法原理与数学推导目标函数(二者通用)二阶泰勒展开:XGBoost 分裂点增益计算&#…

《STL--stack 和 queue 的使用及其底层实现》

引言: 上次我们学习了容器list的使用及其底层实现,相对来说是比较复杂的,今天我们要学习的适配器stack和queue与list相比就简单很多了,下面我们就开始今天的学习: 一:stack(后进先出&#xff…

Redis缓存问题重点详解

前言:本节包含常见redis缓存问题,包含缓存一致性问题,缓存雪崩,缓存穿透,缓存击穿问题及其解决方案 1. 缓存一致性 我们先看下目前企业用的最多的缓存模型。缓存的通用模型有三种: 缓存模型解释Cache Asi…

Redis最佳实践——安全与稳定性保障之访问控制详解

Redis 在电商应用的安全与稳定性保障之访问控制全面详解 一、安全访问控制体系架构 1. 多层级防护体系 #mermaid-svg-jpkDj2nKxCq9AXIW {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-jpkDj2nKxCq9AXIW .error-ico…

矩阵快速幂算法快速上手

矩阵快速幂算法快速上手 一、基础知识回顾1.1 矩阵乘法1.2 单位矩阵 二、快速幂算法思想2.1 整数快速幂2.2 矩阵快速幂 三、矩阵快速幂的代码实现3.1 Python实现3.2 C实现3.3 Java实现 四、矩阵快速幂的应用场景4.1 斐波那契数列4.2 线性递推数列4.3 图论中的路径计数4.4 动态规…