平均值和 Gram 矩阵

时间:2021-06-24 15:19:05

标签: python numpy

我花了无数个小时浏览帖子,但似乎无法得到正确的答案。

-从每个点中减去平均值 (mn)(即以原点为中心,将这些点存储在一个名为 A 的新矩阵中(这是 PCA 中的第一步)。

-计算 3×3 克矩阵

如果有人可以帮助我,非常感谢。

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)

#get the data file from the internet:
from urllib.request import urlopen, urlretrieve

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00229/Skin_NonSkin.txt'
file = "Skin_NonSkin.txt"

response = urlopen(url)
data = response.read()      # a `bytes` object
text = data.decode('utf-8') 
lines = text.split('\r\n')

data = []

#Read in file line by line
for line in lines:
    try:
        if line:
            data.append(list(map(int, line.split('\t'))))
    except:
        print('invalid line of data:',line)
response.close()

#Convert the file to a list of points
P = np.matrix(data)
P.shape

#Mask out only face values and keep just the RGBs
mask = np.array(P[:,3]==1)
mask = mask.flatten()
points = P[mask,:]

## Change order to Red, Green, Blue
points = points[:,(2,1,0)]

# Plot the points in 3D using their actual color values
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(points[:,0], points[:,1], points[:,2], c=points/255)

ax.set_xlabel('Red');
ax.set_ylabel('Green');
ax.set_zlabel('Blue');

points = points[:,(2,1,0)]

red_mean = np.mean(points[:,0])
green_mean = np.mean(points[:,1])
blue_mean = np.mean(points[:,2])
mn = np.array((red_mean,green_mean,blue_mean), dtype=float)

1 个答案:

答案 0 :(得分:0)

您可以计算给定轴上矩阵的均值,然后直接减去。

mn = np.mean(points, axis=0)
C = points - mn;

Gramm 矩阵 G 定义为矩阵,其条目是 C 列的内积,为了使标度对 C 的大小不变,我们将结果除以它的行数

G = (C.T @ C) / C.shape[0]