# Your first data analysis with .NET Jupyter Notebook and Daany.DataFrame

Note: The .NET Jupyter notebook for this blog post can be found here.

# The Structure of Daany.DataFrame

The main part of Daany project is Daany.DataFrame – an c# implementation of a data frame. A data frame is a software component used for handling tabular data, especially for data preparation, feature engineering, and analysis during the development of machine learning models. The concept of Daany.DataFrame implementation is based on simplicity and .NET coding standard. It represents tabular data consisting of columns and rows. Each column has name and type and each row has its index and label.

Usually, rows indicate a zero axis, while columns indicate axis one.

The following image shows a data frame structure:

The basic components of the data frame are:

• header – list of column names,
• index – list of object representing each row,
• data – list of values in the data frame,
• missing value – data with no values in data frame.

The image above shows the data frame components visually, and how they are positioned in the data frame.

# Create Data Frame from a text based file

The data we used are stored in files, and they must be load into application memory in order to be analyzed and transformed. Loading data from files by using Daany.DataFrame is as easy as calling one method.

By using static method DataFrame.FromCsv a user can create data frame object from the csv file. Otherwise, data frame can be persisted on disk by calling static method DataFrame.ToCsv.

The following code shows how to use static methods ToCsv and FromCsv to show persisting and loading data to data frame:

string filename = "df_file.txt";
//define a dictionary of data
var dict = new Dictionary&lt;string, List&gt;
{
{ "ID",new List() { 1,2,3} },
{ "City",new List() { "Sarajevo", "Seattle", "Berlin" } },
{ "Zip Code",new List() { 71000,98101,10115 } },
{ "State",new List() {"BiH","USA","GER" } },
{ "IsHome",new List() { true, false, false} },
{ "Values",new List() { 3.14, 3.21, 4.55 } },

};

//create data frame with 3 rows and 7 columns
var df = new DataFrame(dict);

//first Save data frame on disk and load it
DataFrame.ToCsv(filename, df);

//create data frame with 3 rows and 7 columns
var dfFromFile = DataFrame.FromCsv(filename, sep:',');

//show dataframe
dfFromFile


First, we created a data frame from the dictionary collection. Then we store the data frame to file. After successfully saving, we load the same data frame from the CSV file. The end of the code snippet put asserts in order to prove everything is correctly implemented. The output of the code cell is:

In case the performance is important, you should pass column types to the FromCSV method in order to achieve up to 50% of loading time. For example the following code loads the data from the file, by passing predefined column types:

//defined types of the column
var colTypes1 = new ColType[] { ColType.I32, ColType.IN, ColType.I32, ColType.STR, ColType.I2, ColType.F32, ColType.DT };
//create data frame with 3 rows and 7 columns
var dfFromFile = DataFrame.FromCsv(filename, sep: ',', colTypes: colTypes1);


And we got the same result:

Data can be loaded directly from the web storage by using FromWebstatic method. The following code shows how to load the Concrete Slump Test data from the web. The data set includes 103 data points. There are 7 input variables, and 3 output variables in the data set: Cement, Slag, Fly ash, Water, SP, Coarse Aggr.,Fine Aggr., SLUMP (cm), FLOW (cm), Strength (Mpa). The following code load the Concrete Slump Test data set into Daany DataFrame:

//define web url where the data is stored
var url = "https://archive.ics.uci.edu/ml/machine-learning-databases/concrete/slump/slump_test.data";
//
var df = DataFrame.FromWeb(url);


Once we have the data into the application memory, we can perform some statistical calculations. First, let’s see the structure of the data by calling the Describe method:

df.Describe(false)


Now, we see we have a data frame with 103 rows and all columns are of numerical type. The frequency of the data indicated that values are mostly not repeated. From the maximum and minimum values, we can see the data have no outlines since distributions of the values are tends to be normal.

# Data Visualization

Let’s perform some visualization just to see how visually data look like. As first let’s see the Slump distribution with respect of SP and Fly ash:

var chart = Chart.Plot(
new Graph.Scatter()
{
x = df["SP"],
y = df["Fly ash"],
mode = "markers",
marker = new Graph.Marker()
{
color = df["SLUMP(cm)"].Select(x=>x),
colorscale = "Jet"
}
}
);

var layout = new Layout.Layout(){title="Slump vs. Cement and Slag"};
chart.WithLayout(layout);
chart.WithXTitle("Cement");
chart.WithYTitle("Slag");

display(chart);


From the chart above, we cannot see any relation between those two columns. Let’s see the chart made between Slump and Flow:

var chart = Chart.Plot(
new Graph.Scatter()
{
x = df["SLUMP(cm)"],
y = df["FLOW(cm)"],
mode = "markers",
}
);

var layout = new Layout.Layout(){title="Slump vs. Cement and Slag"};
chart.WithLayout(layout);
chart.WithLegend(true);
chart.WithXTitle("Slump");
chart.WithYTitle("Flow");

display(chart);


We can see some relation in the chart and the relation is positive. This means as Slupm is growing, Flow value grows as well. If we want to measure the relation between the columns we can do that with the following code:

var x1= df["SLUMP(cm)"].Select(x=>Convert.ToDouble(x)).ToArray();
var x2= df["FLOW(cm)"].Select(x=>Convert.ToDouble(x)).ToArray();

//The Pearson coefficient is calculated by
var r=x1.R(x2);
r


The correlation is 0.90 which indicates a strong relationship between those two columns.

The complete .NET Jupyter Notebook for this blog post can be found here

# Introduction

