Saturday, November 2, 2019

Gradient Descent

Linear Regression
y = ax + b
* trying to find a and b based on the train set.
* create cost function which is Mean Squared Error.
* minimising the mean_squared_error.

Ways to minimise
* Normal equation
inv(X.T.dot.X).dot.X.T.y
Computational complexity O(n2.4) to O(n3)
* Gradient Descent
Start from random initialisation, then keep trying by modifying the partial derivative until it reaches minimum.
Important parameter: learning rate.
* Stochastic Gradient Descent
Instead of using the whole train_set, use random index of it
* Mini-batch Gradient Descent
Combining both batch Gradient Descent and Stochastic Gradient Descent
Use small random set of index called mini-batches

Polynomial Regression
Convert the polynomial into another feature (PolynomialFeatures) and then use LinearRegression to solve it.

Learning Curves
* see how is the model learning in terms of errors
* one of the way to see whether model is too simple or too complex, beside the cross validation way.

How to reduce overfitting
* Regularized Linear Models

Multiple way to regularised linear model
* Ridge Regression. Adding 1/2 of squared l2 norm of the weight vector to MSE
* Lasso Regression. Adding l1 norm of the weight vector to MSE
* Lasso Regression tends to perform feature selection and remove it from the equation.
* Elastic Net. combining both ridge regression and lasso regression.

Classification

Binary Classifier
* try SGDClassifier as case study

Performance Measure
* use cross validation
cons: not good for skewed dataset
* use confusion matrix
cross_val_predict follows by confusion_matrix.
* Receiver Operating Characteristic (ROC) Curve

Confusion matrix
True Negative | False Negative
False Positive | True Positive

Accuracy = TP / ( TP + FN ) . e.g. kids video filterer. 
Recall/TPR = TP / (TP + FP) . e.g. detect thief on video surveillance

Both accuracy and recall is tradeoff; because based on SGDClassifier, if you move threshold to right; recall will go down and accuracy will go up. vice versa.

ROC
* plots true positive rate against false positive rate. or, plot recall versus 1-specificity (True negative rate)
* Based on graph, higher recall, more false positive the classifier produces.
* One way to measure the classifier is Area Under Curve. (roc_auc_score).
* Perfect classifier will have auc=1.0, while random will have 0.5.

PrecisionRecall Curve should be used when positivitiese class is rare or false positive is more important than false negative. Otherwise, use ROC curve

Other Type of classification
* Multiclass classification
extending binary classification to support more than one, either through OneVersusOne or OneVersusAll
* Multilabel classification
By outputing multiple label because it is trained on multiple label
* Multioutput classification
generalisation of multilabel classification where each label can have multiclass (multiple values)

Error Analysis
* By using confusion matrix and plotting it, we can see which error is the most common from our model
* It is not enough though.
* Divide each value in confusion matrix by number of images in corresponding class
* We will see more meaningful errors.


First Attempt Into Creating Model

Performance Measure
One of typical one is Root Mean Squared Error (RMSE). It gives the idea of how much error the prediction against the label.
The other one is Mean Squared Error (MSE).

Some data is in string format. need to convert it to something else.

How to create train and test set
* Train and test set can be achieved by sklearn train_test_split. normally, the percentage is 20% for test set.
* It is important to include all data in equal proportion between the real case and train set. e.g. if there is 30%men in real world, the data should also contain 30% men.

How to analyse the feature
* through graphs
* through correlation coefficient. e.g. data.corr()
it shows the coefficient between -1 and 1.
* another one, use panda scatter_matrix. compare each attribute against other attributes.
* combine the attributes because individually they are of no use.
* data cleaning. remove unrelated attributes.

How to handle text
* Conver to ordinal using OrdinalEncoder()
cons: the algorithm might mistake there is distance between different instance.
* OneHotEncoder where it creates column for every category and put 1 for the one that the instance applies to.
use sparse matrix to optimise space.

Fit and transform
* fit is to learn from existing data
* transform is to change the data
* fit_transform does the fit and transform together

Feature scaling
It is important to create same scale for all data; otherwise certain algorithm might perform badly.
* MinMaxScaler
convert it to 0-1 range
* Standardization
subtract the mean value and divided by standard deviation. it is less affected by outliers.

Custom Transformer and Transformation Pipelines
create your own transformer
create pipeline that put together bunch of transformer and models.

How to validate the model
* use mean_squared_error
but it is not enough, because it can lead to overfitting
* use cross validation
split the training set further into folds of train and validation set. and do the train on the train set and validate against the other validation test. calculate the MSE

Finetuning model
* GridSearchCV
* RandomizedSearchCV
* Ensemble; combine model

Intro to Machine Learning

Machine Learning in simple word, is trying to ask machine to deduct something based on data, lots of data.

Based on this data, model is created by algorithm.

argmax return value of a variable that maximise the function.

Machine learning is about:
* turn raw data into feature vectors
* analyse the feature and try different algorithm to come up with the model
* try the model
* rinse and repeat

Type of machine learning:
* supervised vs semisupervised vs unsupervised
supervised have training data.
* online vs batch
online; means it can be done on the go. while batch is offline
* instance-based vs model-based
instance-based; learn by heart. comparing it to the learned example
model-based; build the model from example. then use the model to predict

Overfitting
the model is good for the training set; but it is not good for test data

Underfitting
the model is not good for the training set.

First Post

This space is to document my learnings of interesting computer science  and programming subjects.

Why? Because try to explain something that you learn will make it better understanding.

Artificial Neural Network

Logical Computation With Neuron * It has one or more binary input and one output. * Activate output when certain number of input is active...