Public
Edited
Apr 18
Paused
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
scores = CalculatePca(iris)
Insert cell
CalculatePca = function (data) {
// scale numeric values
const idx = Object.keys(data[0]).filter(key => typeof(data[0][key])=='number')
console.log('idx',idx)
console.log('data',data)

const dt = scale(data.map(obj => Object.fromEntries(Object.entries(obj).filter(([key])=> idx.includes(key)))))
console.log('dt',dt)
console.log('asMatrix(dt)',asMatrix(dt))

const pca = new PCA.PCA(asMatrix(dt), { center: true, scale: true })
console.log('pca',pca)
const scores = pca
.predict(asMatrix(scale(data.map((row) => _.omit(row, ['species','Name'])))))
.toJSON()
.map((row, rowIndex) => {
const columns = Object.keys(data[rowIndex]);
const rowObj = {
group: data[rowIndex]['species'],
Name: data[rowIndex]['Name']
};
columns.forEach((column, colIndex) => {
rowObj[`PC${colIndex + 1}`] = row[colIndex];
});
return rowObj;
}).map(({PC1,PC2,group,Name}) => ({PC1,PC2,group,Name}))
const groups = [...new Set(scores.map( d => d.group))]
return scores
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3tip = require('d3-tip')
Insert cell
// R = require("https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js")
Insert cell
// pca = require('ml-pca')
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