关于「东西方原典考辨:「方以类聚,物以群分」与斯皮尔曼的一般因素」,《稀疏购买记录的偏好补全》给出的定量结论是什么?
结论:第七章考辨《周易·系辞上》「方以类聚,物以群分」与斯皮尔曼 1904 年的一般因素,指出纯分群与混合成员下两种模型各有胜负。
数据口径:书中顾客数、商品数、口味维度与购买概率均为示意参数;全部计算由书中程序生成;本书属于非个人化经营建议,推荐流程只用店铺自有数据,不外传个人数据,不做跨平台追踪。 第七章考辨《周易·系辞上》「方以类聚,物以群分」与斯皮尔曼 1904 年的一般因素,指出纯分群与混合成员下两种模型各有胜负。
对照数据表
下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库
| RMSE on unobserved cells against the noise-free taste | ||
| customers | block means | low rank (ALS, r = 4) |
| pure | 0.0379 | 0.2428 |
| mixed | 0.4092 | 0.2971 |
查看完整验算程序
# Chapter 7. p72: "like gathers with like" as a block model.
# Model: 60 customers x 24 products, 4 customer groups x 4 product
# categories, group-by-category taste table G (Gaussian sd 2, plus 3).
# Pure: each customer belongs to one group. Mixed: each customer
# blends the groups with random weights. Scores carry noise sd 0.2;
# 40 percent observed (illustrative). Block model: mean of observed
# scores in the customer's main group x the product's category.
# Low rank: ALS rank 4 (ridge 0.3, spectral start) on centred scores.
import math
import random
def dot(a, b):
return math.fsum(x * y for x, y in zip(a, b))
def solve(A, b):
# Gaussian elimination with partial pivoting for a small system
n = len(b)
M = [list(A[i]) + [b[i]] for i in range(n)]
for k in range(n):
p = max(range(k, n), key=lambda i: abs(M[i][k]))
M[k], M[p] = M[p], M[k]
for i in range(k + 1, n):
f = M[i][k] / M[k][k]
M[i] = [M[i][j] - f * M[k][j] for j in range(n + 1)]
x = [0.0] * n
for i in range(n - 1, -1, -1):
s = math.fsum(M[i][j] * x[j] for j in range(i + 1, n))
x[i] = (M[i][n] - s) / M[i][i]
return x
def update(trip, F, r, lam, prior=None):
# weighted ridge step for one row: minimise
# sum w (y - f.x)^2 + lam |x - prior|^2 over x
pr = prior if prior is not None else [0.0] * r
A = [[math.fsum(w * F[j][a] * F[j][b] for j, _, w in trip)
+ (lam if a == b else 0.0) for b in range(r)]
for a in range(r)]
y = [math.fsum(w * F[j][a] * v for j, v, w in trip) + lam * pr[a]
for a in range(r)]
return solve(A, y)
def als(nu, ni, obs, r, lam, iters, seed, V=None, trace=None):
# alternating least squares on (u, i, value, weight) tuples
rng = random.Random(seed)
if V is None:
V = [[rng.gauss(0.0, 1.0) for _ in range(r)] for _ in range(ni)]
bu = [[] for _ in range(nu)]
bi = [[] for _ in range(ni)]
for u, i, x, w in obs:
bu[u].append((i, x, w))
bi[i].append((u, x, w))
U = None
for t in range(iters):
U = [update(bu[u], V, r, lam) for u in range(nu)]
V = [update(bi[i], U, r, lam) for i in range(ni)]
if trace is not None:
trace.append((t + 1, U, V))
return U, V
def factors(n, r, rng):
return [[rng.gauss(0.0, 1.0) for _ in range(r)] for _ in range(n)]
def product(A, B):
return [[dot(a, b) for b in B] for a in A]
def mask(nu, ni, p, rng):
return [(u, i) for u in range(nu) for i in range(ni)
if rng.random() < p]
def relerr(M, U, V, cells):
num = math.fsum((dot(U[u], V[i]) - M[u][i]) ** 2 for u, i in cells)
den = math.fsum(M[u][i] ** 2 for u, i in cells)
return math.sqrt(num / den)
def rmse(M, U, V, cells):
num = math.fsum((dot(U[u], V[i]) - M[u][i]) ** 2 for u, i in cells)
return math.sqrt(num / len(cells))
def orthonormal(cols):
# Gram-Schmidt on a list of column vectors
out = []
for c in cols:
v = list(c)
for q in out:
h = dot(q, v)
v = [a - h * b for a, b in zip(v, q)]
nv = math.sqrt(dot(v, v))
out.append([a / nv for a in v])
return out
def spectral(nu, ni, cells, r, rng, iters=30):
# top-r right singular subspace of the rescaled observed matrix:
# Y[u][i] = x / p for observed cells, 0 elsewhere
Y = [[0.0] * ni for _ in range(nu)]
for u, i, x, p in cells:
Y[u][i] = x / p
Yt = [list(c) for c in zip(*Y)]
Q = orthonormal([[rng.gauss(0, 1) for _ in range(ni)]
for _ in range(r)])
for _ in range(iters):
Q = orthonormal([[dot(row, [dot(y, q) for y in Y]) for row in Yt]
for q in Q])
return [[Q[k][i] for k in range(r)] for i in range(ni)]
nu, ni, K = 60, 24, 4
res = []
for kind in ("pure", "mixed"):
rng = random.Random(20261113)
G = [[2 * rng.gauss(0, 1) for _ in range(K)] for _ in range(K)]
ct = [i % K for i in range(ni)]
W = []
for u in range(nu):
if kind == "pure":
W.append([1.0 if g == u % K else 0.0 for g in range(K)])
else:
x = [rng.expovariate(1.0) for _ in range(K)]
W.append([v / math.fsum(x) for v in x])
T = [[3.0 + math.fsum(W[u][g] * G[g][ct[i]] for g in range(K))
for i in range(ni)] for u in range(nu)]
seen = mask(nu, ni, 0.4, rng)
ss = set(seen)
rest = [(u, i) for u in range(nu) for i in range(ni)
if (u, i) not in ss]
y = {c: T[c[0]][c[1]] + rng.gauss(0, 0.2) for c in seen}
main = [max(range(K), key=lambda g: W[u][g]) for u in range(nu)]
blk = {}
for (u, i), v in y.items():
blk.setdefault((main[u], ct[i]), []).append(v)
bm = {key: math.fsum(v) / len(v) for key, v in blk.items()}
e1 = math.sqrt(math.fsum((bm[(main[u], ct[i])] - T[u][i]) ** 2
for u, i in rest) / len(rest))
g0 = math.fsum(y.values()) / len(y)
obs = [(u, i, y[(u, i)] - g0, 1.0) for u, i in seen]
V0 = spectral(nu, ni, [(u, i, x, 0.4) for u, i, x, _ in obs], K, rng)
U, V = als(nu, ni, obs, K, 0.3, 100, 8, V=V0)
e2 = math.sqrt(math.fsum((g0 + dot(U[u], V[i]) - T[u][i]) ** 2
for u, i in rest) / len(rest))
res.append((kind, e1, e2))
print("RMSE on unobserved cells against the noise-free taste")
print(" customers block means low rank (ALS, r = 4)")
for kind, a, b in res:
print(" {:9s} {:.4f} {:.4f}".format(kind, a, b))唯一数据源与引用
范雅福(Fan, Y.). (2026). 《低秩矩阵补全与交替最小二乘、稀疏购买记录推断未表态偏好母典》第 7 章「东西方原典考辨:「方以类聚,物以群分」与斯皮尔曼的一般因素」(版本 1.0.0). 5D Library. DOI 待发布
DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-328 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026328,
author = {Fan, Yafu},
title = {Low-Rank Matrix Completion and Alternating Least Squares: Inferring Unstated Preferences from Sparse Purchase Records},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
note = {DOI pending}
}