You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.8 KiB
Plaintext
144 lines
3.8 KiB
Plaintext
TITLE:: FluidPCA
|
|
summary:: Dimensionality Reduction with Principal Component Analysis
|
|
categories:: Dimensionality Reduction, Data Processing
|
|
related:: Classes/FluidMDS, Classes/FluidDataSet
|
|
|
|
DESCRIPTION::
|
|
|
|
Principal Components Analysis of a link::Classes/FluidDataSet::
|
|
|
|
https://scikit-learn.org/stable/modules/decomposition.html#principal-component-analysis-pca
|
|
|
|
CLASSMETHODS::
|
|
|
|
METHOD:: new
|
|
Make a new instance
|
|
ARGUMENT:: server
|
|
The server on which to run this model
|
|
|
|
INSTANCEMETHODS::
|
|
|
|
PRIVATE:: init
|
|
|
|
METHOD:: fit
|
|
Train this model on a link::Classes/FluidDataSet:: but don't transform the data
|
|
ARGUMENT:: dataset
|
|
A link::Classes/FluidDataSet:: to analyse
|
|
ARGUMENT:: k
|
|
The number of dimensions to reduce to
|
|
ARGUMENT:: action
|
|
Run when done
|
|
|
|
METHOD:: transform
|
|
Given a trained model, apply the reduction to a source link::Classes/FluidDataSet:: and write to a destination. Can be the same
|
|
ARGUMENT:: sourceDataset
|
|
Source data, or the dataset name
|
|
ARGUMENT:: destDataset
|
|
Destination data, or the dataset name
|
|
ARGUMENT:: action
|
|
Run when done
|
|
|
|
METHOD:: fitTransform
|
|
link::Classes/FluidPCA#fit:: and link::Classes/FluidPCA#transform:: in a single pass
|
|
ARGUMENT:: sourceDataset
|
|
Source data, or the dataset name
|
|
ARGUMENT:: destDataset
|
|
Destination data, or the dataset name
|
|
ARGUMENT:: k
|
|
The number of dimensions to reduce to
|
|
ARGUMENT:: action
|
|
Run when done
|
|
|
|
METHOD:: transformPoint
|
|
Given a trained model, transform the data point in a link::Classes/Buffer:: and write to an output
|
|
ARGUMENT:: sourceBuffer
|
|
Input data
|
|
ARGUMENT:: destBuffer
|
|
Output data
|
|
ARGUMENT:: action
|
|
Run when done
|
|
|
|
EXAMPLES::
|
|
|
|
code::
|
|
s.boot;
|
|
//Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers, a FluidStandardize and a FluidPCA
|
|
(
|
|
~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
|
|
~raw = FluidDataSet(s,\pca_help_12D);
|
|
~standardized = FluidDataSet(s,\pca_help_12Ds);
|
|
~reduced = FluidDataSet(s,\pca_help_2D);
|
|
~audio = Buffer.read(s,~audiofile);
|
|
~mfcc_feature = Buffer.new(s);
|
|
~stats = Buffer.alloc(s, 7, 12);
|
|
~datapoint = Buffer.alloc(s, 12);
|
|
~standardizer = FluidStandardize(s);
|
|
~pca = FluidPCA(s);
|
|
)
|
|
|
|
|
|
// Load audio and run an mfcc analysis, which gives us 13 points (we'll throw the 0th away)
|
|
(
|
|
~audio = Buffer.read(s,~audiofile);
|
|
FluidBufMFCC.process(s,~audio, features: ~mfcc_feature);
|
|
)
|
|
|
|
// Divide the time series in 100, and take the mean of each segment and add this as a point to
|
|
// the 'raw' FluidDataSet
|
|
(
|
|
{
|
|
var trig = LocalIn.kr(1, 1);
|
|
var buf = LocalBuf(12, 1);
|
|
var count = PulseCount.kr(trig) - 1;
|
|
var chunkLen = (~mfcc_feature.numFrames / 100).asInteger;
|
|
var stats = FluidBufStats.kr(
|
|
source: ~mfcc_feature, startFrame: count * chunkLen,
|
|
startChan:1, numFrames: chunkLen, stats: ~stats, trig: trig
|
|
);
|
|
var rd = BufRd.kr(12, ~stats, DC.kr(0), 0, 1);
|
|
var bufWr, dsWr;
|
|
12.do{|i|
|
|
bufWr = BufWr.kr(rd[i], buf, DC.kr(i));
|
|
};
|
|
dsWr = FluidDataSetWr.kr(\pca_help_12D, buf: buf, trig: Done.kr(stats));
|
|
LocalOut.kr( Done.kr(dsWr));
|
|
FreeSelf.kr(count - 98);
|
|
}.play;
|
|
)
|
|
|
|
|
|
//First standardize our dataset, so that the MFCC dimensions are on comensurate scales
|
|
//Then apply the PCA in-place on the standardized data
|
|
//Download the dataset contents into an array for plotting
|
|
(
|
|
~reducedarray = Array.new(100);
|
|
~standardizer.fitTransform(~raw, ~standardized);
|
|
~pca.fitTransform(~standardized, ~reduced, 2, action:{
|
|
~reduced.dump{|x| 100.do{|i|
|
|
~reducedarray.add(x["data"][i.asString])
|
|
}};
|
|
});
|
|
|
|
)
|
|
|
|
|
|
//Visualise the 2D projection of our original 12D data
|
|
(
|
|
d = ~reducedarray.flatten(1).unlace.deepCollect(1, { |x| x.normalize});
|
|
w = Window("scatter", Rect(128, 64, 200, 200));
|
|
w.drawFunc = {
|
|
Pen.use {
|
|
d[0].size.do{|i|
|
|
var x = (d[0][i]*200);
|
|
var y = (d[1][i]*200);
|
|
var r = Rect(x,y,5,5);
|
|
Pen.fillColor = Color.blue;
|
|
Pen.fillOval(r);
|
|
}
|
|
}
|
|
};
|
|
w.refresh;
|
|
w.front;
|
|
)
|
|
::
|