Daany is .NET data analytics library written in C# and it supposed to be a tool for data preparation, feature engineering and other kinds of data transformations prior to creating ml-ready data set. It is .NET Core based library with ability to run on Windows Linux based distribution and Mac. It is based on .NET Standard 2.1.

Besides data analysis, the library implements a set of statistics or data science features e.g. time series decompositions, optimization performance parameters and similar.

Currently Daany project consists of four main components:

• Daany.DataFrame,
• Daany.Stats,
• Daany.MathStuff and
• Daany.DataFrame.Ext

The main Daany component is Daany.DataFrame – a data frame implementation for data analysis. It is much like Pandas but the component is not going to follow pandas implementation. It is suitable for doing data exploration and preparation with C# Jupyter Notebook. In order to create or load data into data frame it doesn’t require any predefined class type. In order to defined relevant value type of each column all data are parsed internally during data frame creation. The Daany.DataFrame implements set of powerful features for data manipulation, handling missing values, calculated columns, merging two or more data frames into one, and similar. It is handy for extracting its rows or columns as series of elements and put into the chart to visualizing the data.

Daany.Stat is a collection of statistics features e.g. time series decompositions, optimization, performance parameters and similar.

Daany.Math is a component within data frame with implementation of od matrix and related linear algebra capabilities. It also contains some implementation of other great open source projects. The component is not going to be separate NuGet package.

Daany.DataFrame.Ext contains extensions for Daany.DataFrame component, but they are related to other projects mostly to ML.NET. The Daany.DataFrame should not be dependent on ML.NET and other libraries. So, any future data frame feature which depends on something other than Daany.Math, should be placed in Daany.Ext.

The project is developed as a need to have a set of data transformation features in one library while I am working with machine learning. So, I thought it might help to others. Currently, the library has pretty much data transformation features and might be your number one data analytics library on .NET platform. Collaboration to the project is also welcome.

Daany is 100% .NET Core component and can be run on any platform .NET Core supports, from the Windows x86/x64 to Mac or Linux based OS. It can be used by Visual Studio or Visual Studio Code. It consisted of 3 NuGet packages, so the easiest way to start with it is to install the packages in your .NET application. Within Visual Studio create or open your .NET application and open NuGet packages window. Type Daany in the browse edit box and hit enter. You can find four packages starting with Daany. You have few options to install the packages.

1. Install Daany.DataFrame – only. Use this option if you want only data analysis by using data frame. Once you click Install button, Daany.DataFrame and Daany.Math will be installed into your project app.

2. Install Daany.Stat package. This package already contains DataFrame, as well as time series decomposition and related statistics features.

Once you install the packages, you can start developing your app using Daany packages.

# Using Daany as assembly reference

Since Daany has no dependency to other libraries you can copy three dlls and add them as reference to your project.

In order to do so clone the project from http://github.com/bhrnjica/daany,build it and copy Daany.DataFrame.dll, Daany.Math.dll and Daany.Stat.dll to your project as assembly references. Whole project is just 270 KB.

# Using Daany with .NET Jupyter Notebook

Daany library is ideal with .NET Jupyter Notebook, and some of the great notebooks are implemented already, and can be viewed at http://github.com/bhrnjica/notebooks. The GitHub project contains the code necessary to run the notebooks in Binder, a Jupyter Virtual Environment, and try Daany without any local installation. So the first recommendation is to try Daany with already implemented notebooks using Binder.com.

# Namespaces in Daany

Daany project contains several namespaces for separating different implementation. The following list contains relevant namespaces:

• using Daany – data frame and related code implementation,
• using Daany.Ext – data frame extensions, used with dependency on third party library,
• using Daany.MathStuff – math related stuff implemented in Daany,
• using Daany.Optimizers – set of optimizers like SGD,
• using Daany.Stat – set of statistics implementations in the project.

That’s all for this post. Next blog posts will show more exciting implementation using Daany.

# Predictive Maintenance on .NET Platform

## Summary

However, this notebook is completely implemented on .NET platform using:

• C# Jupyter Notebook,- Jupyter Notebook experience with C# and .NET,
• ML.NET – Microsoft open source framework for machine learning, and
• DaanyDAta ANalYtics open source library for data analytics. It can be installed as Nuget package.

There are small differences between this notebook and the notebooks at the official azure gallery portal, but in most cases, the code follows the steps defined there. The purpose of this notebook is to demonstrate how to use .NET Jupyter Notebook with Daany.DataFrame and ML.NET in order to prepare the data and build the Predictive Maintenance Model on .NET platform. But first lets see what is Predictive Maintenance and why is it important.

## Quick Introduction to Predictive Maintenance

Simply speaking it is a technique to determine (predict) the failure of the machine component in the near future so that the component can be replaced based on the maintenance plan before it fails and stop the production process. The Predictive maintenance can improve the production process and increase the productivity. By successfully handling with predictive maintenance we are able to achieve the following goals:

• reduce the operational risk of mission-critical equipment

• control cost of maintenance by enabling just-in-time maintenance operations

• discover patterns connected to various maintenance problems

• provide Key Performance Indicators.

The following image shows different type of maintenance in the production.

### Predictive maintenance data collection

In order to handle and use this technique we need a various data from the production, including but not limited to:

• telemetry data from the observed machines (vibration, voltage, temperature etc)
• errors and logs data relevant to each machine,
• failure data, when a certain component is replaced, etc
• quality and accuracy data, machine properties, models, age etc.

### 3 Steps in Predictive Maintenance

Usually, every Predictive Maintenance technique should proceed by the following 3 main steps:

