Python - 线性回归 - 图像

3

我正在尝试理解Python中的机器学习。我一直在使用以下示例(http://scikit-learn.org/stable/auto_examples/plot_multioutput_face_completion.html#example-plot-multioutput-face-completion-py)及其代码进行学习。

我想测试/验证关于线性回归内部运作原理的理解。目标是通过查看图片已知的上半部分来预测图片下半部分的缺失。最初有300张64*64的图片(4096像素)。独立变量X是一个300*2048的矩阵(300张图片,2048像素(这些图片的上半部分)。因变量也是一个300*2048的矩阵(图片的下半部分)。系数矩阵似乎是一个2048*2048的矩阵。 我的理解正确吗?

  • 预测单个y像素(例如图片1中最上方的像素)的方法是将图片1上半部分的所有2048个像素乘以回归系数集合,从而估计出下半部分的每个缺失像素,考虑到该特定图像的所有2048个像素?

  • 回归系数是像素相关的(每个y像素具有不同的2048个回归系数集),这些系数是通过在可用的300个图像中寻找相同像素位置上的特定像素位置的OLS拟合来估计的?

我可能被矩阵搞混了 - 如果我错了,请纠正我。非常感谢。W

print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import fetch_olivetti_faces
from sklearn.utils.validation import check_random_state

from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV

# Load the faces datasets
data = fetch_olivetti_faces()
targets = data.target

data = data.images.reshape((len(data.images), -1))
train = data[targets < 30]
test = data[targets >= 30]  # Test on independent people

# Test on a subset of people
n_faces = 5
rng = check_random_state(4)
face_ids = rng.randint(test.shape[0], size=(n_faces, ))
test = test[face_ids, :]

n_pixels = data.shape[1]
X_train = train[:, :np.ceil(0.5 * n_pixels)]  # Upper half of the faces
y_train = train[:, np.floor(0.5 * n_pixels):]  # Lower half of the faces
X_test = test[:, :np.ceil(0.5 * n_pixels)]
y_test = test[:, np.floor(0.5 * n_pixels):]

# Fit estimators
ESTIMATORS = {
    "Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32,
                                       random_state=0),
    "K-nn": KNeighborsRegressor(),
    "Linear regression": LinearRegression(),
    "Ridge": RidgeCV(),
}

y_test_predict = dict()
for name, estimator in ESTIMATORS.items():
    estimator.fit(X_train, y_train)
    y_test_predict[name] = estimator.predict(X_test)

1
线性回归不太适用于这个问题,建议使用神经网络。 - Parsa
1
@user2662639 这个问题是一个sklearn演示。神经网络并不是万能的 - 在这个问题中只有300个训练样本,所以它们的性能不会有显著的差异(尝试将未经训练的“深度”网络拟合到300个样本中,看看会发生什么。你的网络结构需要更小才有可能工作)。更好的解决方案是在输出结构中施加平滑性,例如通过正则化矩阵分解等方法。 - eqzx
1个回答

1

你说得对。

每张图像中有4096个像素。测试集中的每个输出像素都是训练系数针对该像素和测试集中的2048个输入像素的线性组合。

如果您查看sklearn线性回归文档,您会发现多目标回归的系数形状为(n_targets, n_features) (2048个目标,2048个特征)

In [24]: ESTIMATORS['Linear regression'].coef_.shape
Out[24]: (2048, 2048)

在底层,它调用scipy.linalg.lstsq,因此需要注意的是,系数之间没有“信息共享”,也就是说,每个输出都是所有2048个输入像素的单独线性组合。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接