You are reading the article A Beginner’s Guide Bayesian Inference updated in December 2023 on the website Achiashop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 A Beginner’s Guide Bayesian Inference
This article was published as a part of the Data Science Blogathon.
Introduction
Classical
Frequentist
Bayesian
Let’s understand the differences among these 3 approaches with the help of a simple example.
Suppose we’re rolling a fair six-sided die and we want to ask what is the probability that the die shows a four? Under the Classical framework, all the possible outcomes are equally likely i.e., they have equal probabilities or chances. Hence, answering the above question, there are six possible outcomes and they are all equally likely. So, the probability of a four on a fair six-sided die is just 1/6. This Classical approach works well when we have well-defined equally likely outcomes. But when things get a little subjective then it may become a little complex.
On the other hand, Frequentist definition requires us to have a hypothetical infinite sequence of a particular event and then to look at the relevant frequency in that hypothetical infinite sequence. In the case of rolling a fair six-sided die, if we roll it for the infinite number of times then 1/6th of the time, we will get a four and hence, the probability of rolling four in a six-sided die will be 1/6 under frequentist definition as well.
Now if we proceed a little further and ask if our die is fair or not. Under frequentist paradigm, the probability is either zero when it’s not a fair die and one if it is a fair die because under frequentist approach everything is measured from a physical perspective and hence, the die can be either fair or not. We cannot assign a probability to the fairness of the die. Frequentists are very objective in how they define probabilities but their approach cannot give intuitive answers for some of the deeper subjective issues.
Bayesian perspective allows us to incorporate personal belief/opinion into the decision-making process. It takes into account what we already know about a particular problem even before any empirical evidence. Here we also have to acknowledge the fact my personal belief about a certain event may be different than others and hence, the outcome that we will get using the Bayesian approach may also be different.
For example, I may say that there is a 90% probability that it will rain tomorrow whereas my friend may say I think there is a 60% chance that it will rain tomorrow. So inherently Bayesian perspective is a subjective approach to probability, but it gives more intuitive results in a mathematically rigorous framework than the Frequentist approach. Let’s discuss this in detail in the following sections.
What is Bayes’ Theorem?Simplistically, Bayes’ theorem can be expressed through the following mathematical equation
Now let’s focus on the 3 components of the Bayes’ theorem
• Prior
• Likelihood
• Posterior
• Prior Distribution – This is the key factor in Bayesian inference which allows us to incorporate our personal beliefs or own judgements into the decision-making process through a mathematical representation. Mathematically speaking, to express our beliefs about an unknown parameter θ we choose a distribution function called the prior distribution. This distribution is chosen before we see any data or run any experiment.
How do we choose a prior? Theoretically, we define a cumulative distribution function for the unknown parameter θ. In basic context, events with the prior probability of zero will have the posterior probability of zero and events with the prior probability of one, will have the posterior probability of one. Hence, a good Bayesian framework will not assign a point estimate like 0 or 1 to any event that has already occurred or already known not to occur. A very handy widely used technique of choosing priors is using a family of distribution functions that is sufficiently flexible such that a member of the family will represent our beliefs. Now let’s understand this concept a little better.
i. Conjugate Priors – Conjugacy occurs when the final posterior distribution belongs to the family of similar probability density functions as the prior belief but with new parameter values which have been updated to reflect new evidence/ information. Examples Beta-Binomial, Gamma -Poisson or Normal-Normal.
ii. Non-conjugate Priors –Now, it is also quite possible that the personal belief cannot be expressed in terms of a suitable conjugate prior and for those cases simulation tools are applied to approximate the posterior distribution. An example can be Gibbs sampler.
iii. Un-informative prior – Another approach is to minimize the amount of information that goes into the prior function to reduce the bias. This is an attempt to have the data have maximum influence on the posterior. These priors are known as uninformative Priors but for these cases, the results might be pretty similar to the frequentist approach.
• Likelihood – Suppose θ is the unknown parameter that we are trying to estimate. Let’s represent fairness of a coin with θ. Now to check the fairness, we are flipping a coin infinitely and each time it is either appearing as ‘head’ or ‘tail’ and we are assigning a 1 or 0 value accordingly. This is known as the Bernoulli Trials. Probability of all the outcomes or ‘X’s taking some value of x given a value of theta. We’re viewing each of these outcomes as independent and hence, we can write this in product notation. This is the probability of observing the actual data that we collected (head or tail), conditioned on a value of the parameter theta (fairness of coin) and can be expressed as follows-
This is the concept of likelihood which is the density function thought of as a function of theta. To maximize the likelihood i.e., to make the event most likely to occur for the data we have, we will choose the theta that will give us the largest value of the likelihood. This is referred to as the maximum likelihood estimate or MLE. Additionally, a quick reminder is that the generalization of the Bernoulli when we have N repeated and independent trials is a binomial. We will see the application later in the article.
Mechanism of Bayesian Inference:The Bayesian approach treats probability as a degree of beliefs about certain event given the available evidence. In Bayesian Learning, Theta is assumed to be a random variable. Let’s understand the Bayesian inference mechanism a little better with an example.
Inference example using Frequentist vs Bayesian approach: Suppose my friend challenged me to take part in a bet where I need to predict if a particular coin is fair or not. She told me “Well; this coin turned up ‘Head’ 70% of the time when I flipped it several times. Now I am giving you a chance to flip the coin 5 times and then you have to place your bet.” Now I flipped the coin 5 times and Head came up twice and tail came up thrice. At first, I thought like a frequentist.
So, θ is an unknown parameter which is a representation of fairness of the coin and can be defined as
θ = {fair, loaded}
Additionally, I assumed that the outcome variable X (whether head or tail) follows Binomial distribution with the following functional representation
Now in our case n=5.
Now my likelihood function will be
Now, I saw that head came up twice, so my X =2.
= 0.13 if θ =loaded
Therefore, using the frequentist approach I can conclude that maximum likelihood i.e., MLE (theta hat) = fair.
Now comes the tricky part. If the question comes how sure am I about my prediction? I will not be able to answer that question perfectly or correctly as in a frequentist world, a coin is a physical object and hence, my probability can be either 0 or 1 i.e., the coin is either fair or not.
Therefore, my prior P(loaded)=0.9. I can now update my prior belief with data and get the posterior probability using Bayes’ Theorem.
My numerator calculation will be as follows-
The denominator is a constant and can be calculated as the expression below. Please note that we are here basically summing up the expression over all possible values of θ which is only 2 in this case i.e., fair or loaded.
Hence, after replacing X with 2 we can calculate the Bayesian probability of the coin being loaded or fair. Do it yourself and let me know your answer! However, you will realize that this conclusion contains more information to make a bet than the frequentist approach.
Application of Bayesian Inference in financial risk modeling:Bayesian inference has found its application in various widely used algorithms e.g., regression, Random Forest, neural networks, etc. Apart from that, it also gained popularity in several Bank’s Operational Risk Modelling. Bank’s operation loss data typically shows some loss events with low frequency but high severity. For these typical low-frequency cases, Bayesian inference turns out to be useful as it does not require a lot of data.
Earlier, Frequentist methods were used for operational risk models but due to its inability to infer about the parameter uncertainty, Bayesian inference was considered to be more informative as it has the capacity of combining expert opinion with actual data to derive the posterior distributions of the severity and frequency distribution parameters. Generally, for this type of statistical modeling, the bank’s internal loss data is divided into several buckets and the frequencies of each bucket loss are determined by expert judgment and then fitted into probability distributions.
Hello! I am Ananya. I have a degree in Economics and I have been working as a financial risk analyst for the last 5 years. I am also a voracious reader of Data Science Blogs just as you are. This is my first article for Analytics Vidhya. Hope you found this article useful.
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.
Related
You're reading A Beginner’s Guide Bayesian Inference
Understanding Neural Network: A Beginner’s Guide
The term “neural network” is derived from the work of a neuroscientist, Warren S. McCulloch and Walter Pitts, a logician, who developed the first conceptual model of an artificial neural network. In their work, they describe the concept of a neuron, a single cell living in a network of cells that receives inputs, processes those inputs, and generates an output. In the computing world, neural networks are organized on layers made up of interconnected nodes which contain an activation function. These patterns are presented to the network through the input layer which further communicates it to one or more hidden layers. The hidden layers perform all the processing and pass the outcome to the output layer.
Neural networks are typically used to derive meaning from complex and non-linear data, detect and extract patterns which cannot be noticed by the human brain. Here are some of the standard applications of neural network used these days. # Pattern/ Image or object recognition # Times series forecasting/ Classification # Signal processing # In self-driving cars to manage control # Anomaly detection These applications fall into different types of neural networks such as convolutional neural network, recurrent neural networks, and feed-forward neural networks. The first one is more used in image recognition as it uses a mathematical process known as convolution to analyze images in non-literal ways. Let’s understand neural network in R with a dataset. The dataset consists of 724 observations and 7 variables. “Companies.Changed” , “Experience.Score”, “Test.Score”, “Interview.Score”, “Qualification.Index”, “age”, “Status” The following codes runs the network classifying ‘Status’ as a function of several independent varaibles. Status refers to recruitment with two variables: Selected and Rejected. To go ahead, we first need to install “neuralnet” package >library(neuralnet) >HRAnalytics<-read.csv(“filename.csv”) > temp<-HRAnalytics Now, removing NA from the data > temp <-na.omit(temp) > dim(temp) # 724 rows and 7 columns) [1] 724 7 > y<-( temp$Status ) # Assigning levels in the Status Column > levels(y)<-c(-1,+1) > class(y) [1] “factor” # Now converting the factors into numeric > y<-as.numeric (as.character (y)) > y <-as.data.frame(y) > names(y)<-c(“Status”) Removing the existing Status column and adding the new one Y > temp$ Status <-NULL > temp <-cbind(temp ,y) > temp <-scale( temp ) > chúng tôi (100) > n=nrow( temp ) The dataset will be split up in a subset used for training the neural network and another set used for testing. As the ordering of the dataset is completely random, we do not have to extract random rows and can just take the first x rows. > train <- sample (1:n, 500, FALSE) > f<- Status ~ Companies.Changed+Experience.Score+Test.Score+Interview.Score+Qualification.Index+age Now we’ll build a neural network with 3 hidden nodes. We will Train the neural network with backpropagation. Backpropagation refers to the backward propagation of error. > fit <- neuralnet (f, data = temp [train ,], hidden =3, algorithm = “rprop+”) Plotting the neural network > plot(fit, intercept = FALSE ,show.weights = TRUE)
The above plot gives you an understanding of all the six input layers, three hidden layers, and the output layer. > z<-temp > z<-z[, -7] The compute function is applied for computing the outputs based on the independent variables as inputs from the dataset. Now, let’s predict on testdata (-train) > pred <- compute (fit, z[-train,]) > sign(pred$net.result ) Now let’s create a simple confusion matrix: > table(sign(pred$net.result),sign( temp[-train ,7])) -1 1 -1 108 20 1 36 60 (108+60)/(108+20+36+60) [1] 0.75 Here, the prediction accuracy is 75% I hope the above example helped you understand how neural networks tune themselves to find the right answer on their own, increasing the accuracy of the predictions. Please note that the acceptable level of accuracy is considered to be over 80%. Unlike any other technique, neural networks also have certain limitations. One of the major limitation is that the data scientist or analyst has no other role than to feed the input and watch it train and gives the output. One of the article mentions that “with backpropagation, you almost don’t know what you’re doing”. If we just ignore the negatives, neural network has huge application and is a promising and practical form of machine learning. In the recent times, the best-performing artificial-intelligence systems in areas such as autonomous driving, speech recognition, computer vision, and automatic translation are all aided by neural networks. Only time will tell, how this field will emerge and offer intelligent solutions to problems we still have not thought of.
A Beginner’s Guide To Get Productive With Fastds!
This article was published as a part of the Data Science Blogathon.
IntroductionLearn how to control your files and large dataset with a few lines of scripts. FastDS combines the power of Git and DVC to provide a hassle-free versioning experience.
Companies are always on the lookout for tools that can improve the productivity of their employees, as 92% of employees say productive tools that help them perform tasks efficiently improve work satisfaction. FastDS is for beginners, data science enthusiasts, and employers looking for a tool to improve the Git and data versioning experience.
In this blog, we will understand everything about FastDS. We will also use it to version file, dataset, and Urdu Automatic Speech Recognition model and push the project files to DagsHub remote.
What is FastDS?FastDS (fds) is an open-source tool for all data professionals to version control code and datasets. fds at a high level uses Git and DVC to clone repositories, add changes, commit, and push. You can also use fds save for automating committing and pushing tasks.
Conventional Vs. FastDS
FastDS provides shortcuts for Git, and DVC commands to minimise the chances of human error, automate repetitive tasks, and provide a beginner-friendly environment.
Key Features of FastDSWhen I started using Git with DVC, I was making many mistakes in running commands. It took me a while to understand the mutual relation, and still, I make the same mistakes, which can cause delays during the project. The fds solve all that by automating repetitive tasks. In this section, we will learn about the various features of fds and how they can help us clone, monitor, and save project files.
Init: Initialize both Git and DVC repositories.
Status: Get the status of the Git and DVC repository.
Add: Add files/folders to Git and DVC repository.
Commit Commits changes to Git and DVC repository.
Clone: Clones from Git repository and pulls data from DVC remote.
Push: Push commits to Git and DVC remote.
Save: Add all the changes in the project, commit with a message, and push the changes to Git and DVC remote.
Automatic Speech RecognitionIn this project, we will fine-tune a version of Facebook/wav2vec2-Xls-r-300m on the Urdu subset of the common_voice dataset under the CC-0 license. The wav2vec 2.0 model architecture has allowed data scientists to build a state-of-the-art speech-to-text application in more than 80 languages. Like Siri or Google, you can create your speech recognition in your native language.
I have followed XLSR-Wav2Vec2 for the low-resource ASR guide to fine-tune the model and used Wav2Vec2 with n-grams to boost the model performance. Combining a fine-tuned model checkpoint with a language model using Kenlm can improve the word error rate (WER) by 10 to 15 points.
In this project, we will learn:
To create a repository on DagsHub.
Clone the repository using fds.
Run the training script.
Monitor the results.
Add the Data and Model folder to DVC.
Add a language model.
Use one line to save and push changes to the DagsHub remote.
Setting up FastDSFirst, we need to create a repository on DagsHub and add the required details. Your storage consists of Git, DVC, and MLflow remote links, and you can use them to upload files, datasets, and machine learning experiments.
Then, on your local machine, you can run the script below to clone the repository or if you are working on Colab, use `!` at the start of each script. The fds usually pull both Git and DVC files, but currently, we have an empty repository.
pip install fastds cd Urdu-ASR-SOTA dvc remote modify origin --local auth basic dvc remote modify origin --local user dvc remote modify origin --local passwordBefore running the training notebook, we need to move the dataset and required file to the local directory and then install all the packages required to fine-tune the model. You can check the list here.
Training FastDSWe won’t go deep into the training process. Instead, we will focus on generating model files and adding them to Git and DVC using fds. If you are interested in learning about the fine-tuning process, read XLSR-Wav2Vec2 for a low-resource ASR guide.
We will pull local audio data using the Hugging Face dataset library and use a transformer to download and finetune facebook/wav2vec2-xls-r-300m. We will also integrate DagHub logger and MLflow to track all the experiments.
CommittingAfter running the notebook and saving model checkpoints in a Model folder, we will use `fds add .` This will trigger an automatic response asking you to add large files/folders into DVC. After that, it will add those folders to .gitignore to avoid Git conflict.
You can now commit both Git and DVC and push the files to DagsHub remote storage by running the script below.
fds commit "first try" fds push ResultsI ran multiple experiments; some failed, some inconclusive, and some gave optimal results. You can access all the experiments under the Experiment tab. In the end, I was able to produce the best results by training the model for 200 epochs on four V100S GPUs (OVH cloud). The model is significant, and it is impossible to run it on Google Colab.
Usually, on Google Colab, training a model on 30 epochs can take up to 14-27 hours, depending on the available GPU.
If you want to evaluate the results, run the below script in the base directory. It will generate both WER and CER with target and predicted TXT files.
python3 chúng tôi --model_id Model --dataset Data --config ur --split test --chunk_length_s 5.0 --stride_length_s 1.0 --log_outputs Language ModelTo boost model performance, I have added a language model. If you are interested in learning about n-grams and want to add a language model to your ASR model, check out this tutorial: Boosting Wav2Vec2 with n-grams in Transformers.
Let’s save all the changes and push them to the remote by running the `fds save` script.
fds save "language model added"We can rerun a model evaluation script to monitor the improvement. Combining the n-gram language model with Wav2Vec2 has improved WER 56 to 46.
sh run_eval.shThe evaluation results with prediction are available in the Eval Results folder. The Eval Results folder has subfolders “With LM” and “Without LM”.
Quick StartThe project is open to the public under an open-source license, and if you are starting from zero, follow the steps mentioned in the code block to get started.
After that, you can run a prediction by running the Python script.
from datasets import load_dataset, Audio from transformers import pipeline model = "Model" data = load_dataset("Data", "ur", split="test", delimiter="t") def path_adjust(batch): batch["path"] = "Data/ur/clips/" + str(batch["path"]) return batch data = data.map(path_adjust) sample_iter = iter(data.cast_column("path", Audio(sampling_rate=16_000))) sample = next(sample_iter) asr = pipeline("automatic-speech-recognition", model=model) prediction = asr( sample["path"]["array"], chunk_length_s=5, stride_length_s=1) prediction ConclusionIn this blog, we have learned about fds and how they can be used in a machine learning project. We have covered all the critical features with examples. The Urdu ASR project is open to contribution, and if you see a mistake in the code or have suggestions about the project, please create an issue. I made a Gradio demo and deployed it on Hugging Face spaces: Urdu-ASR-SOTA Gradio App.
Read more articles on FastDS here.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Related
Beginner’s Guide To Image Gradient
So, the gradient helps us measure how the image changes and based on sharp changes in the intensity levels; it detects the presence of an edge. We will dive deep into it by manually computing the gradient in a moment.
Why do we need an image gradient?Image gradient is used to extract information from an image. It is one of the fundamental building blocks in image processing and edge detection. The main application of image gradient is in edge detection. Many algorithms, such as Canny Edge Detection, use image gradients for detecting edges.
Mathematical Calculation of Image gradientsEnough talking about gradients, Let’s now look at how we compute gradients manually. Let’s take a 3*3 image and try to find an edge using an image gradient. We will start by taking a center pixel around which we want to detect the edge. We have 4 main neighbors of the center pixel, which are:
(iv) P(x,y+1) bottom pixel
We will subtract the pixels opposite to each other i.e. Pbottom – Ptop and Pright – Pleft , which will give us the change in intensity or the contrast in the level of intensity of the opposite the pixel.
Change of intensity in the X direction is given by:
Gradient in Y direction = PR - PLChange of intensity in the Y direction is given by:
Gradient in Y direction = PB - PTGradient for the image function is given by:
𝛥I = [𝛿I/𝛿x, 𝛿I/𝛿y]Let us find out the gradient for the given image function:
We can see from the image above that there is a change in intensity levels only in the horizontal direction and no change in the y direction. Let’s try to replicate the above image in a 3*3 image, creating it manually-
Let us now find out the change in intensity level for the image above
GX = PR - PL Gy = PB - PT GX = 0-255 = -255 Gy = 255 - 255 = 0𝛥I = [ -255, 0]
Let us take another image to understand the process clearly.
Let us now try to replicate this image using a grid system and create a similar 3 * 3 image.
Now we can see that there is no change in the horizontal direction of the image
GX = PR - PL , Gy = PB - PT GX = 255 - 255 = 0 Gy = 0 - 255 = -255𝛥I = [0, -255]
But what if there is a change in the intensity level in both the direction of the image. Let us take an example in which the image intensity is changing in both the direction
Let us now try replicating this image using a grid system and create a similar 3 * 3 image.
GX = PR - PL , Gy = PB - PT GX = 0 - 255 = -255 Gy = 0 - 255 = -255𝛥I = [ -255, -255]
Now that we have found the gradient values, let me introduce you to two new terms:
Gradient magnitude
Gradient orientation
Gradient magnitude represents the strength of the change in the intensity level of the image. It is calculated by the given formula:
Gradient Magnitude: √((change in x)² +(change in Y)²)The higher the Gradient magnitude, the stronger the change in the image intensity
Gradient Orientation represents the direction of the change of intensity levels in the image. We can find out gradient orientation by the formula given below:
Gradient Orientation: tan-¹( (𝛿I/𝛿y) / (𝛿I/𝛿x)) * (180/𝝅) Overview of FiltersWe have learned to calculate gradients manually, but we can’t do that manually each time, especially with large images. We can find out the gradient of any image by convoluting a filter over the image. To find the orientation of the edge, we have to find the gradient in both X and Y directions and then find the resultant of both to get the very edge.
Different filters or kernels can be used for finding gradients, i.e., detecting edges.
3 filters that we will be working on in this article are
Roberts filter
Prewitt filter
Sobel filter
All the filters can be used for different purposes. All these filters are similar to each other but different in some properties. All these filters have horizontal and vertical edge detecting filters.
These filters differ in terms of the values orientation and size
Roberts FilterSuppose we have this 4*4 image
Let us look at the computation
The gradient in x-direction =
Gx = 100 *1 + 200*0 + 150*0 - 35*1 Gx = 65The gradient in y direction =
Gy = 100 *0 + 200*1 - 150*1 + 35*0 Gy = 50Now that we have found out both these values, let us calculate gradient strength and gradient orientation.
Gradient magnitude = √(Gx)² + (Gy)² = √(65)² + (50)² = √6725 ≅ 82We can use the arctan2 function of NumPy to find the tan-1 in order to find the gradient orientation
Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅) = 37.5685 Prewitt FilterPrewitt filter is a 3 * 3 filter and it is more sensitive to vertical and horizontal edges as compared to the Sobel filter. It detects two types of edges – vertical and horizontal. Edges are calculated by using the difference between corresponding pixel intensities of an image.
A working example of Prewitt filterSuppose we have the same 4*4 image as earlier
Let us look at the computation
The gradient in x direction =
Gx = 100 *(-1) + 200*0 + 100*1 + 150*(-1) + 35*0 + 100*1 + 50*(-1) + 100*0 + 200*1 Gx = 100The gradient in y direction =
Gy = 100 *1 + 200*1 + 200*1 + 150*0 + 35*0 +100*0 + 50*(-1) + 100*(-1) + 200*(-1) Gy = 150Now that we have found both these values let us calculate gradient strength and gradient orientation.
Gradient magnitude = √(Gx)² + (Gy)² = √(100)² + (150)² = √32500 ≅ 180We will use the arctan2 function of NumPy to find the gradient orientation
Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅) = 56.3099 Sobel FilterSobel filter is the same as the Prewitt filter, and just the center 2 values are changed from 1 to 2 and -1 to -2 in both the filters used for horizontal and vertical edge detection.
A working example of Sobel filterSuppose we have the same 4*4 image as earlier
Let us look at the computation
The gradient in x direction =
Gx = 100 *(-1) + 200*0 + 100*1 + 150*(-2) + 35*0 + 100*2 + 50*(-1) + 100*0 + 200*1 Gx = 50The gradient in y direction =
Gy = 100 *1 + 200*2 + 100*1 + 150*0 + 35*0 +100*0 + 50*(-1) + 100*(-2) + 200*(-1) Gy = 150Now that we have found out both these values, let us calculate gradient strength and gradient orientation.
Gradient magnitude = √(Gx)² + (Gy)² = √(50)² + (150)² = √ ≅ 58Using the arctan2 function of NumPy to find the gradient orientation
Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅) = 71.5650 Implementation using OpenCVWe will perform the program on a very famous image known as Lenna.
Let us start by installing the OpenCV package
#installing opencv !pip install cv2After we have installed the package, let us import the package and other libraries
Using Roberts filterPython Code:
Using Prewitt Filter #Converting image to grayscale gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Creating Prewitt filter kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]]) kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]]) #Applying filter to the image in both x and y direction img_prewittx = cv2.filter2D(img, -1, kernelx) img_prewitty = cv2.filter2D(img, -1, kernely) # Taking root of squared sum(np.hypot) from both the direction and displaying the result prewitt = np.hypot(img_prewitty,img_prewittx) prewitt = prewitt[:,:,0] prewitt = prewitt.astype('int') plt.imshow(prewitt,cmap='gray')OUTPUT:
Using Sobel filter gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) kernelx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]]) kernely = np.array([[1, 2, 1],[0, 0, 0],[-1,-2,-1]]) img_x = cv2.filter2D(gray_img, -1, kernelx) img_y = cv2.filter2D(gray_img, -1, kernely) #taking root of squared sum and displaying result new=np.hypot(img_x,img_y) plt.imshow(new.astype('int'),cmap='gray')OUTPUT:
ConclusionThis article taught us the basics of Image gradient and its application in edge detection. Image gradient is one of the fundamental building blocks of image processing. It is the directional change in the intensity of the image. The main application of image gradient is in edge detection. Finding the change in intensity can conclude that it can be a boundary of an object. We can compute the gradient, its magnitude, and the orientation of the gradient manually. We usually use filters, which are of many kinds and for different results and purposes. The filters discussed in this article are the Roberts filter, Prewitt filter, and Sobel filter. We implemented the code in OpenCV, using all these 3 filters for computing gradient and eventually finding the edges.
Some key points to be noted:
Image gradient is the building block of any edge detection algorithm.
We can manually find out the image gradient and the strength and orientation of the gradient.
We learned how to find the gradient and detect edges using different filters coded using OpenCV.
Beginner’s Guide To Using Nmap
nmap is a network scanning tool which can be used for a whole variety of network discovery tasks including port scanning, service enumeration and OS fingerprinting.
To install nmap on Ubuntu or Raspbian use:
sudo
apt-get install
nmap
For Linux versions that use yum, like Fedora, run this as root:
yum install
nmap
The simplest invocation is just to supply a hostname or IP address of a machine that you want to scan. nmap will then scan the machine to see which ports are open. For example:
nmap
192.168.1.101All TCP/IP connections use a port number to uniquely identify each network service. For example, web browser connections are made on port 80; emails are sent on port 25 and downloaded on port 110; secure shell connections are made on port 22; and so on. When nmap does a port scan, it shows which ports are open and able to receive connections. In turn, this indicates which services are running on the remote machine.
From a security point of view, the less services which are running on a host, the more secure it is. This is because there are less “holes” that an attacker can use to try and access the machine. It is also a useful way to perform a preliminary check to see if a service is running (and accepting connections). A quick scan of my Ubuntu server looks like this:
To discover which software is providing the server behind each of the open ports use the -sV option:
nmap
-sV
192.168.1.101Here are the results from a Raspberry Pi:
nmap has correctly discovered that the OpenSSH server is being used to provide a SSH service on the Pi. The tool also notes that the Pi is running Linux!
sudo
nmap
-O
192.168.1.43Here is the output from a scan performed against a Windows XP machine:
If you want to scan more than one host at a time, nmap allows you to specify multiple addresses or use address ranges. To scan more than one host just add extra addresses to the parameter list (with each one separated by a SPACE). For example to scan for open ports on 192.168.1.1, 192.168.1.4 and 192.168.1.43, use:
nmap
192.168.1.1 192.168.1.4 192.168.1.43To specify an address range use the dash symbol. For example to scan the first five hosts on your network you could use:
nmap
192.168.1.1-5
The output would look something like this:
The first host found is the router supplied by my Internet Service Provider (on address 192.168.1.1) and the second one is my Raspberry Pi (on 192.168.1.4).
Cookbook and summaryTo check if a specific port is open use -p followed by the port number or the port name, for example:
nmap
-p
ssh
192.168.1.4It can be combined with the -sV flag to determine the version of the software attached to that port:
nmap
-p
ssh
-sV
192.168.1.4To discover which hosts are alive on your network use the -sn flag. This will just ping the hosts specified in the address range. For example:
nmap
-sn
192.168.1.1-254
As a closing word of warning, don’t run scans against hosts that you don’t control or have permission to scan. Excessive scanning can be interpreted as an attack or could disrupt services unnecessarily.
Image credit: fiber Network Server by BigStockPhoto
Gary Sims
Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time.
The Beginner’s Guide To Smartphone Camera
But Why Not Get a Good Standalone Camera Instead?The whole “smartphone vs. camera” debate has been raging ever since high-end back-lit CMOS cameras started appearing on regular phones. So, what’s better?
The answer is: It really doesn’t matter. All of it depends on the reasons you might want to get a phone with a good camera as opposed to a piece of hardware completely dedicated to taking photographs. People who are serious about photography might get a decent DSLR camera, but still might want to dump some cash on a smartphone with good optics simply because it’s less bulky. You might not be carrying your bulky rig with all its attachable lens around when a great photo opportunity presents itself. In those cases, it’s very useful to have a powerful camera in your pocket.
You just can’t drag all of this around every time you walk out of your house:
What Makes a Phone’s Camera “Good?”If a phone doesn’t give you any information about its aperture or focal length, you have no way of telling whether it has a camera that meets your liking. Usually, phones that don’t show any indication in their specs other than the resolution are not putting any priority on their cameras.
Since you’re limited to whatever optical specifications the manufacturer provides for your lens, it’s not a bad idea to try to find something that suits your liking and provides the optical experience you are accustomed to taking pictures with.
For people who are not experienced with cameras, here are a few pointers:
A bigger focal length means that you’ll cover less area in the picture. The simplest way to describe focal length is by comparing it to zoom. The higher the focal length, the more “zoomed” the camera is. Smaller focal lengths mean you’ll have wider angles. Nikon has a decent guide on this if you’d like to know more in-depth information. Focal lengths are measured in millimeters. The typical optics on a phone have somewhere between 20 and 30 mm of focal length.
The aperture (focal ratio) determines how much light enters the camera. This ratio is notated with a fancy-looking italic lowercase “F”, known as an “f-number”. A higher f-number represents a smaller aperture, which captures more light. This is important for special shots that put objects in focus. For example, compare the two images below:
The top image is taken using a small aperture, and the bottom image is taken using a large one. On some phone cameras, the shutter will take care of this by moving ever so slightly just before taking a picture to modify the aperture. Similarly, focal length is also adjusted through optical zoom.
Ultimately, a good smartphone camera will have all these things. It will have the ability to zoom by moving the optics (adjusting the focal length) and change the aperture with a mechanical shutter. Since the cameras are digital, there has to be a decent on-board backlit CMOS sensor to construct these images with great accuracy. After all that, you can worry about resolution. But, anyway, a good camera will also have a decent resolution, althoug you shouldn’t make a big fuss about anything more than 5 megapixels.
Examples of Good Smartphone CamerasThe first thing that comes to mind as far as cameras are concerned is the Nokia Lumia 1020, with its brilliant 41-megapixel camera, its special software, and its spectacular CMOS and optics. There’s also the Samsung Galaxy S4 Zoom (a phone with an integrated full-blown PAS – point-and-shoot – camera), and the regular S4. The HTC One and iPhone 5S are close runners up.
Let’s Continue This Discussion!Miguel Leiva-Gomez
Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time.
Update the detailed information about A Beginner’s Guide Bayesian Inference on the Achiashop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!