1. Collect Data – collect all possible descriptions,historical and real-time data, usually by using IOT devices, various loggers, technical documentation, etc.

2. Predict Failures – collected data can be used and transformed into machine learning ready data sets, and build a machine learning model to predict the failures of the components in the set of machines in the production.

3. React – by obtaining the information which components will fail in the near future, we can activate the process of replacement so the component will be replaced before it fails, and the production process will not be interrupted.

## Predict Failures

In this article, the second step will be presented, which will be related to data preparation. In order to predict failures in the production process, a set of data transformations, cleaning, feature engineering, and selection must be performed to prepare the data for building a machine learning model. The data preparation part plays a crucially step in the model building since a quality data preparation will directly reflect on the model accuracy and reliability.

## Software requirements

In this article, the complete procedure in data preparation is presented. The whole process is performed using:

• .NET Core 3.1 – the latest .NET platform version,

• .NET Jupyter Notebook– .NET implementation of popular Jupyer Notebook,

• ML.NET – Microsoft open-source framework for Machine Learning on .NET Platform and

• DaanyDAta ANalYtics library. It can be found at Github but also as Nuget package.

### Notebook preparation

In order to complete this task, we should install several Nuget packages and include several using keywords. The following code block shows the using keywords, and additional code related to notebook output format.

Note: nuget package installation must be in the first cell of the Notebook, otherwise the notebook will not work as expected. Hope this will be changed once the final version would be released.

//using Microsoft.ML.Data;
using XPlot.Plotly;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
//using statement of Daany package
using Daany;
using Daany.MathStuff;
using Daany.Ext;
//
using Microsoft.ML;

//DataFrame formatter
using Microsoft.AspNetCore.Html;
Formatter<DataFrame>.Register((df, writer) =>
{
//renders the rows
var rows = new List<List<IHtmlContent>>();
var take = 20;
//
for (var i = 0; i < Math.Min(take, df.RowCount()); i++)
{
var cells = new List<IHtmlContent>();
foreach (var obj in df[i])
{
}
}
var t = table(
tbody(rows.Select(r => tr(r))));

writer.Write(t);
}, "text/html");


In order to start with data preparation, we need data. The data can be found at Azure blob storage. The data is maintained by Azure Gallery Article.

Once the data are downloaded from the blob storage, they will not be downloaded again and they will be used as local copies.

### The Data

The data we are using for predictive maintenance can be classified to:

• telemetry – which collects historical data about machine behavior (voltage, vibration, etc)
• errors – the data about warnings and errors in the machines
• maint – data about replacement and maintenance for the machines,
• machines – descriptive information about the machines,
• failures – data when a certain machine is stopped, due to component failure.

We load all the files in order to fully prepare data for the training process. The following code sample loads the data in to application memory.

%%time
//Load ALL 5 data frame files
//DataFrame Cols: datetime,machineID,volt,rotate,pressure,vibration
var telemetry = DataFrame.FromCsv("data/PdM_telemetry.csv", dformat: "yyyy-mm-dd hh:mm:ss");
var errors = DataFrame.FromCsv("data/PdM_errors.csv", dformat: "yyyy-mm-dd hh:mm:ss");
var maint = DataFrame.FromCsv("data/PdM_maint.csv", dformat: "yyyy-mm-dd hh:mm:ss");
var failures = DataFrame.FromCsv("data/PdM_failures.csv", dformat: "yyyy-mm-dd hh:mm:ss");
var machines = DataFrame.FromCsv("data/PdM_machines.csv", dformat: "yyyy-mm-dd hh:mm:ss");


## Telemetry

The first data source is the telemetry data about machines. It consists of voltage, rotation, pressure, and vibration measurements measured from 100 machines in real-time hourly. The time period the data has been collected is during the year 2015. The following data shows the first 10 records in the dataset.

A description of the whole dataset is shown on the next cell. As can be seen, we have nearly million records for the machines, which is good starting point for the analysis.

In case we want to see the visualization of the telemetry data, we can select on of several column and show it.

## Errors

One of the most important information in every Predictive Maintenance system is Error data. Actually errors are non-breaking recorded events while the machine is still operational. The error date and times are rounded to the closest hour since the telemetry data is collected at an hourly rate.

errors.Head()


//count number of errors
var barValue = errors["errorID"].GroupBy(v => v)
.OrderBy(group => group.Key)
.Select(group => Tuple.Create(group.Key, group.Count()));

//Plot Errors data
var chart = Chart.Plot(
new Graph.Bar()
{
x = barValue.Select(x=>x.Item1),
y = barValue.Select(x=>x.Item2),
//  mode = "markers",
}

);
var layout = new XPlot.Plotly.Layout.Layout()
{ title = "Error distribution",
xaxis=new XPlot.Plotly.Graph.Xaxis() { title="Error name" },
yaxis = new XPlot.Plotly.Graph.Yaxis() { title = "Error Count" } };
//put layout into chart
chart.WithLayout(layout);

display(chart)


## Maintenance

The Maintenance is the next PrM component which tells us about scheduled and unscheduled maintenance. The maintenance contains the records which correspond to both regular inspection of components as well as failures. To add the record into the maintenance table a component must be replaced during the scheduled inspection or replaced due to a breakdown. In case the records are created due to breakdowns are called failures. Maintenance contains the data from 2014 and 2015 years.

maint.Head()


## Machines

The data include information about 100 machines which are subject of the Predictive Maintenance analysis. The information includes: model type, and machine age. Distribution of the machine age categorized by the models across production process is shown in the following image:

