0

I am new to coding and have an assignment where I have to answer the following questions.

-Subtract the mean values (mn) from each of the points (i.e. center the points around the origin and store these in a new matrix called A (This is the first step in PCA).

-Calculate the 3×3 Gram matrix

I am genuinely lost, please help

%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)

marqzl10
  • 1
  • 1
  • Welcome to coding and SO! Does this answer your question? [Principal Component Analysis (PCA) in Python](https://stackoverflow.com/questions/13224362/principal-component-analysis-pca-in-python) I suggest you take a look at the third answer. – yann ziselman Jun 21 '21 at 06:53

0 Answers0