惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

鱼雨昱

以彼之矛-攻彼之盾:通过伪造运行环境无损解压RedBendEFDPackage固件包 松下 Let's Note RZ5 侧边无线物理开关在Linux下的复活指南 全站升级:正式全面启用 HTTP/3(QUIC)和IPv6 日机拾贝之富士通Felica锁定的强制解除 索尼电脑恢复镜像中MOD文件的处理 新sdat2img Yuu Web Synth Engine Web Office Toolbox(WOT) PyPianoCatSongDataExtractor
基于决策树和线性回归模型以优化深度优先搜索(DFS)性能
2024-07-27 · via 鱼雨昱

近期在实习的时候遇到一个应用和数据库间查询时的性能优化问题,在跟同事讨论解决方案时,最终选定了线性回归模型的办法。但是具体怎么落地的,就碍于NDA不能明说了。

树的构建与数据生成

尝试定义了一个简单的树结构和一个生成比较大的树的方法

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.children = []

def createTreeBesar(depth, breadth):
    def addChildren(node, currentDepth):
        if currentDepth < depth:
            for _ in range(breadth):
                child = TreeNode(random.randint(1, 100))
                node.children.append(child)
                addChildren(child, currentDepth + 1)
    root = TreeNode(random.randint(1, 100))
    addChildren(root, 1)
    return root

产生样本数据

def generateSampleData():
    data = []
    for _ in range(10000):
        value = random.randint(1, 1000)
        priority = random.random()
        data.append([value, priority])
    data = np.array(data)
    X = data[:, :-1]
    y = data[:, -1]
    return X, y

模型的训练与加载

为避免浪费每次的运行时间和适合性能评估,将保存模型

def trainPriorityModel():
    X, y = generateSampleData()
    model = DecisionTreeRegressor()
    model.fit(X, y)
    joblib.dump(model, 'priorityModel.pkl')
    return model

def trainIndexModel(data):
    values = [node.value for node in data]
    positions = list(range(len(data)))
    model = LinearRegression()
    model.fit(np.array(values).reshape(-1, 1), positions)
    joblib.dump(model, 'indexModel.pkl')
    return model

def loadModel(filePath, trainFunc):
    if os.path.exists(filePath):
        return joblib.load(filePath)
    else:
        return trainFunc()

深度优先搜索和性能评估

def standardDfs(node, visited):
    if node is None or node in visited:
        return
    visited.add(node)
    for child in node.children:
        standardDfs(child, visited)

def indexedDfs(node, visited, indexModel, data):
    if node is None or node in visited:
        return
    visited.add(node)
    for child in node.children:
        locatedNode = locateNode(indexModel, child.value, data)
        indexedDfs(locatedNode, visited, indexModel, data)

def evaluatePerformance(treeRoot, priorityModel, indexModel, data):
    startTime = time.time()
    visitedStandard = set()
    standardDfs(treeRoot, visitedStandard)
    standardTime = time.time() - startTime
    print(f"Standard DFS Run Time: {standardTime:.6f} SEC")

    startTime = time.time()
    visitedIndexed = set()
    indexedDfs(treeRoot, visitedIndexed, indexModel, data)
    indexedTime = time.time() - startTime
    print(f"Indexed DFS Run Time: {indexedTime:.6f} SEC")

结果

结果好像很好的样子?
indexed_dfs_opt