//Distribution of models across age
var d1 = machines.Filter("model", "model1", FilterOperator.Equal)["age"]
.GroupBy(g => g).Select(g=>(g.Key,g.Count()));
var d2 = machines.Filter("model", "model2", FilterOperator.Equal)["age"]
.GroupBy(g => g).Select(g=>(g.Key,g.Count()));
var d3 = machines.Filter("model", "model3", FilterOperator.Equal)["age"]
.GroupBy(g => g).Select(g=>(g.Key,g.Count()));
var d4 = machines.Filter("model", "model4", FilterOperator.Equal)["age"]
.GroupBy(g => g).Select(g=>(g.Key,g.Count()));
//define bars
var b1 = new Graph.Bar(){ x = d1.Select(x=>x.Item1),y = d1.Select(x=>x.Item2),name = "model1"};
var b2 = new Graph.Bar(){ x = d2.Select(x=>x.Item1),y = d2.Select(x=>x.Item2),name = "model2"};
var b3 = new Graph.Bar(){ x = d3.Select(x=>x.Item1),y = d3.Select(x=>x.Item2),name = "model3"};
var b4 = new Graph.Bar(){ x = d4.Select(x=>x.Item1),y = d4.Select(x=>x.Item2),name = "model4"};

//Plot machine data
var chart = Chart.Plot(new[] {b1,b2,b3,b4});
var layout = new XPlot.Plotly.Layout.Layout()
{ title = "Components Replacements",barmode="stack",
xaxis=new XPlot.Plotly.Graph.Xaxis() { title="Machine Age" },
yaxis = new XPlot.Plotly.Graph.Yaxis() { title = "Count" } };
//put layout into chart
chart.WithLayout(layout);

display(chart)


## Failures

The Failures data represent the replacements of the components due to the failure of the machines. Once the failure is happened the machine is stopped. This is a crucial difference between Errors and Failures.

failures.Head()


//count number of failures
var falValues = failures["failure"].GroupBy(v => v)
.OrderBy(group => group.Key)
.Select(group => Tuple.Create(group.Key, group.Count()));

//Plot Failure data
var chart = Chart.Plot(
new Graph.Bar()
{
x = falValues.Select(x=>x.Item1),
y = falValues.Select(x=>x.Item2),
//  mode = "markers",
}

);
var layout = new XPlot.Plotly.Layout.Layout()
{ title = "Failure Distribution across machines",
xaxis=new XPlot.Plotly.Graph.Xaxis() { title="Component Name" },
yaxis = new XPlot.Plotly.Graph.Yaxis() { title = "Number of components replaces" } };
//put layout into chart
chart.WithLayout(layout);

display(chart)


## Feature Engineering

This section contains several feature engineering methods used to create features based on the machines’ properties.

### Lagged Telemetry Features

First, we are going to create several lagged telemetry data, since telemetry data are classic time series data.

In the following, the rolling mean and standard deviation of the telemetry data over the last 3-hours lag window is calculated for every 3 hours.

//prepare rolling aggregation for each column for average values
var agg_curent = new Dictionary<string, Aggregation>()
{
{ "datetime", Aggregation.Last }, { "volt", Aggregation.Last }, { "rotate", Aggregation.Last },
{ "pressure", Aggregation.Last },{ "vibration", Aggregation.Last }
};
//prepare rolling aggregation for each column for average values
var agg_mean = new Dictionary<string, Aggregation>()
{
{ "datetime", Aggregation.Last }, { "volt", Aggregation.Avg }, { "rotate", Aggregation.Avg },
{ "pressure", Aggregation.Avg },{ "vibration", Aggregation.Avg }
};
//prepare rolling aggregation for each column for std values
var agg_std = new Dictionary<string, Aggregation>()
{
{ "datetime", Aggregation.Last }, { "volt", Aggregation.Std }, { "rotate", Aggregation.Std },
{ "pressure", Aggregation.Std },{ "vibration", Aggregation.Std }
};

//group Telemetry data by machine ID
var groupedTelemetry = telemetry.GroupBy("machineID");

//calculate rolling mean for grouped data for each 3 hours
var _3AvgValue = groupedTelemetry.Rolling(3, 3, agg_mean)
.Create(("machineID", null), ("datetime", null),("volt", "voltmean_3hrs"), ("rotate", "rotatemean_3hrs"),
("pressure", "pressuremean_3hrs"), ("vibration", "vibrationmean_3hrs"));
//show head of the newely generated table


//calculate rolling std for grouped datat fro each 3 hours
var _3StdValue = groupedTelemetry.Rolling(3, 3, agg_mean)
.Create(("machineID", null), ("datetime", null),("volt", "voltsd_3hrs"), ("rotate", "rotatesd_3hrs"),
("pressure", "pressuresd_3hrs"), ("vibration", "vibrationsd_3hrs"));
//show head of the new generated table


For capturing a longer term effect 24 hours lag features we are going to calculate rolling avg and std.

//calculate rolling avg and std for each 24 hours
var _24AvgValue = groupedTelemetry.Rolling(24, 3, agg_mean)
.Create(("machineID", null), ("datetime", null),
("volt", "voltmean_24hrs"), ("rotate", "rotatemean_24hrs"),
("pressure", "pressuremean_24hrs"), ("vibration", "vibrationmean_24hrs"));
var _24StdValue = groupedTelemetry.Rolling(24, 3, agg_std)
.Create(("machineID", null), ("datetime", null),
("volt", "voltsd_24hrs"), ("rotate", "rotatesd_24hrs"),
("pressure", "pressuresd_24hrs"), ("vibration", "vibrationsd_24hrs"));


