Classification in .NET

Classifcation is one of the simplest methods of machine learning, but can be very useful in some scenarios. Fortunetly .NET has very good implementation of it, which is part of Accord.NET framework. There are couple of different algorithms that you can choose from. In this example I will use simple Naive Bayes algorithm.

1. Create learner

var bayes = new NaiveBayesLearning<NormalDistribution>();

2. Let it learn from input / output values

var inputs = new double[]
{
    new double[] { 1.3, 2.2, 3.1 },
    new double[] { 2.3, 3.2, 4.1 }
};
var output = new int [] { 1, 2 };
var nb = teacher.Learn(inputs, outputs);

Learn method accepts many different configuration of values. Here are all possible inputs:

public virtual TModel Learn(double[][] x, int[] y, double[] weight = null);
public virtual TModel Learn(double[][] x, int[][] y, double[] weight = null);
public virtual TModel Learn(double[][] x, double[][] y, double[] weight = null);

3. Then you can classify values using Decide method:

var answers = nb.Decide(inputs);

I created simple .NET Core Web API to show how one could use Accord.NET classification in web .NET project. You can find it in this Github repo. It is simple example, but I hope that it will give you idea how could you use it.

For more classification algorithms in Accord.NET and details go to documentation.

Top wordpress performance

I was recently complemented on fast page loads on this blog and asked how I achieved it. Fortunately great performance on wordpress blog can be achieved without much hustle and for free. Here is what I use:

  1. Latest PHP runtime with enabled OP Cache.
  2. Nginx web server as reverse proxy.
  3. HTTPS through free TLS certificate from Let’s encrypt.
  4. Cloudflare, especially those settings
    Rocket Loaderautomatic
    Brotlion
    HSTSon
    Browser Cache Expiration8 days
    Autominifycss, js
  5. Autoptimize, Cache Enabler, WP Smush and WP-Optimize wordpress plugins.

I also use GTmetrix performance score for assessing results of all those optimzations.

ASP.NET Core is the best web framework

I started developing web applications more than five years ago with .NET Framework MVC. Back then in version 4 with latest C# 5 language. At that time when I compared it to competing web frameworks I saw many downsides to it, but also couple of strong points:

1. Visual Studio is the best IDE in the world and it is especially suited for .NET development.
2. C# is great language for web development, it is staticaly typed, very well designed and is very readable. It is much better than JavaScript or Python and even Java which has some downsides compared to it e.g. worse generic types implementation.
3. .NET Framework was often used with another Microsoft product – SQL Server, which is very good SQL database with great tools.

Downsides that I saw at the time:

1. Not open source
2. Runs only on windows
3. Not perfect design of web framework
4. Average performance

But with .NET Core situation change dramaticaly. All downsides mentioned above disappeared. First version of .NET Core had couple of flaws, but version two fixed them and result is magnificent. C# language also was hugely improved in those couple of years. Here are categories that I use to compare web frameworks and my asessment of .NET Core:

IDEtop
Languagetop
Open sourceyes
Runs on Linuxyes
Quality of web frameworktop
Performancetop
Librariesvery good
Community supportvery good

Those are reasons for my belief that .NET Core is currently best web framework in the world. Microsoft did magnificent job. I think that people around the world start to recognize it and it mean’s that those two last categories soon will change from very good to top.

Linear regression in .NET

Linear regression is quite simple method of machine learning, but it is very useful in some scenarios. Fortunetly .NET has very good implementation of it, which is part of Accord.NET framework. Here is simple example how to use it for two variables.

1. Create algorithm wrapper

var ols = new OrdinaryLeastSquares();

2. Let it learn relation between input and output variables

double[] inputs = { 80, 60, 10, 20, 30 };
double[] outputs = { 20, 40, 30, 50, 60 };
var regression = ols.Learn(inputs, outputs);

3. Now you can use it in order to get estimated value corresponding to input

var y = regression.Transform(85);

I created simple .NET Core 2 API project that shows how one could integrate linear regression into it. In this repository you can check how I have done that.

For more complex scenarios, where each input value has multiple output values or multiple input values correspond to multiple output values, you should use different Learn implementations. Here is part of OrdinaryLeastSquares class, that lists all Learn method implementations:

public SimpleLinearRegression Learn(double[] x, double[] y, double[] weights = null);
public MultipleLinearRegression Learn(double[][] x, double[] y, double[] weights = null);
public MultivariateLinearRegression Learn(double[][] x, double[][] y, double[] weights = null);

