Naive Bayes

Bayes theorem helps us to incorporate new evidences/information into our model.

Code from sklearn

from sklearn.naive_bayes import GaussianNB

clf=GaussianNB()

clf.fit(features_train,labels_train)

Bayesian Learning:

Bayesian learning.PNG


Naive bayes

 

Linear in the number of variables.

Naive Bayes assumes conditional independence across attributes and doesn’t capture inter-relationship among attributes.

Even if probability of one of the attributes given label becomes zero, whole thing ends up being zero.

Machine learning

Pandas and numpy:

numpy.mean(df[ (df.gold>0) ][“bronze”])

olympic_medal_counts = {‘country_name’:countries,
‘gold’: Series(gold),
‘silver’: Series(silver),
‘bronze’: Series(bronze)}
df = DataFrame(olympic_medal_counts)

del df[‘country_name’]
# YOUR CODE HERE
avg_medal_count=df.apply(lambda x:numpy.mean(x))

b=numpy.array([4,2,1])

a=numpy.column_stack((numpy.array(gold),numpy.array(silver),numpy.array(bronze)))

points=numpy.dot(a,b)

olympic_points_df=DataFrame({‘country_name’:countries,’points’:points})

df[‘points’] = df[[‘gold’,’silver’,’bronze’]].dot([4, 2, 1]) olympic_points_df = df[[‘country_name’,’points’]]

 

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)

R2 score:

r2 score.PNG

If {\bar {y}} is the mean of the observed data:

{\bar {y}}={\frac {1}{n}}\sum _{i=1}^{n}y_{i}

then the variability of the data set can be measured using three sums of squares formulas:

SS_{\text{tot}}=\sum _{i}(y_{i}-{\bar {y}})^{2},
SS_{\text{reg}}=\sum _{i}(f_{i}-{\bar {y}})^{2},
{\displaystyle SS_{\text{res}}=\sum _{i}(y_{i}-f_{i})^{2}=\sum _{i}e_{i}^{2}\,}

The most general definition of the coefficient of determination is

R^{2}\equiv 1-{SS_{\rm {res}} \over SS_{\rm {tot}}}.\,

 

Accuracy = true positive + true negatives/total

from sklearn.metrics import accuracy_score

accuracy_score(y_true,y_pred)

Accuracy is not a right metric when data is skewed.


 

Precision:

precision.PNG

Recall:

Recall.PNG

 

f beta.PNG


ROC CURVE:


Errors:

Underfitting: Error due to bias, Oversimplified model, performs badly on both training and testing data.

Overfitting: Error due to variance. Over complicated model. model is too specific, performs badly on testing data.

Model complexity graph:

Model complexity graph.PNG

Training set: for training model.

Cross validation set: for choosing right parameters like degree of the polynomial. Useful to check whether the trained model is overfitting. If trained model performs poorly on this cross validation set, then the model is overfitted.

Testing set: For final testing.

K-FOLD CROSS VALIDATION:

Divide all your data into k buckets and iteratively create models by choosing one bucket as testing set and remaining for training.

Use average of these models for final model.

K-FOLD.PNG

 

If you increase training points on different models:

 

Grid Search CV:

GRID SEARCH CV.PNG


Plot learning curves to identify when to stop collecting data.


Supervised learning:

If there is an order in output data, then go for continuous model. Ex: income, age.

If there is no order, go for a discrete model. Ex: phone numbers, persons


Algorithms to minimize sum of squared errors:

  1. Ordinary least squares: sklearn LinearRegression.
  2. Gradient descent.

There can be multiple lines that minimize |error|, but only one that minimizes error^2.


Instance based learning:

Properties:

  1. Remembers.
  2. Fast and doesn’t learn.
  3. simple.
  4. No generalization.
  5. Sensitive to noise.

 

  1. Look up:

In k-nn all features matter equally because when we calculate ditance , all features are treated equally.

 

Locally weighted regression (evolved from k-nearest neighbors):


Naive bayes:

Powerful tools for creating classifiers for incoming labeled data.

Design patterns

 

Design that is more automated and require less maintenance is the standard.

Getters and setters:

Setters can be used for validation and constraining the value to be set to a particular range.

Getters can be used for returning default value, if the variable is not set yet or for lazy instantiation.


Can’t make objects out of abstract class. Abstract class can have some non abstract members.

Interface have only abstract methods.


 

Image result for design patterns java