### Merging telemetry features

Once we have rolling lag features calculated, we can merge them into one data frame:

//before merge all features create set of features from the current values for every 3 or 24 hours
DataFrame _1CurrentValue = groupedTelemetry.Rolling(3, 3, agg_curent)
.Create(("machineID", null), ("datetime", null),
("volt", null), ("rotate", null), ("pressure", null), ("vibration", null));


Now that we have basic data frame merge previously calculated data frames with this one.

//merge all telemetry data frames into one
var mergeCols= new string[] { "machineID", "datetime" };
var df1 = _1CurrentValue.Merge(_3AvgValue, mergeCols, mergeCols, JoinType.Left, suffix: "df1");

var df2 = df1.Merge(_24AvgValue, mergeCols, mergeCols, JoinType.Left, suffix: "df2");

var df3 = df2.Merge(_3StdValue, mergeCols, mergeCols, JoinType.Left, suffix: "df3");

var df4 = df3.Merge(_24StdValue, mergeCols, mergeCols, JoinType.Left, suffix: "df4");


At the end of the merging process, select relevant columns.

//select final dataset for the telemetry
var telDF = df4["machineID","datetime","volt","rotate", "pressure", "vibration",
"voltmean_3hrs","rotatemean_3hrs","pressuremean_3hrs","vibrationmean_3hrs",
"voltmean_24hrs","rotatemean_24hrs","pressuremean_24hrs","vibrationmean_24hrs",
"voltsd_3hrs", "rotatesd_3hrs","pressuresd_3hrs","vibrationsd_3hrs",
"voltsd_24hrs", "rotatesd_24hrs","pressuresd_24hrs","vibrationsd_24hrs"];

//remove NANs
var telemetry_final = telDF.DropNA();


Now top 5 rows of final telemetry data looks like the following image:

telemetry_final.Head()


## Lag Features from Errors

Unlike telemetry that had numerical values, errors have categorical values denoting the type of error that occurred at a time-stamp. We are going to aggregate categories of the error with different types of errors that occurred in the lag window.

First, encode the errors with One-Hot-Encoding:

var mlContext = new MLContext(seed:2019);
//One Hot Encoding of error column
var encodedErr = errors.EncodeColumn(mlContext, "errorID");

//sum duplicated errors by machine and date
var errors_aggs = new Dictionary<string, Aggregation>();

//group and sum duplicated errors
encodedErr =  encodedErr.GroupBy(new string[] { "machineID", "datetime" }).Aggregate(errors_aggs);

//
encodedErr = encodedErr.Create(("machineID", null), ("datetime", null),
("error1", "error1sum"), ("error2", "error2sum"),
("error3", "error3sum"), ("error4", "error4sum"), ("error5", "error5sum"));


// align errors with telemetry datetime values so that we can calculate aggregations
var er = telemetry.Merge(encodedErr,mergeCols, mergeCols, JoinType.Left, suffix: "error");
//
er = er["machineID","datetime", "error1sum", "error2sum", "error3sum", "error4sum", "error5sum"];
//fill missing values with 0
er.FillNA(0);


//count the number of errors of different types in the last 24 hours, for every 3 hours
//define aggregation
var errors_aggs1 = new Dictionary<string, Aggregation>()
{
{ "datetime", Aggregation.Last },{ "error1sum", Aggregation.Sum }, { "error2sum", Aggregation.Sum },
{ "error3sum", Aggregation.Sum },{ "error4sum", Aggregation.Sum },
{ "error5sum", Aggregation.Sum }
};

//count the number of errors of different types in the last 24 hours,  for every 3 hours
var eDF = er.GroupBy(new string[] { "machineID"}).Rolling(24, 3, errors_aggs1);

//
var newdf=  eDF.DropNA();

var errors_final = newdf.Create(("machineID", null), ("datetime", null),
("error1sum", "error1count"), ("error2sum", "error2count"),
("error3sum", "error3count"), ("error4sum", "error4count"), ("error5sum", "error5count"));


## The Time Since Last Replacement

As the main task here is how to create a relevant feature in order to create a quality data set for the machine learning part. One of the good features would be the number of replacements of each component in the last 3 months to incorporate the frequency of replacements.

Furthermore, we can calculate how long it has been since a component is last replaced as that would be expected to correlate better with component failures since the longer a component is used, the more degradation should be expected. As first we are going to encode the maintenance table:

//One Hot Encoding of error column
var encMaint = maint.EncodeColumn(mlContext, "comp");


//create separate data frames in order to calculate proper time since last replacement
DataFrame dfComp1 = encMaint.Filter("comp1", 1, FilterOperator.Equal)["machineID", "datetime"];
DataFrame dfComp2 = encMaint.Filter("comp2", 1, FilterOperator.Equal)["machineID", "datetime"];;
DataFrame dfComp3 = encMaint.Filter("comp3", 1, FilterOperator.Equal)["machineID", "datetime"];;
DataFrame dfComp4 = encMaint.Filter("comp4", 1, FilterOperator.Equal)["machineID", "datetime"];;



//from telemetry data create helped data frame so we can calculate additional column from the maintenance data frame
var compData = telemetry_final.Create(("machineID", null), ("datetime", null));

%%time
//calculate new set of columns so that we have information the time since last replacement of each component separately
var newCols= new string[]{"sincelastcomp1","sincelastcomp2","sincelastcomp3","sincelastcomp4"};
var calcValues= new object[4];