For more details and configuration options go to documentation.

Dijkstra algorithm in .NET

Dijkstra algorithm is used for finding shortest path in graph between two vertices. I won’t go into details, you can find them on it’s wikipedia page.

Fortunately in .NET there exists couple of implementations of this algorithm. I tried some of them and I liked QuickGrap version best. Here is how one could use it in .NET projects.

1. Create graph, add vertices and edges to it

var graph = new AdjacencyGraph<string, Edge<string>>(allowParallelEdges: false);
graph.AddVertex("A");
graph.AddVertex("B");
var AB_Edge = new Edge<string>("A", "B");
graph.AddEdge(AB_Edge);

2. Determine edge costs

var edgeCosts = new Dictionary<Edge<string>, double>();
var AB_Weight = 10;
edgeCosts.Add(AB_Edge, AB_Weight);

3. Now you can mix those two

var dijkstra = new DijkstraShortestPathAlgorithm<string, Edge<string>>(graph, e => edgeCosts[e]);

4. Then you can find shortest path length between vertecies

dijkstra.Compute("A");
if (dijkstra.TryGetDistance("B", out double distance)) 
{  
    // use distance variable
}

5. In order to determine edges of shortest path, you have to create observer and attach it to algorithm

var observer = new VertexPredecessorRecorderObserver<string, Edge<string>>();
using (observer.Attach(dijkstra)) dijkstra.Compute("A"); // 'A' is root vertex
 
if (observer.TryGetPath("B", out IEnumerable<Edge<string>> path))
{
    // use path variable
}

Note that methods for getting distance and path are written using TryXXX pattern. Because of that they return bool value that indicates if there is path between given vertecies. Proper output of method is returned in out variable.

I have created .NET Core 2 API project that uses QuickGraph implementation of Dijkstra algorithm, where you can find how you could go about using it in your own project. Remember that it is just simple example and in real project you would have to combine it with DB data source. But it should be good place to start. You can find it in this Github repo.

Buddhism can’t be reconciled with science

There are different types of Buddhism, but they share ideological background. Probably the best – most clear and concise – formulation of basic Buddhist beliefs are Four Noble Truths. On this wikipedia page I found very good summary of Four Noble Truths:

The Four Noble Truths refer to and express the basic orientation of Buddhism in a short expression: we crave and cling to impermanent states and things, which are dukkha, “incapable of satisfying” and painful. This craving keeps us caught in samsara, the endless cycle of repeated rebirth and dying again, and the dukkha that comes with it. There is, however, a way to end this cycle, namely by attaining nirvana, cessation of craving, whereafter rebirth and associated dukkha will no longer arise again. This can be accomplished by following the eightfold path, restraining oneself, cultivating discipline, and practicing mindfulness and meditation.

I want to highlight sentence endless cycle of repeated rebirth and dying again. Most buddhists interpret it as a process of reincarnation. They believe that death of individual is not end to his life. It is just beginning of his next life, his next incarnation.

Buddhism depends heavily on that dogma. Reason is that if there is no reicarnation, there is no need for buddhist way of life, as described by the eightfold path. If after death of an individual there is no next life, practicing it or not result in same outcome. If there are no next incarnations, most teachings of Buddha are worthless – because they heavily depended on false assumptions.

I argue that it is the case. There is no reincarnation. There is no proof of it and no reason to believe in it from scientific point of view.

First it is very hard to reconcile reincarnation with theory of evolution, which is proven, well established scientific theory. From evolutionary point of view, life on Earth appeared spontaneously around 4 billion years ago. If each new life requires some spirit or soul (however you call it) that needs next incarnation, from where those first organisms get theirs? Were they created or always been there, created in Big Bang?

Second there is no scientific proof for reincarnation. It is not needed to explain any reliable data, any fact. Science can’t prove that reincarnation doesn’t exist, but also it can’t prove that somewhere in the Universe there is planet on which live pink unicorns. But there is same amount of reasons to believe in both of those claims – none. This is why Buddhism can’t be reconciled with science.

Paradoxically Buddhist should be happy about it. Buddha tought people that they should devote their lifes to praciticing the eightfold path, so they won’t reincarnate anymore and won’t be experiencing dukkha for eternity. But if reincarnation doesn’t exist, there is no point in doing that. No one will reincarnate anyway. So realisation of this truth should free Buddhists from a lot of trouble and still their goal will be achieved, with 100% guarantee.

7 questions that every startup must answer