Creational:

  1. Singleton:
    1. Can only have one instance of that particular class.
    2. President of a country, System in java.
    3. Private constructor, singleton using enum.
    4. @Singleton annotation.
    5. Difficult to unit test – why?
  2. Factory:
    1. Having a logic to return a particular subclass object, when asked for a class object.
  3. Abstract Factory:
  4. Builder:
    1. Separates object construction from its representation.
    2. interfaces.
  5. Prototype:
    1. Chess game initial setup.
    2. Copying/cloning the initial setup rather than creating the initial setup everytime you need it. Reduce redundant work.
    3. Copy a fully initialized instance.
    4. Link to code.

How to create objects?

Structural:

Inheritance? Interface? etc.

How are different classes related?

How are objects composed?

  1. Adapter:
    1.  Match interfaces of different classes. helps to communicate.
  2. Composite:
  3. Proxy:
    1. An object representing another object, like credit card as a proxy of bank account.
    2. Remote object and Home object(proxy).
  4. Flyweight:
    1. Reuse same objects by resetting values of the objects appropriately instead of creating new objects every time.
  5. Facade:
    1. Event managers, process, execute, group many steps into a single step.
  6. Bridge:
  7. Decorator:
    1. Add responsibilities to objects dynamically.
    2. Ex: adding different Toppings for different pizzas, adding discounts to different orders.

Behavioral:

Interactions between different objects.

  1. Template method:
  2. Mediator:
    1. instead of applications talking to each other, we use an enterprise service bus.
  3. Chain of responsibility:
    1. Passing a request through different objects.
  4. Observer:
    1. A way of notifying a change to a number of classes.
    2. This pattern is implemented in java.
    3. Subject extends Observable.
    4. Who wants to listen implements Observer and registers with the subject.
  5. Strategy:
    1. change the implementation/strategy of an interface at a later point in time.
    2. Pass whatever implementation needs to be used as an argument.
  6. Command:
    1. Encapsulate a command request as an object.
    2. java.lang.runnable threads are implemented like this.
  7. State:
  8. Visitor:
    1. Adding new operations to a particular class without inheritance and wi
  9. Iterator:
    1. Sequentially access the elements of a collection.
  10. Interpreter:
  11. Memento:
    1. Saving states of something as objects to restore them in future point of time if necessary.
    2. Undo/Redo operations.

Strategy pattern:

What:

 

Strategy design pattern.PNG

When:

Strategy pattern - when.PNG


Observer pattern:

when:

Observer pattern - when

What:

Observer pattern

Factory pattern :

Factory pattern - whenFactory pattern

Abstract Factory pattern:

Singleton pattern:

Singleton pattern.PNG

 Builder pattern:

Builder pattern.PNG

Prototype pattern:

 

Dependency injection

dependency injection1.PNG

What is dependency injection?

Instead of initiating the object we are dependent on, we take help of a dependency framework which will push the ready made object to the dependent class during runtime.

In the above picture we are not initializing hotDrink with new hotDrink(), instead we are getting it as a constructor parameter.

Why is it useful?

This is useful to decouple two different code packages and remove direct dependency.

If you have an interface which can have multiple implementations, with the dependency injection framework, you can choose to run which implementation to run at runtime. Suppose you want to isolate and test a particular package, you can provide mock implementations of all other interfaces it is dependent on.

If you have to do the same without DI injection and interfaces, then you need to change code in lot of places, calling mock object at all the places you were calling original object to test.

It is useful when you have dependencies depending on other dependencies and so on.

Implementing with Google Guice:

Guice diGuice di2

You should usually initialize the injector where you bootstrap the program.

bind or guice configure() helps you define which implementation of interface to use.

@implementedby annotation can be used instead of configure() and bind.

@implementedby can be used as a default one.

If both guice module(bind) and @implementedby compete for different implementations of same interface, guice module wins.


 

Wanna have conditional logic to pick implementations?

Providers


Dependency injection without interface:

 

Dependency injection

Enable a class to be generic by making it disown the responsibility of defining an object in itself. Make other classes define the input object for it.

If you want to change the input object , you dont need to change the original class with dependency injection.

dependency injection

A spring container contains a set of objects or beans.

Essence of Rich Dad Poor Dad

Identify what are assets and what are liabilities.

Increase asset column.

Don’t work for money, make money work for you.

Rich buy luxuries late.

Rich guys income statement and balance sheet.

rdpd.jpg

How corporations help rich with taxes.

5oxAy.png

Corporations earn, spend and then pay taxes on the rest.

Individuals earn, get taxed and then spend.

Listing some assets:

  1. Businesses that do not require my presence.
  2. Stocks – Fortunes are made in new stock issues(new stocks are tax-free).
  3. Bonds.
  4. Income generating real estate.
  5. Notes (IOUs).
  6. Royalties from intellectual property.

Dimensions of financial literacy:

  1. Accounting:
  2. Investing:
  3. Understanding markets:
  4. The law: