Friday, April 7, 2023

Installing Rocky Linux on VM Fusion

The link in reference is a good one. Mainly 

- The rocky linux is not listed as an OS type in VM Fusion 

- Need to select the linux type as Linux > Other Linux 4.x Kernal 64bit 

With this, the installation window comes up properly 

references:

 https://linux.how2shout.com/install-rocky-linux-on-vmware-player-virtual-machine/

Monday, April 3, 2023

what is virbr0

The virbr0, or "Virtual Bridge 0" interface is used for NAT (Network Address Translation). It is provided by the libvirt library, and virtual environments sometimes use it to connect to the outside network.

$ ifconfig

Sample outputs:


virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00  

          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

          inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:39 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:0 (0.0 b)  TX bytes:7921 (7.7 KiB)

  

Remove that bridge vrbr0 will make it come back.


$ sudo ifconfig virbr0 down

$ sudo brctl delbr virbr0

Now start the 'default' network using virsh command.


$ sudo virsh net-start default

This will automatically re-create the virbr0 bridge.



references:

https://gist.github.com/abelardojarab/e10ed30ab69bf9636929e17e3446bc2a

What is Cosine Similarity and Adjusted Cosine Similarity

Cosine Similarity

Cosine Similarity is a measurement that quantifies the similarity between two vectors [Which is Rating Vector in this case]

Adjusted Cosine

Adjusted cosine similarity is a modified version of vector-based similarity where we incorporate the fact that different users have different ratings schemes. In other words, some users might rate items highly in general, and others might give items lower ratings as a preference. To handle this nature from rating given by user , we subtract average ratings for each user from each user's rating for different movies.

references:


What is a Zero Vector

 A zero vector, denoted 0, is a vector of length 0, and thus has all components equal to zero. It is the additive identity of the additive group of vectors.

A non-zero vector in a vector space V is a vector that is not equal to the zero vector in V.

In mathematics and physics, a vector space (also called a linear space) is a set whose elements, often called vectors, may be added together and multiplied ("scaled") by numbers called scalars. Scalars are often real numbers, but can be complex numbers or, more generally, elements of any field. The operations of vector addition and scalar multiplication must satisfy certain requirements, called vector axioms. The terms real vector space and complex vector space are often used to specify the nature of the scalars: real coordinate space or complex coordinate space.

references

https://en.wikipedia.org/wiki/Vector_space

AI/ML a good method for printing model performance info

 


# Creating a common fun  0-0OOction which is usable to print the accuracy metrics of different models

def evaluate_performance(actual, pred):

    # Accuracy Score

    acc_score = round(accuracy_score(actual, pred)*100,2)

    

    # Confusion matrix

    confusion = confusion_matrix(actual, pred)

   

    TP = confusion[1,1] # true positive 

    TN = confusion[0,0] # true negatives

    FP = confusion[0,1] # false positives

    FN = confusion[1,0] # false negatives

    

    # Calculating Sensitivity/Recall

    sensitivity_recall = (TP / float(TP + FN))

    sensitivity_recall = round(sensitivity_recall,2)

  

    # Calculating Specificity

    specificity = (TN / float(TN + FP))

    specificity = round(specificity,2)  

  

    # Calculating Precision

    precision = (TN / float(TN + FP))

    precision = round(precision,2)  

    

    # Calculating F_1 score

    f1_score = 2 * ((precision * sensitivity_recall) / (precision + sensitivity_recall))

    f1_score = round(f1_score,2)  

    

    return pd.DataFrame([{"TP":TP,"TN":TN,"FP":FP,"FN":FN,"Recall":sensitivity_recall,"Precision":precision,"Specificity":specificity,"F1-Score":f1_score,"Accuracy":acc_score}])

Sunday, April 2, 2023

How to use GridSearchCV?

sklearn.model_selection.GridSearchCV(estimator, param_grid,scoring=None,

          n_jobs=None, iid='deprecated', refit=True, cv=None, verbose=0, 

          pre_dispatch='2*n_jobs', error_score=nan, return_train_score=False) 