In his great book Zero to One it’s author Peter Thiel included list of seven questions that start-ups should ask themselves before starting. In his opinion, if start-up neglects to answer few of those questions it will probably fail. Here they are:

  1. The Engineering Question – Can you create breakthrough technology instead of incremental improvements?
  2. The Timing Question – Is now the right time to start your particular business?
  3. The Monopoly Question – Are you starting with a big share of a small market?
  4. The People Question – Do you have the right team?
  5. The Distribution Question – Do you have a way to not just create but deliver your product?
  6. The Durability Question – Will your market position be defensible 10 and 20 years into the future?
  7. The Secret Question – Have you identified a unique opportunity that others don’t see?

After listing those questions book explains them in detail. I strongly recommend this book to everyone interested in technology and innovation.

 

You don’t have to spend a lot of money in order to get great stereo system

Many years ago one have to spend at least couple of thousands of dollars in order to get great power amplifier and DAC. But today situation is different, thanks to improvements in technology and cheap labor in China. Recently I upgraded my system with great components, spending just around 350$ including shipping. Key is to know what to look for and buy from Chinese producers (I bought them on Aliexpress).

1. In couple of past years quality of Class D amplifiers went up, so now they can compete with class A and A/B amplifiers in sound quality. Because class D amplifiers consume much less energy than class A and A/B for same power output, they are much cheaper to manufacture. You don’t need big heat sink, huge transformers and big, bulky housing. That decreses manufacturing costs and transport. If you decide to buy simple power amplifier rather than integrated amp where you pay for additional features, you can buy cheap, but very good amplifier.

Great example of such amplifier is NFJ & FXAUDIO FX-502S PRO. I couldn’t find any measurments on the Internet, but it has great build quality and is based on great TPA3250 chip. It is Class D amplifier chip packed with technology by Texas Instruments. Currently it is one of the best Class D chips out there. The best thing is that this amplifier is priced at around 70$. It is extremely cheap for quality that it provides. Plus it is energy-efficient, so it will consume a lot less energy than same power output class A or A/B amplifier.

FXAUDIO FX-502S PRO

2. Technology has progressed also in DAC sector. Currently high quality DAC chips are very cheap, same is true about other analog and digital components needed to create great DAC. If you add to it low cost manufacturing and great engineering in China, you will get excelent DACs for very cheap price.

Great example of such DAC is Topping D50. You can see detaild measurments here, they are great. I don’t mean just great for around 250$ that you have to spend on it, I mean great for any amount money. You can’t buy much better DAC if you would spent twenty times more. It is close to the state-of-the-art. It has USB, Coaxial and Toslink ports which make it compatible with every digital source. It is probably good idea to power it with linear power supply, because it operates on very weak currents, so low quality power supply could have negative impact on it’s performance. You could buy reasonably good Linear Power Supply for around 50$, so for around 300$ you can have great digital to analog conversion in your system. If it is too much Topping D30 is also great choice at around half of the price, also LPS is optional.

Here is graph for THD+N, which is one of the most important factors in digital-audio conversion:

Topping D50 measurments

3. You will save yourself a lot of money if you won’t spend it on expensive CD player. If you have external DAC you will use digital output. So only job of CD player is to read bis from digital disc. Even cheap CD / DVD player will do great job at this. Consider laptop CD reader that you can by for under 30$ – it will read discs bit perfect.

I have chosen BDP-S6700 player, because it reads all type of discs: CD, SACD, DVD and BluRay. You can buy it at around 100$.

Setting up World Community Grid on Ubuntu and Debian

Setting up World Community Grid client on Debian based Linux operating system is pretty easy and only requires access to the Internet. Just run (in order) four commands in command line:

sudo apt-get update
sudo apt-get install boinc-client boinc-manager
sudo -u boinc boinccmd --project_attach http://www.worldcommunitygrid.org [Account Key]

You can find account key on your World Community Grid profile.
If you want to stop processing just run:

sudo service boinc-client stop

You can verify if CPU is computing by running command below. It shows processes currently running and their CPU usage.

top

You should find there process named boinc with heavy load on CPU. But remember to wait a while, because first step for boinc client is to fetch data from WCG servers. It won’t process instantaneously. It may take a while until boinc process will show up on the list.
If after, let’s say, 10 minutes you won’t see heavy load on CPU coming from boinc client, toubleshoot using command below.

boinccmd --get_messages

For example, when one tries to run World Communty Grid on Raspberry Pi 2, WCG servers responds that CPU architecture is not supported.