//perform calculation
{
var machineId = Convert.ToInt32(row["machineID"]);
var date = Convert.ToDateTime(row["datetime"]);

var maxDate1 = dfComp1.Filter("machineID", machineId, FilterOperator.Equal)["datetime"]
.Where(x => (DateTime)x <= date).Select(x=>(DateTime)x).Max();
var maxDate2 = dfComp2.Filter("machineID", machineId, FilterOperator.Equal)["datetime"]
.Where(x => (DateTime)x <= date).Select(x=>(DateTime)x).Max();
var maxDate3 = dfComp3.Filter("machineID", machineId, FilterOperator.Equal)["datetime"]
.Where(x => (DateTime)x <= date).Select(x=>(DateTime)x).Max();
var maxDate4 = dfComp4.Filter("machineID", machineId, FilterOperator.Equal)["datetime"]
.Where(x => (DateTime)x <= date).Select(x=>(DateTime)x).Max();

//perform calculation
calcValues[0] = (date - maxDate1).TotalDays;
calcValues[1] = (date - maxDate2).TotalDays;
calcValues[2] = (date - maxDate3).TotalDays;
calcValues[3] = (date - maxDate4).TotalDays;
return calcValues;
});

Wall time: 178708.9764ms


var maintenance_final = compData;


## Machine Features

The machine data set contains descriptive information about machines like the type of machines and their ages which is the years in service.

machines.Head()


## Joining features into final ML ready data set

As the last step in Feature engineering, we are performing merging all features into one data set.

var merge2Cols=new string[]{"machineID"};
var fdf1= telemetry_final.Merge(errors_final, mergeCols, mergeCols,JoinType.Left, suffix: "er");
var fdf2 = fdf1.Merge(maintenance_final, mergeCols,mergeCols,JoinType.Left, suffix: "mn");
var features_final = fdf2.Merge(machines, merge2Cols,merge2Cols,JoinType.Left, suffix: "ma");

features_final= features_final["datetime", "machineID",
"voltmean_3hrs", "rotatemean_3hrs", "pressuremean_3hrs", "vibrationmean_3hrs",
"voltstd_3hrs", "rotatestd_3hrs", "pressurestd_3hrs", "vibrationstd_3hrs",
"voltmean_24hrs", "rotatemean_24hrs", "pressuremean_24hrs", "vibrationmean_24hrs",
"voltstd_24hrs","rotatestd_24hrs", "pressurestd_24hrs", "vibrationstd_24hrs",
"error1count", "error2count", "error3count", "error4count", "error5count",
"sincelastcomp1", "sincelastcomp2", "sincelastcomp3", "sincelastcomp4",
"model", "age"];
//

DataFrame.ToCsv("data/final_features.csv", features_final);


# Define Label Column

The Label in prediction maintenance should be the probability that a machine will fail in the near future due to a failure certain component. If we take 24 hours to be a task for this problem, the label construction is consists of a new column in the feature data set which indicate if certain machine will fail or not in the next 24 hours due to failure one of several components.

With this way we are defining the label as a categorical variable containing: – none – if the machine will not fail in the next 24 hours, – comp1 to comp4

• if the machine will fail in the next 24 hours due to the failure of certain components.

Since we can experiment with the label construction by applying different conditions, we can implement methods that take several arguments in order to define the general problem.

failures.Describe(false)


//constructing the label column which indicate if the current machine will
//fail in the next predTime (24 hours as default) due to failur certain component.
//create final data frame from feature df
var finalDf = new DataFrame(features_final);

//group failures by machineID and datetime
string[] cols = new string[] {  "machineID" , "datetime"};
var failDfgrp = failures.GroupBy(cols);

var rV = new object[] { "none" };
finalDf.AddCalculatedColumns(new string[]{"failure"}, (object[] row, int i) => rV);

//create new data frame from featuresDF by grouping machineID and datatime
var featureDfGrouped = finalDf["datetime","machineID", "failure"].GroupBy(cols);

//now look for every failure and calculate if the machine will fail in the last 24 hours
//in case two or more components were failed for the ssame machine add new row in df
var failureDfExt = featureDfGrouped.Transform((xdf) =>
{
//extract the row from featureDfGrouped
var xdfRow = xdf[0].ToList();
var refDate = (DateTime)xdfRow[0];
var machineID = (int)xdfRow[1];

//now look if the failure contains the machineID
if(failDfgrp.Group2.ContainsKey(machineID))
{
//get the date and calculate total hours
var dff = failDfgrp.Group2[machineID];

foreach (var dfff in dff)
{
for (int i = 0; i < dfff.Value.RowCount(); i++)
{
//"datetime","machineID","failure"
var frow = dfff.Value[i].ToList();
var dft = (DateTime)frow[0];

//if total hours is less or equal than 24 hours set component to the failure column
var totHours = (dft - refDate).TotalHours;
if (totHours <= 24 && totHours >=0)
{
if (xdf.RowCount() > i)
xdf["failure", i] = frow[2];
else//in case two components were failed for the same machine and
//at the same time, add new row with new component name
{
var r = xdf[0].ToList();
r[2] = frow[2];
}
}
}
}
}
return xdf;
});

//Now merge extended failure Df with featureDF
var final_dataframe = finalDf.Merge(failureDfExt, cols, cols,JoinType.Left, "fail");

