Saturday, 12 September 2020

ML.NET: Machine Learning for .NET Developers


Machine Learning in .Net

ML.NET is a free software machine learning library for the C# and F# programming languages. It also supports Python models when used together with NimbusML. The preview release of ML.NET included transforms for feature engineering like n-gram creation, and learners to handle binary classification, multi-class classification, and regression tasks. Additional ML tasks like anomaly detection and recommendation systems have since been added, and other approaches like deep learning will be included in future versions.
ML.NET brings model-based Machine Learning analytic and prediction capabilities to existing .NET developers. The framework is built upon .NET Core and .NET Standard inheriting the ability to run cross-platform on Linux, Windows and macOS.
Developers can train a Machine Learning Model or reuse an existing Model by a 3rd party and run it on any environment offline. This means developers do not need to have a background in Data Science to use the framework. 

Build for .Net Developers

    With ML.Net, you can use your existing skills to easily integrate ML into your existing .Net applications without any prior experience.

ML.Net Performance

    Microsoft's paper on machine learning with ML.NET demonstrated it is capable of training sentiment analysis models using large datasets while achieving high accuracy. Its results showed 95% accuracy on Amazon's 9GB review dataset.

NimbusML Python support

    Microsoft acknowledged that the Python programming language is popular with Data Scientists, so it has introduced NimbusML the experimental Python bindings for ML.NET. This enables users to train and use machine learning models in Python. It was made open source similar to Infer.NET.

ML.Net capabilities

  • Sentiment Analysis - Analyse sentiment of customer review using binary classification algorithm.
  • Product recommendation - Recommends products based on purchase history using a matrix factorization algorithm.
  • Price prediction - Predict taxi fare based on parameters such as distance traveled using regression algorithm.
  • Customer segmentation - Identify group of customers with similar profiles using clustering algorithm.
  • Object detection - Recognize objects in an image using an ONNX deep learning model.
  • Fraud detection - Detect fraudulent credit card transaction using a binary classification algorithm.
  • Sales spike detection -  Detect spikes and changes in product sales using an anomaly detection model.
  • Image classification - Classify images using TensorFlow deep learning model.
  • Sales forecasting - Forecast further sales for product using a regression algorithm. 

Reference: ML.Net, wiki

Tuesday, 11 February 2020

How to upgrade to Angular 9


Update your existing project to the new version of Angular now easily

For step-by-step instructions on how to update to the latest Angular release, use the interactive update guide at update.angular.io

If your application uses the CLI, you can update to version 9 automatically with the help of the ng update script

Steps to upgrade to Angular 9

1. Identify the existing version of angular, in my case it was Angular 8.
Angular command  $ ng version



2. Upgrade Angular CLI
Angular command $ ng update @angular/cli @angular/core --next  



         Now your Angular CLI and Angular Project is upgraded to Angular 9 successfully.. As Angular 9 is in RC period, the flag --next is required while using ng update command. This flag is not required, once final version of Angular 9 is released.The above commands will run a series of small migrations that will convert the code of your application to be compatible with version 9 as shown in above image.

Angular 9 New Features and Ivy

Angular Version 9 was released recently in beta (RC). A release candidate (RC) is a beta version with potential to be a stable product, which is ready to release unless significant bugs emerge. In this article, we shall take an overview of the new features in the version 9 changes and updates available for use now.


Type Script 3.7.x Support

 ↪  Angular 9 supports the Typescript 3.7 or above version. So, if we want to use Angular 9 for our application, then we need to first upgrade the Typescript version to 3.7 or above.


Default & New Ivy Rendering Engine
 ↪  

Smaller Bundle Size

Dependency Injection Changes in Core

Need for Faster Mobile Apps

Angular Core Type-Safe Changes

Modules with Providers Support

Changes in Angular Forms

i18n Improvements

Service Worker Updates

Sunday, 16 April 2017

Web API Token Based Authentication


ASP.NET Web API can be accessed over Http by any client using the Http protocol. This framework enables data communication in JSON format (by default) and hence helps in lightweight communication.

