Published
Edited
Oct 6, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
telcoData = new SimpleData()
.loadDataFromUrl({
url:"https://raw.githubusercontent.com/ealecho/makailabs/main/WA_Fn-UseC_-Telco-Customer-Churn.csv"
})
Insert cell
Insert cell
descricbeTelcoData = telcoData.clone().describe()
Insert cell
showTable(descricbeTelcoData)
Insert cell
showTable(telcoData)
Insert cell
Insert cell
Insert cell
checkTelcoData = telcoData.clone().checkValues()
Insert cell
showTable(checkTelcoData)
Insert cell
Insert cell
telcoData1 = telcoData.clone().formatAllKeys()
Insert cell
showTable(telcoData1)
Insert cell
Insert cell
Insert cell
showTable(cleanDataCheck)
Insert cell
Insert cell
showTable(telcoDataCleanNum)
Insert cell
Insert cell
Insert cell
Insert cell
showTable(telcoData2Check)
Insert cell
showTable(telcoData2)
Insert cell
Insert cell
Insert cell
showTable(telcoData3)
Insert cell
Insert cell
telcoData4 = telcoData3
.clone()
.replaceValues({
key: "paymentMethod",
oldValue: "automatic",
newValue: "",
method: "partialString"
})
Insert cell
paymentMethods = telcoData4
.clone()
.getUniqueValues({key: "paymentMethod"})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
showTable(telcoData4)
Insert cell
Insert cell
showTable(telcoLabelEncod)
Insert cell
Insert cell
telcoDf = new dfd.DataFrame(telcoLabelEncod.clone().getData())
Insert cell
Insert cell
showTable(telcoOneHotEncode)
Insert cell
telcoData4.getData()
Insert cell
telcoData4.getData().map(r => r["gender"])
Insert cell
// nomalized

normalize = tensor =>
tf.div(
tf.sub(tensor, tf.min(tensor)),
tf.sub(tf.max(tensor), tf.min(tensor))
);
Insert cell
oneHot = (val, categoryCount) =>
Array.from(tf.oneHot(val, categoryCount).dataSync());
Insert cell
// convert categorical features into on-hot encoded vectors

toCategorical = (data, column) => {

// extract values from objects in the array using column name
const values = data.map(r => r[column]);

//keep only unique values
const uniqueValues = new Set(values);

const mapping = {};

//populate our mapping object
Array.from(uniqueValues).forEach((value, index) => {
mapping[index] = value;
});

const encoded = values.map(v => {
if (!v) {
return 0;
}
return mapping[v];
})
.map(v => oneHot(v, uniqueValues.size));

return encoded;
};
Insert cell
Insert cell
telcoDataNormalized = {
// min-max normalization (numeric variables)
//let min_max_columns = ['tenure', 'monthlyCharges', 'totalCharges']
let newTelcoDf = new dfd.DataFrame(telcoOneHotEncode.getData())

let scaler = new dfd.MinMaxScaler()
scaler.fit(newTelcoDf)

let telcoDataNormalised = dfd.toJSON(scaler.transform(newTelcoDf))

return new SimpleData({
data: telcoDataNormalised,
})

}
Insert cell
showTable(telcoDataNormalized)
Insert cell
Insert cell
Insert cell
Insert cell
// X_t = telcoDataTransformed.drop({columns: "churn"})
Insert cell
// Y = telcoDataTransformed.loc({columns: ["churn"]})
Insert cell
// telcoDataNormalized.getLength()
Insert cell

spiltData = {
//transform data into danfo dataframe
let telcoDataTransformed = new dfd.DataFrame(telcoDataNormalized.getData())

//select independent variables (categorical features) as a tensor
const x = telcoDataTransformed.drop({columns: "churn"}).tensor

const X_t = tf.tensor2d(await x.array())

// select dependent variables (churn)
const y = telcoDataTransformed.loc({columns: ["churn"]}).tensor
//const X_t = telcoDataTransformed.loc({columns: ["churn"]}).tensor
// return [X.tensor, Y.tensor]

let testSize = 0.1;

const splitIdx = parseInt((1- testSize) * telcoDataNormalized.getLength(), 10);

const [xTrain, xTest] = tf.split(X_t, [splitIdx, telcoDataNormalized.getLength() - splitIdx]);
const [yTrain, yTest] = tf.split(y, [splitIdx, telcoDataNormalized.getLength() - splitIdx]);

return [xTrain, xTest, yTrain, yTest];
};
Insert cell
Insert cell
Insert cell
model = {

let [xTrain, xTest, yTrain, yTest] = spiltData;
//A sequential model is any model where the outputs of one layer are the inputs to the next layer, i.e. the model topology is a simple 'stack' of layers, with no branching or skipping....
const model = tf.sequential();

//add a layer with 32 neurons
model.add(
tf.layers.dense({
units: 32,
activation: "relu",
//...This means that the first layer passed to a tf.Sequential model should have a defined input shape
inputShape: [xTrain.shape[1]]
})
);

//add a layer with 64 neurons
model.add(
tf.layers.dense({
units: 64,
activation: "relu"
})
);

//add a layer with 2 neurons
model.add(
tf.layers.dense({
units: 1,
activation: "softmax"
})
);

//here our neural network has two hidden layers, the inputlayer is implied from the
// input shape of the first layer we created

//Configures and prepares the model for training and evaluation.
model.compile({
optimizer: tf.train.adam(0.001),
loss: "binaryCrossentropy",
metrics: ["accuracy"]
});

const trainingConfig = {
batchSize: 32,
epochs: 32, // train model for 100 epochs
shuffle: true, // shuffle the data
validationSplit: 0.1, //use 10% of that data for validation
}
const his = await model.fit(xTrain, yTrain, trainingConfig)
return his
}
Insert cell
showHistory(model, ['loss', 'acc'], { height: 200 })
Insert cell
Insert cell


// history = Error: target expected a batch of elements where each example has shape [2] (i.e.,tensor shape [*,2]) but the target received an input with 6328 examples, each with shape [40] (tensor shape [6328,40]
Insert cell
history = {
const [xTrain, xTest, yTrain, yTest] = spiltData;
const trainingConfig = {
batchSize: 32,
epochs: 10, // train model for 100 epochs
shuffle: true, // shuffle the data
validationSplit: 0.1, //use 10% of that data for validation
}
//const his = await model.fit(xTrain, yTrain, trainingConfig)

}
Insert cell
lossContainer = html`<div></div>`;
Insert cell
Insert cell
Insert cell
SimpleData = {
const sda = await import("https://cdn.skypack.dev/simple-data-analysis");
return sda.SimpleData
};
Insert cell
Insert cell
dfd = require("danfojs@1.0.5/lib/bundle.js").catch(() => {
window.dfd.Series.prototype.print = window.dfd.DataFrame.prototype.print = function () {
return print(this);
};
return window.dfd;
})
Insert cell
Insert cell
tf = require('@tensorflow/tfjs')//require("@tensorflow/tfjs-core")
Insert cell
tf.setBackend("cpu")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
showValuesDistribution = async tensor => {
const elem = html`<div></div>`;
tfvis.show.valuesDistribution(elem, tensor);

return elem;
}
Insert cell
Insert cell
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