//define final set of columns
final_dataframe = final_dataframe["datetime", "machineID",
"voltmean_3hrs", "rotatemean_3hrs", "pressuremean_3hrs", "vibrationmean_3hrs",
"voltsd_3hrs", "rotatesd_3hrs", "pressuresd_3hrs", "vibrationsd_3hrs",
"voltmean_24hrs", "rotatemean_24hrs", "pressuremean_24hrs", "vibrationmean_24hrs",
"voltsd_24hrs", "rotatesd_24hrs", "pressuresd_24hrs", "vibrationsd_24hrs",
"error1count", "error2count", "error3count", "error4count", "error5count",
"sincelastcomp1", "sincelastcomp2", "sincelastcomp3", "sincelastcomp4",
"model", "age", "failure_fail"];

//rename column
final_dataframe.Rename(("failure_fail", "failure"));

//save the file data frame to disk
DataFrame.ToCsv("data/final_dataFrame.csv",final_dataframe);


### Final Data Frame

Lets see how the final_dataframe looks like. It contains 24 columns. Most of the columns are numerical. The Model column is categorical and it should be encoded once we prepare the machine learning part.

Also the label column failure is categorical column containing 5 different categories: none, comp1, comp2, comp3 and comp4. We can also see the data set is not balance, since we have 2785705 none and the rest of the rows in total of 5923 other categories. This is typical unbalanced dataset, and we should be careful when evaluation models, because the model which returns always none value will have more than 97% of accuracy.

final_dataframe.Describe(false)


In the next part, we are going to implement the training and evaluation process of the Predictive Maintenance model. The full notebook for this blog post can be found here

# What is .NET Jupyter Notebook

In this blog post, we are going to explore the main features in the new C# Juypter Notebook. For those who used Notebook from other programming languages like Python or R, this would be an easy task. First of all, the Notebook concept provides a quick, simple and straightforward way to present a mix of text and $\Latex$, source code implementation and its output. This means you have a full-featured platform to write a paper or blog post, presentation slides, lecture notes, and other educated materials.

The notebook consists of cells, where a user can write code or markdown text. Once he completes the cell content confirmation for cell editing can be achieved by Ctrl+Enter or by press run button from the notebook toolbar. The image below shows the notebook toolbar, with a run button. The popup combo box shows the type of cell the user can define. In the case of text, Markdown should be selected, for writing source code the cell should be Code.

To start writing code to C# Notebook, the first thing we should do is install NuGet packages or add assembly references and define using statements. In order to do that, the following code installs several nuget packages, and declare several using statements. But before writing code, we should add a new cell by pressing + toolbar button.

The first few NuGet packages are packages for ML.NET. Then we install the XPlot package for data visualization in .NET Notebook, and then we install a set of Daany packages for data analytics. First, we install Daany.DataFrame for data exploration and analysis, and then Daany.DataFrame.Ext set of extensions for data manipulation used with ML.NET.

//ML.NET Packages
#r "nuget:Microsoft.ML.LightGBM"
#r "nuget:Microsoft.ML"
#r "nuget:Microsoft.ML.DataView"

//Install XPlot package
#r "nuget:XPlot.Plotly"

//Install Daany.DataFrame
#r "nuget:Daany.DataFrame"
#r "nuget:Daany.DataFrame.Ext"
using System;
using System.Linq;

//Daany data frame
using Daany;
using Daany.Ext;

//Plotting functionalities
using XPlot.Plotly;

//ML.NET using
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers.LightGbm;


The output for the above code:

Once the NuGet packages are installed successfully, we can start with data exploration. But before this declare few using statements:

We can define classes or methods globally. The following code implements the formatter method for displaying Daany.DataFrame in the output cell.

// Temporal DataFrame formatter for this early preview
using Microsoft.AspNetCore.Html;
Formatter<DataFrame>.Register((df, writer) =>
{

//renders the rows
var rows = new List<List<IHtmlContent>>();
var take = 20;

//
for (var i = 0; i < Math.Min(take, df.RowCount()); i++)
{
var cells = new List<IHtmlContent>();
foreach (var obj in df[i])
{
}
}

var t = table(
tbody(
rows.Select(
r => tr(r))));

writer.Write(t);
}, "text/html");


For this demo we will used famous Iris data set. We will download the file from the internet, load it by using Daany.DataFrame, a display few first rows. In order to do that we run the folloing code:

var url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data";
var cols = new string[] {"sepal_length","sepal_width", "petal_length", "petal_width", "flower_type"};
var df = DataFrame.FromWeb(url, sep:',',names:cols);


The output looks like this:

As can be seen, the last line from the previous code has no semicolon, which means the line should be displayed in the output cell. Let’s move on, and implement two new columns. The new columns will be sepal and petal area for the flower. The expression we are going to use is:

$$PetalArea = petal_width \cdot petal_length;\ SepalArea = sepal_width \cdot sepal_length;$$

As can be seen, the $\LaTeX$ is fully supported in the notebook.

The above formulea is implemented in the following code:

//calculate two new columns into dataset
df.AddCalculatedColumn("SepalArea", (r, i) => Convert.ToSingle(r["sepal_width"]) * Convert.ToSingle(r["sepal_length"]));
df.AddCalculatedColumn("PetalArea", (r, i) => Convert.ToSingle(r["petal_width"]) * Convert.ToSingle(r["petal_length"]));


The data frame has two new columns. They indicate the area for the flower. In order to see basic statistics parameters for each of the defined columns, we call Describe method.

//see descriptive stats of the final ds
df.Describe(false)


From the table above, we can see the flower column has only 3 values. The most frequent value has a frequency equal to 50, which is an indicator of a balanced dataset.

# Data visualization

The most powerful feature in Notebook is a data visualization. In this section, we are going to plot some interesting charts.

