python - 线性回归 - 图像

标签 python computer-vision scikit-learn linear-regression

我正在努力专注于 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)

最佳答案

你是对的。

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

如果您查看 sklearn Linear Regression documentation ,你会看到多目标回归的系数的形状是 (n_targets, n_features) (2048 targets, 2048 features)

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

在引擎盖下,它正在调用 scipy.linalg.lstsq ,因此请务必注意,系数之间没有“信息共享”,因为每个输出都是所有 2048 个输入像素的单独线性组合。

关于python - 线性回归 - 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33018873/

相关文章:

opencv - Canny Edge 的自适应参数

python - reshape 和转置问题

python - 随机森林模型拟合

python - PyCharm 模块导入停止正常工作 (OS X)

python - 如何安装matplotlib? [Python]

python - 人脸检测抛出错误:函数 cv::CascadeClassifier::detectMultiScale 中的 !empty()

python - 面临 ImportError : No module named discriminant_analysis

python - 数据帧移位功能

python - 如何在 python 中打印列表理解的进度?

c - 扩展 ffmpeg extract_mvs.c 示例