Public
Edited
Nov 9, 2023
Insert cell
Insert cell
Iris.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
function encodeSpecies(speciesArr) {
const encodingObj = {
"Iris-setosa": [1,0,0],
"Iris-versicolor": [0,1,0],
"Iris-virginica": [0,0,1]
}

const encodedData = []
speciesArr.forEach(species => {
encodedData.push(encodingObj[species])
})

return encodedData
}
Insert cell
function generateDataset(splitSize = 0.9) {
const features = df.map(data => {
return [
data['SepalLengthCm'],
data['SepalWidthCm'],
data['PetalLengthCm'],
data['PetalWidthCm']
]
})
const labels = df.map(data => data['Species'])
return {
features: tf.tensor(features),
labels: tf.tensor(encodeSpecies(labels))
};
}
Insert cell
data = generateDataset()
Insert cell
Insert cell
function IrisLogisticRegression() {
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [4],
units: 3,
activation: 'softmax'
}))
model.compile({
optimizer: "sgd",
loss: "categoricalCrossentropy",
metrics: ['accuracy']
})

return model
}
Insert cell
history = IrisLogisticRegression().fit(
data.features,
data.labels,
{
validationSplit: 0.1,
epochs: 200,
callbacks: {
onEpochEnd: (epoch, logs) => {
console.log(`Epoch ${epoch}: loss = ${logs.loss}, accuracy = ${logs.acc}`);
}
}
}
)
Insert cell
Insert cell
Insert cell
tf = require('@tensorflow/tfjs')
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