Token based authentication
Since the Web API adoption is increasing at a rapid pace, there is a serious need for implementing security for all types of clients trying to access data from Web API services. One of the most preferred mechanism is to authenticate client over HTTP using a signed token. Simply put, a token is a piece of data which is created by a server, and which contains enough data to identify a particular user. The process starts by allowing users to enter their username and password which accessing a service. Once the user provides the username/password, a token is issued which allows users to fetch a specific resource - without using their username and password every time. This token is sent to the server with each request made by the client and contains all necessary information to validate a user’s request. The following diagram explains how Token-Based authentication is used in communication between clients and server.
Token based authentication works by ensuring that each request to a server is accompanied by a signed token which the server verifies for authenticity and only then responds to the request.



Why use Tokens based authentication?

  1. Tokens are stateless - The token is self-contained and contains all the information it needs for authentication. This is great for scalability as it frees your server from having to store session state.
  2. Tokens can be generated from anywhere -  Token generation is decoupled from token verification allowing you the option to handle the signing of tokens on a separate server or even through a different company such us Auth0. 
  3. Fine grained access control - Within the token payload you can easily specify user roles and permissions as well as resources that the user can access.
  4. Mobile Friendly - This type of authentication does not require cookies, so this authentication type can be used with mobile applications.
  5. Loosely Coupling- Your front-end application is not coupled with specific authentication mechanism, the token is generated from the server and your API is built in a way to understand this token and do the authentication.
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained method for securely transmitting information between parties encoded as a JSON object. JWT has gained mass popularity due to its compact size which allows tokens to be easily transmitted via query strings, header attributes and within the body of a POST request.
A JSON Web Token consists of three parts: Header, Payload and Signature. The header and payload are Base64 encoded, then concatenated by a period, finally the result is algorithmically signed producing a token in the form of header.claims.signature. The header consists of metadata including the type of token and the hashing algorithm used to sign the token. The payload contains the claims data that the token is encoding.

JSON Web Token Best practices
1. Keep it secret. Keep it safe. The signing key should be treated like any other credentials and revealed only to services that absolutely need it.
2. Do not add sensitive data to the payload. Tokens are signed to protect against manipulation and are easily decoded. Add the bare minimum number of claims to the payload for best performance and security.
3. Give tokens an expiration. Technically, once a token is signed – it is valid forever – unless the signing key is changed or expiration explicitly set. This could pose potential issues so have a strategy for expiring and/or revoking tokens.

4. Embrace HTTPS. Do not send tokens over non-HTTPS connections as those requests can be intercepted and tokens compromised.

World of Constructors in C#

Constructors

     Constructor are class methods that are executed automatically when instance of class is created. Constructors are use to initialize globally declared members of given class. Constructors can only run once when instance of class is created or in other words memory is allocated for given class and even it constructor runs before any written code in class. We can have multiple constructors in class, with help of polymorphism in way of overloading. If we do not define any constructor in class then compiler will automatically create Default constructor in the class.

Notable points about Constructors
üA Class can have any numbers of constructors.
üA static constructor does not have any parameters.
üWith in class you can create only one static constructors.
üA constructor does not have any return type, not even void.

Types of Constructors
1. Default constructor - A Constructor without any parameter is called a default constructor. In this constructor every instance of class will be initialized without any parameters values. The default constructor initializes all numeric fields to zero and all strings and object to null.

2. Parametrized constructor - A Constructor with at least one parameter is called a parameterized constructor. This good side of this constructor is that you can initialize each instance with different values.
a) Constructor Overloading - We can overload constructor by creating another constructor with same method name with different parameters.

3. Copy constructor - A Parameterized constructor that contains a parameter of same class type is called copy constructor. The main purpose of copy constructor is to initialize new instance to the value of an existing instance.

4. Static constructor - When we declare constructor as static it will be invoked only once for any number of instances of class and its during creation of first instance or first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.
a) Notable points of Static constructor
i. A static constructor does not take access modifiers or have parameters.
ii. A static constructor cannot be called directly.
iii. The user has not control on when the static constructor is executed in the program because it is called by CLR during compile time.
iv. In Static constructors constants variable can be created, where values does not changes after initialization.

