返回介绍

Problem set for optimization

发布于 2025-02-25 23:44:00 字数 1505 浏览 0 评论 0 收藏 0

We will use a few standard examples throughout to illustrate differnt optimization techniques.

Matrix Multiplication

def mult(u, v):
    m, n = u.shape
    n, p = v.shape
    w = np.zeros((m, p))
    for i in range(m):
        for j in range(p):
            for k in range(n):
                w[i, j] += u[i, k] * v[k, j]
    return w
u = np.reshape(np.arange(6), (2,3))
v = np.reshape(np.arange(9), (3,3))

np.testing.assert_array_almost_equal(mult(u, v), u.dot(v))

Pairwise distance matrix

def dist(u, v):
    n = len(u)
    s = 0
    for i in range(n):
        s += (u[i] - v[i])**2
    return np.sqrt(s)
u = np.array([4,5])
v = np.array([1,1])

np.testing.assert_almost_equal(dist(u, v), np.linalg.norm(u-v))
def pdist(vs, dist=dist):
    n = len(vs)
    m = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            m[i, j] = dist(vs[i], vs[j])
    return m
from scipy.spatial.distance import squareform, pdist as sp_pdist

vs = np.array([[0,0], [1,2], [2,3], [3,4]])

np.testing.assert_array_almost_equal(pdist(vs), squareform(sp_pdist(vs)))

Word count

import string

def word_count(docs):
    wc = {}
    for doc in docs:
        words = doc.translate(None, string.punctuation).split()
        for word in words:
            wc[word] = wc.get(word, 0) + 1
    return wc
docs = ['hello, there handsome!', 'hi, there, beautiful']

word_count(docs)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文