1.estimator: Pass the model instance for which you want to check the hyperparameters.

2.params_grid: the dictionary object that holds the hyperparameters you want to try

3.scoring: evaluation metric that you want to use, you can simply pass a valid string/ object of evaluation metric

4.cv: number of cross-validation you have to try for each selected set of hyperparameters

5.verbose: you can set it to 1 to get the detailed print out while you fit the data to GridSearchCV

6.n_jobs: number of processes you wish to run in parallel for this task if it -1 it will use all available processors. 



#import all necessary libraries

import sklearn

from sklearn.datasets import load_breast_cancer

from sklearn.metrics import classification_report, confusion_matrix 

from sklearn.datasets import load_breast_cancer 

from sklearn.svm import SVC 

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split 

 

#load the dataset and split it into training and testing sets

dataset = load_breast_cancer()

X=dataset.data

Y=dataset.target

X_train, X_test, y_train, y_test = train_test_split( 

                        X,Y,test_size = 0.30, random_state = 101) 

# train the model on train set without using GridSearchCV 

model = SVC() 

model.fit(X_train, y_train) 

   

# print prediction results 

predictions = model.predict(X_test) 

print(classification_report(y_test, predictions)) 




# defining parameter range 

param_grid = {'C': [0.1, 1, 10, 100],  

              'gamma': [1, 0.1, 0.01, 0.001, 0.0001], 

              'gamma':['scale', 'auto'],

              'kernel': ['linear']}  

   

grid = GridSearchCV(SVC(), param_grid, refit = True, verbose = 3,n_jobs=-1) 

   

# fitting the model for grid search 

grid.fit(X_train, y_train) 

 

# print best parameter after tuning 

print(grid.best_params_) 

grid_predictions = grid.predict(X_test) 

   

# print classification report 

print(classification_report(y_test, grid_predictions)) 

references:

How to do HyperParametertuning with GridSearchCV

In almost any Machine Learning project, we train different models on the dataset and select the one with the best performance. However, there is room for improvement as we cannot say for sure that this particular model is best for the problem at hand. Hence, our aim is to improve the model in any way possible. One important factor in the performances of these models are their hyperparameters, once we set appropriate values for these hyperparameters, the performance of a model can improve significantly. In this article, we will find out how we can find optimal values for the hyperparameters of a model by using GridSearchCV.


GridSearchCV is the process of performing hyperparameter tuning in order to determine the optimal values for a given model. As mentioned above, the performance of a model significantly depends on the value of hyperparameters. Note that there is no way to know in advance the best values for hyperparameters so ideally, we need to try all possible values to know the optimal values. Doing this manually could take a considerable amount of time and resources and thus we use GridSearchCV to automate the tuning of hyperparameters.


GridSearchCV is a function that comes in Scikit-learn’s(or SK-learn) model_selection package.So an important point here to note is that we need to have the Scikit learn library installed on the computer. This function helps to loop through predefined hyperparameters and fit your estimator (model) on your training set. So, in the end, we can select the best parameters from the listed hyperparameters.


 { 'C': [0.1, 1, 10, 100, 1000],  

   'gamma': [1, 0.1, 0.01, 0.001, 0.0001], 

   'kernel': ['rbf',’linear’,'sigmoid']  }

Here C, gamma and kernels are some of the hyperparameters of an SVM model. Note that the rest of the hyperparameters will be set to their default values


GridSearchCV tries all the combinations of the values passed in the dictionary and evaluates the model for each combination using the Cross-Validation method. Hence after using this function we get accuracy/loss for every combination of hyperparameters and we can choose the one with the best performance.

references:

https://www.mygreatlearning.com/blog/gridsearchcv/#:~:text=GridSearchCV%20is%20a%20technique%20for,parameter%20values%2C%20predictions%20are%20made.