In order to see how sepal and petal areas are spread in 2D plane, the following plot is implemented:

//plot the data in order to see how areas are spread in the 2d plane
//XPlot Histogram reference: http://tpetricek.github.io/XPlot/reference/xplot-plotly-graph-histogram.html

var faresHistogram = Chart.Plot(new Graph.Histogram(){x = df["flower_type"], autobinx = false, nbinsx = 20});
var layout = new Layout.Layout(){title="Distribution of iris flower"};
faresHistogram.WithLayout(layout);
display(faresHistogram);


The chart is also an indication of a balanced dataset.

Now lets plot areas depending on the flower type:

// Plot Sepal vs. Petal area with flower type

var chart = Chart.Plot(
new Graph.Scatter()
{
x = df["SepalArea"],
y = df["PetalArea"],
mode = "markers",
marker = new Graph.Marker()
{
color = df["flower_type"].Select(x=>x.ToString()=="Iris-virginica"?1:(x.ToString()=="Iris-versicolor"?2:3)),
colorscale = "Jet"
}
}
);

var layout = new Layout.Layout(){title="Plot Sepal vs. Petal Area & color scale on flower type"};
chart.WithLayout(layout);
chart.WithLegend(true);
chart.WithLabels(new string[3]{"Iris-virginica","Iris-versicolor", "Iris-setosa"});
chart.WithXTitle("Sepal Area");
chart.WithYTitle("Petal Area");
chart.Width = 800;
chart.Height = 400;

display(chart);


As can be seen from the chart above, flower types are separated almost linearly, since we used petal and sepal areas instead of width and length. With this transformation, we can get a 100% accurate ml model.

# Machine Learning

Once we finished with data transformation and visualization we can define the final data frame before machine learning application. To end this we are going to select only two columns for features and one label column which will be flower type.

//create new data-frame by selecting only three columns
var derivedDF = df["SepalArea","PetalArea","flower_type"];


Since we are going to use ML.NET, we need to declare Iris in order to load the data into ML.NET.

//Define an Iris class for machine learning.
class Iris
{
public float PetalArea { get; set; }
public float SepalArea { get; set; }
public string Species { get; set; }
}
//Create ML COntext
MLContext mlContext = new MLContext(seed:2019);


Then load the data from Daany data frame into ML.NET:

//Load Data Frame into Ml.NET data pipeline
{
//convert row object array into Iris row

var prRow = new Iris();
prRow.SepalArea = Convert.ToSingle(oRow["SepalArea"]);
prRow.PetalArea = Convert.ToSingle(oRow["PetalArea"]);
prRow.Species = Convert.ToString(oRow["flower_type"]);
//
return prRow;
}));


Once we have data, we can split it into train and test sets:

//Split dataset in two parts: TrainingDataset (80%) and TestDataset (20%)
var trainTestData = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
var trainData = trainTestData.TrainSet;
var testData = trainTestData.TestSet;


The next step in prepare the data for training is define pipeline for dtaa transformation and feature engineering:

//one encoding output category column by defining KeyValues for each category
IEstimator<ITransformer> dataPipeline =
mlContext.Transforms.Conversion.MapValueToKey(outputColumnName: "Label", inputColumnName: nameof(Iris.Species))

//define features columns
.Append(mlContext.Transforms.Concatenate("Features",nameof(Iris.SepalArea), nameof(Iris.PetalArea)));


Once we completes the preparation paert, we can perform the training part. The training start by calling Fit to the pipeline:

%%time
// Define LightGbm algorithm estimator
IEstimator<ITransformer> lightGbm = mlContext.MulticlassClassification.Trainers.LightGbm();
//train the ML model
TransformerChain<ITransformer> model = dataPipeline.Append(lightGbm).Fit(trainData);


Once the training is completes, we have trained model which can be evaluated. In order to print the evaluation result with formatting, we are going to install Daany DataFrame extension which has implementation of printing results


//evaluate train set
var predictions = model.Transform(trainData);
var metricsTrain = mlContext.MulticlassClassification.Evaluate(predictions);
ConsoleHelper.PrintMultiClassClassificationMetrics("TRAIN Iris DataSet", metricsTrain);
ConsoleHelper.ConsoleWriteHeader("Train Iris DataSet Confusion Matrix ");
ConsoleHelper.ConsolePrintConfusionMatrix(metricsTrain.ConfusionMatrix);


//evaluate test set
var testPrediction = model.Transform(testData);
var metricsTest = mlContext.MulticlassClassification.Evaluate(testPrediction);
ConsoleHelper.PrintMultiClassClassificationMetrics("TEST Iris Dataset", metricsTest);
ConsoleHelper.ConsoleWriteHeader("Test Iris DataSet Confusion Matrix ");
ConsoleHelper.ConsolePrintConfusionMatrix(metricsTest.ConfusionMatrix);


As can be seen, we have a 100% accurate model for Iris flower recognition. Now, let’s add a new column into the data frame called Prediction to have a model prediction in the data frame.
In order to do that, we are evaluating the model on the train and the test data set. Once we have a prediction for both sets, we can join them and add as a separate column in Daany data frame. The following code does exactly what we described previously.

var flowerLabels = DataFrameExt.GetLabels(predictions.Schema).ToList();
var p1 = predictions.GetColumn<uint>("PredictedLabel").Select(x=>(int)x).ToList();
var p2 = testPrediction.GetColumn<uint>("PredictedLabel").Select(x => (int)x).ToList();
//join train and test

The output above shows the first few rows in the data frame. To see the few last rows from the data frame we call a Tail method.
dff.Tail()