Tuesday, June 28, 2022

DATA INFRASTRUCTURE

 








OOPS concepts

 OOPS:

OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.


basic concepts of OOPS:

Abstraction

Encapsulation

Inheritance

Polymorphism


Class:

A class is simply a representation of a type  of object. It is the blueprint/plan/template that describes the details of an object.

object:

An object is an instance of a class. It has its own state, behavior and identity.


Encapsulation:

Encapsulation is an attribute of an object, and it contains all data which id hidden. That hidden data can be restricted to the members of that class.

Levels are Public, private , Protected , Internal and Protected Internal.


Polymorphism:

Polymorphism is nothing but assigning behavior or value in a sub-class to something that was already declared in the main class. Simply polymorphism takes more than one form.


Inheritance:

Inheritance is a concept where one class shares the structure and behavior defined in another class. If Inhertiance applied to one class is called single inheritance and if it depends on multiple classes, then it is called multiple inheritance.


Manipulators:

Manipulators are the functions which can be used in conjunction with the insertion (<< ) and extraction (<< ) operators on an object. Examples are endl and setw.


Constructors:

A  constructor is a method used to  initialize  the state of  an object and it gets invoked at the time of object creation. Rules for constructor are 

Constructor Name should be the same as a class name 

A constructor must have no return type.

Destructor:

A destructor is a method  which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.


Inline function:

An inline function is a technique used by the compilers and instructs to insert complete body of the function whereever that function is used in the program source code.


Virtual function:

A virtual function is a member function of a class and is functionality can be overridden in its derived class. This function can be implemented by using a keyword called virtual an it can be given function declaration.

A virtual function can be declared using a token(virtual) in C++. It can be achieved in  C/PYTHON language by using function pointers or pointers to function.


Friend function 

A friend function is a friend of a class that is allowed to access to public, private or protected data in that same class. If the function is defined outside the class cannot access such information.


A friend can be declared  anywhere in the class declaration and it cannot be affected by access control keywords like private, public or protected.


Function  overloading:

Function overloading is a regular function, but it is assigned with multiple parameters. It allows the creation of several methods with the same name which differ from each other by the type of input and output of the function.


Example


void add(int& a, int& b);

void add(doubles& a,  doubles& b);

void add(struct bob& a, struct bob& b);


Operator overloading:


Operator overloading is a function where different operators are appiled and depends on the arguments. Operator  -,* can be  used to pass through the function and it has its own precedence to execute.


Abstract class:

An abstract class is a class which cannot be instantiated.  Creation of an object is not possible with an abstract class, but it can be inherited. An abstract class can contain only an Abstract method. Java allows only abstract method in abstract class while other languages allow non-abstract method as well.

Ternary operator:

The ternary operator is said to be an operator which takes three arguments. Arguments and results are of different data types and it depends on the function. The ternary operator is also called a conditional operator.

Finalized method:

Finalized method helps to perform cleanup operations on the resources which are not currently used. Finalized method is protected and it is accessible only through this class or by a derived class.

Different types of arguments:

A parameter is a variable used during the declaration of the function or subroutine and arguments are passed to the function body and it should match with the parameter defined.

There are two types of arguments:

Call by value - Value passed will get modified only inside the function, and it returns the same value whatever it is passed into the function.

Call by reference - Value passed  will get  modified in both inside and outside the functions and it returns the same or different value.

































Thursday, May 26, 2022

how to swap two variables ?

 swap 2 variables 

>>> a = 1 

>>> b = 2


>>>  a 

2


>>> b

1


reason :


In other languages you need to keep an intermediate variable to accomplish this.


reference:


https://stackoverflow.com/questions/14836228/is-there-a-standardized-method-to-swap-two-variables-in-python/14836456#14836456


In python , two variables values swapped using this syntax:

left, right = right, left 


Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.


That means the following for the expression a,b = b,a :

  • The right-hand side b,a is evaluated, that is to say, a tuple of two elements is created in the memory. The two elements are the objects designated by the identifiers b and a, that were existing before the instruction is encountered during the execution of the program.
  • Just after the creation of this tuple, no assignment of this tuple object has still been made, but it doesn't matter, Python internally knows where it is.
  • Then, the left-hand side is evaluated, that is to say, the tuple is assigned to the left-hand side.
  • As the left-hand side is composed of two identifiers, the tuple is unpacked in order that the first identifier a be assigned to the first element of the tuple (which is the object that was formerly b before the swap because it had name b)
    and the second identifier b is assigned to the second element of the tuple (which is the object that was formerly a before the swap because its identifiers was a)

This mechanism has effectively swapped the objects assigned to the identifiers a and b

So, to answer your question: YES, it's the standard way to swap two identifiers on two objects.
By the way, the objects are not variables, they are objects.


I know three ways to swap variables, but a, b = b, a is the simplest. There is


XOR (for integers)

x = x ^ y
y = y ^ x
x = x ^ y

Or concisely,

x ^= y
y ^= x
x ^= y

Temporary variable

w = x
x = y
y = w
del w

Tuple swap

x, y = y, x


I would not say it is a standard way to swap because it will cause some unexpected errors.


nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]


nums[i] will be modified first and then affect the second variable nums[nums[i] - 1].


Does not work for multidimensional arrays, because references are used here.


import numpy as np

# swaps
data = np.random.random(2)
print(data)
data[0], data[1] = data[1], data[0]
print(data)

# does not swap
data = np.random.random((2, 2))
print(data)
data[0], data[1] = data[1], data[0]
print(data)



To get around the problems explained by eyquem, you could use the copy module to return a tuple containing (reversed) copies of the values, via a function:

from copy import copy

def swapper(x, y):
  return (copy(y), copy(x))

Same function as a lambda:


swapper = lambda x, y: (copy(y), copy(x))

Then, assign those to the desired names, like this:

x, y = swapper(y, x)

NOTE: if you wanted to you could import/use deepcopy instead of copy.


exceptions / challenges on the syntax 


















Thursday, May 19, 2022

Machine Learning Approach to Sales Demand Forecasting

 Machine Learning Approach to Sales Demand Forecasting


During these uncertain times, businesses are operating in unfamiliar grounds and are obliged to rely on rapid shifts in customer behaviour. 

Behaviour may be influenced by new experience, market economic conditions, inflation, political unrest etc. Machine learning techniques can help with demand or sales order forecasting.


ML Approach
Data Collection-> Exploratory Data Analysis->Feature Engineering->Modelling->Hyperparameter Tuning->Deployment

The most critical part for demand forecasting is data and feature engineering.

Data Integration Ideas:-
- Link sales demand to order management data
- Link CRM opportunities with transactional sales data
- Link customer and product data to expected demand
- Outlier treatment and data cleaning/enrichment

Feature Engineering Ideas:-
- Sales opportunity representation based on historical sales
- Transactions forecasting
- Historical product sales

Feature Selection Ideas:-
- Rank features based on their relevance.
- Adding the most important features and checking model performance each time until the ideal cut-off is achieved.
- Using a wrapper method to eliminate noise/redundancy.

ML Algorithms:-
- ARIMA/SARIMA
- Linear Regression
- XGBoost
- K-Nearest Neighbors Regression
- Random Forest
- Long Short-Term Memory (LSTM)
- Ensemble model depending on the business need - NLP to understand customer sentiment from emails, classification model to predict if customer will order and churn and finally forecasting order dates, quantity etc.