Published
Edited
Jun 5, 2022
8 forks
Importers
12 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
food = (await FileAttachment("food-texture.csv").csv({ typed: true })).map(
(row) => {
// add row names column
row.Name = row[""];
// and delete empty column
delete row[""];
return row;
}
)
Insert cell
SummaryTable(food)
Insert cell
Insert cell
food_scaled = scale(food.map(({ Name, ...columnsToKeep }) => columnsToKeep))
Insert cell
SummaryTable(food_scaled)
Insert cell
Insert cell
Insert cell
pca = new PCA(asMatrix(food_scaled), { center: true, scale: true })
Insert cell
Insert cell
viewof loadings = Inputs.table(
pca.getEigenvectors().data.map((eigenvectorForPCs, variableIndex) => {
const variable = Object.keys(food_scaled[0])[variableIndex];
const row = {
Variable: variable
};
eigenvectorForPCs.forEach((value, pcIndex) => {
row[`PC${pcIndex + 1}`] = value;
});
return row;
})
)
Insert cell
Insert cell
pca.getStandardDeviations()
Insert cell
Insert cell
pca.getExplainedVariance()
Insert cell
Insert cell
viewof scores = Inputs.table(
pca
.predict(asMatrix(food_scaled))
.toJSON()
.map((row, rowIndex) => {
const columns = Object.keys(food_scaled[rowIndex]);
const rowObj = {
Name: food[rowIndex].Name
};
columns.forEach((column, colIndex) => {
rowObj[`PC${colIndex + 1}`] = row[colIndex];
});
return rowObj;
})
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
PCA = (await require("https://bundle.run/ml-pca@4.0.2")).PCA
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more