Published
Edited
Aug 16, 2019
Insert cell
Insert cell
Insert cell
Insert cell
params=d3.range(1000).map(n => ({
age: Math.floor(Math.random()*80),
gender: Math.random() > 0.5 ? 'M' : 'F',
appts: Math.floor(Math.log(1/Math.random()))
}))
Insert cell
trainingData=params.map(({age, gender, appts}) => [age, gender, appts])
Insert cell
method = ({age, gender, appts}) =>
(gender == 'M' ?
(appts == 0 ? 10 : 0) + (age < 5 ? 20 : age > 60 ? 40 : 10) :
(appts == 0 ? 10 : 0) + (age < 10 ? 25 : age > 65 ? 50 : 15)
)// + Math.floor(Math.random()*10-5)
Insert cell
results=params.map(method)
Insert cell
Insert cell
regression = new ml.DecisionTreeRegression(options)
Insert cell
regression.train(trainingData, results)
Insert cell
arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length
Insert cell
arrAvg(results)
Insert cell
regression.predict([[25, 'M', 3]])//, [85, 'F', 0]])
Insert cell
Insert cell
x = d3.range(100).map(d => [d * 0.01, d*0.02])
Insert cell
y = x.map(d => Math.sin(d[0]))
Insert cell
reg = new ml.DecisionTreeRegression()
Insert cell
reg.train(x, y)
Insert cell
estimations = reg.predict(x)
Insert cell
Insert cell
exampleTest = ['Rainy'//, 'Hot', 'High', 'False'],
,'Rainy'//, 'Hot', 'High', 'True'],
,'Overcast'//, 'Hot', 'High', 'False'],
,'Sunny'//, 'Mild', 'High', 'False'],
,'Sunny'//, 'Cool', 'Normal', 'False'],
,'Sunny'//, 'Cool', 'Normal', 'True'],
,'Overcast'//, 'Cool', 'Normal', 'True'],
,'Rainy'//, 'Mild', 'High', 'False'],
,'Rainy'//, 'Cool', 'Normal', 'False'],
,'Sunny'//, 'Mild', 'Normal', 'False'],
,'Rainy'//, 'Mild', 'Normal', 'True'],
,'Overcast'//, 'Mild', 'High', 'True'],
,'Overcast'//, 'Hot', 'Normal', 'False'],
,'Sunny']//, 'Mild', 'High', 'True']]
Insert cell
exampleResults = [26,30,48,46,62,23,43,35,38,48,48,62,44,30]
Insert cell
exampleRegression = new ml.DecisionTreeRegression();
Insert cell
exampleRegression.train(exampleTest, exampleResults)
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