5. Private constructor - Private constructor is a special instance constructor used in a class that contains static member only. If a class has private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.

6.  Instance constructor - A Constructor without any parameter is called instance constructor. This sounds similar to Default constructor and static constructor, but it is different.
a) Default constructor initializes default values of variable as per data types, by in instance constructor we can assign specific values to variables.
              b) In static constructor, it get called automatically from CLR, but instance constructor get called when instance of class is created.

Destructor

     Destructor are use to release memory allocated for instance of object. In .Net framework, the garbage collector automatically manages the allocation and release of memory for manages objects in your application. But there were still unmanaged code, which can be free up specifically by using Destructor.

Tuesday, 2 September 2014

Difference between Object Vs Var Vs Dynamic Keyword

Object Vs Var Vs Dynamic Keyword
So right now in C# we have the Object class, and the var and dynamic types. At first look, they all seem to do the same job, but not really.

Then the question is, what is the difference between Object, var and dynamic? And when should we use them?

So here is the answer.

Object

The object class in C# represents the System.Object type, which is the root type in the C# class hierarchy. Generally we use this class when we cannot specify the object type at compile time, which generally happens, when we deal with interoperability.

Let's have an example. The following example explains that the variable amount, of which the type is object, but at run time we can get the actual type of that variable that is stored in the variable.


Object.jpg

Let's perform a mathematical operation on it.

Object1.jpg

Why are we unable to perform a mathematical operation on it and instead get an error message but in the previous example we get the type of Amount as System.Int32. If the amount is a Systme.Int32 type and we can store an integer value in it then why are we unable to apply a simple mathematical operation?

Here is the reason. Actually, an object requires explicit type conversion before it can be used. We can store anything in the object type variable but for performing an operation we must type cast it. Because C# is a statically typed language so it will throw an exception when we start performing any operation on it without proper type casting.

Object2.jpg

Var

The var type was introduced in C# 3.0. It is used for implicitly typed local variables and for anonymous types. The var keyword is generally used with LINQ.

When we declare a variable as a var type, the variable's type is inferred from the initialization string at compile time.

Object3.jpg

We cannot change the type of these variables at runtime. If the compiler can't infer the type, it produces a compilation error.

Object4.jpg

Dynamic

The dynamic type was introduced in C# 4.0. The dynamic type uses System.Object indirectly but it does not require explicit type casting for any operation at runtime, because it identifies the types at runtime only.

Object5.jpg

In the code above we are assigning various types of values in the variable amount because its type is dynamic and dynamic delays determination of the type until execution. All dynamic types variables enjoy the party at runtime.


Sunday, 31 August 2014

Serialization and Types of Serialization in C#.Net

Serialization:

1.Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache.

2.Serialization is the technology that enables an object to be converted into a linear stream of data that can be easily passed across process boundaries and machines. This stream of data needs to be in a format that can be understood by both ends of a communication channel so that the object can be serialized and reconstructed easily.

Advantage:

Using serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format.

Serialization is used by Remoting, Web Services SOAP for transmitting data between a server and a client. The Remoting technology of .NET makes use of serialization to pass objects by value from one application domain to another.

De-serialization is the reverse; it is the process of reconstructing the same object later.



Types of Serialization

Serialization can be of the following types:

1.Binary Serialization
2.SOAP Serialization
3.XML Serialization

Binary Serialization:

Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically. The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media.

Difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not. In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved.

Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object


The SOAP protocol is ideal for communicating between applications that use heterogeneous architectures. In order to use SOAP serialization in .NET we have to add a reference to System.Runtime.Serialization.Formatters.Soap in the application. The basic advantage of SOAP serialization is portability. The SoapFormatter serializes objects into SOAP messages or parses SOAP messages and extracts serialized objects from the message.

XML Serialization:

· According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or the parameters and returns values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document.

· XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform." Implementing XML Serialization in .Net is quite simple.

ML.NET: Machine Learning for .NET Developers

Machine Learning in .Net ML.NET is a free software machine learning library for the C# and F# programming languages. It also supports Pyth...