我正在尝试理解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)