Trending December 2023 # How To Code Your Resnet From Scratch In Tensorflow? # Suggested January 2024 # Top 16 Popular

You are reading the article How To Code Your Resnet From Scratch In Tensorflow? updated in December 2023 on the website 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 How To Code Your Resnet From Scratch In Tensorflow?

This article was published as a part of the Data Science Blogathon


We all have heard about ResNets for Image Recognition and, many of us feel that ResNets can be intimidating in the beginning. The architecture of a ResNet looks huge and complicated at first, but once you understand the core concept behind ResNets you can do wonders with it.

In this blog we are going to look at the following points:

What are ResNets?

What are the different types of ResNets?

How to code a ResNet in Tensorflow?

What are ResNets and their Types?

ResNets are called Residual Networks. ResNet is a special type of Convolutional Neural Network (CNN) that is used for tasks like Image Recognition. ResNet was first introduced in 2023 by Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun in their paper – “Deep Residual Learning for Image Recognition”.

Different types of ResNets can be developed based on the depth of the network like ResNet-50 or ResNet-152. The number at the end of ResNet specifies the number of layers in the network or how deep the networks are. We can design a ResNet with any depth using the basic building blocks of a ResNet that we will be looking ahead:

A ResNet can be called an upgraded version of the VGG architecture, with the difference between them being the skip connections used in ResNets. In the figure below, we can see the architecture of the VGG as well as the 34 layer ResNet.

Now you might be wondering why do we use a skip connection what purpose does it serve? So the answer to your question would be, in earlier CNN architectures as more and more layers were added to the Neural Network it was observed that the performance of the model started dropping, this was because of the vanishing gradient problem. As we went deeper into a network the vanishing gradient problem becomes more and more significant and the solution to this problem was using a skip connection. To know more about skip connections and the math behind them you can refer to this paper.

In Figure 2. we can see how a skip connection works, the skip connection skips training from a few layers and then connects it to the output. This helps the network skip the layers, which are hurting the performance of the model. This allows us to go deeper into the network without facing the problem of vanishing gradient.

In Figure 2. we can see two types of skip connections the left side block is called an Identity block and, the right side block is called a Bottleneck / Convolutional block. The difference between the two blocks is that the Identity block directly adds the residue to the output whereas, the Convolutional block performs a convolution followed by Batch Normalisation on the residue before adding it to the output.

Identity Block Structure and Code

Now, let’s understand this identity block, every identity block has the architecture/algorithm as following: (Refer Fig 3.)

Algorithm for Identity Block X_skip = Input Convolutional Layer (3X3) (Padding=’same’) (Filters = f) →(Input) Batch Normalisation →(Input) Relu Activation →(Input) Convolutional Layer (3X3) (Padding = ‘same’) (Filters = f) →(Input) Batch Normalisation →(Input) Add (Input + X_skip) Relu Activation


You might wonder why we have taken padding as ‘same’ only for all Convolution layers. The reason behind this is, we have to maintain the shape of our input until we add it to the residue. If the shape of the input gets changed, we will get a Numpy Error saying- “Two arrays with different shapes cannot be added”.

For example, consider the input size = (24 * 24), so the shape of our residue = (24 * 24) when we apply a kernel of (3, 3) on this input, the output shape = (22 * 22) but the shape of our residue will still be (24 * 24) which will make it impossible to Add as their shapes are different. Note that the layers having a conv filter of (1,1) don’t require padding as the kernel size (1 * 1) will not alter the shape of the input. Look at this formula for reference to the above example.

Fig 4. The formula for Output Size after a Convolution

Code for Identity Block

Now let’s code this block in Tensorflow with the help of Keras. To execute this code you will need to import the following:

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt

def identity_block(x, filter): # copy tensor to variable called x_skip x_skip = x # Layer 1 x = tf.keras.layers.Conv2D(filter, (3,3), padding = 'same')(x) x = tf.keras.layers.BatchNormalization(axis=3)(x) x = tf.keras.layers.Activation('relu')(x) # Layer 2 x = tf.keras.layers.Conv2D(filter, (3,3), padding = 'same')(x) x = tf.keras.layers.BatchNormalization(axis=3)(x) # Add Residue x = tf.keras.layers.Add()([x, x_skip]) x = tf.keras.layers.Activation('relu')(x) return x

Code for Identity Block of 34-Layer ResNet

Convolutional Block Structure and Code

Now that we have coded the identity block let us move on to the convolutional block the architecture/algorithm for Convolutional Block is as follows: (Refer Fig 5.)

Fig 5. Convolutional Block in a ResNet

Algorithm for Convolutional Block X_skip = Input Convolutional Layer (3X3) (Strides = 2) (Filters = f) (Padding = ‘same’) →(Input) Batch Normalisation →(Input) Relu Activation →(Input) Convolutional Layer (3X3) (Filters = f) (Padding = ‘same’) →(Input) Batch Normalisation →(Input) Convolutional Layer (1X1) (Filters = f) (Strides = 2) →(X_skip) Add (Input + X_skip) Relu Activation


Some of the points to note in this convolution block are, the residue is not directly added to the output but is passed through a Convolution Layer. The strides in these layers are used to minimize the size of the image. Similar to the Identity block, we have to make sure that the Shape of the Input and Residue is the same so let us confirm this with an example. Refer to Fig 4. for cross-checking the calculations.

Input Shape = (24, 24), Residue Shape = (24, 24) After ConvInput1 → Input Shape = (13, 13) *stride = 2 After ConvInput2 → Input Shape = (13, 13) After ConvResidue1 → Residue Shape = (13, 13) *stride = 2

As we can see that the Input Shape and the Residue Shape end with the same dimensions we can now start coding this block.

Code for Convolutional Block def convolutional_block(x, filter): # copy tensor to variable called x_skip x_skip = x # Layer 1 x = tf.keras.layers.Conv2D(filter, (3,3), padding = 'same', strides = (2,2))(x) x = tf.keras.layers.BatchNormalization(axis=3)(x) x = tf.keras.layers.Activation('relu')(x) # Layer 2 x = tf.keras.layers.Conv2D(filter, (3,3), padding = 'same')(x) x = tf.keras.layers.BatchNormalization(axis=3)(x) # Processing Residue with conv(1,1) x_skip = tf.keras.layers.Conv2D(filter, (1,1), strides = (2,2))(x_skip) # Add Residue x = tf.keras.layers.Add()([x, x_skip]) x = tf.keras.layers.Activation('relu')(x) return x

Code for Convolutional Block in 34-Layer ResNet

ResNet-34 Structure and Code

Fig 6. 34-Layer, 50-Layer, 101-Layer ResNet Architecture

Now let us follow the architecture in Fig 6. and build a ResNet-34 model. While coding this block we have to keep in mind that the first block, of every block in the ResNet will have a Convolutional Block followed by Identity Blocks except the conv2 block. For example, in the architecture mentioned in Fig 6. the conv3 block has 4 sub-blocks. So, the 1st sub-block will be Convolutional Block, which will be followed by 3 Identity Blocks. For reference, you can go back and look at Fig 1. where the solid black lines represent an Identity Block. The dotted black line represents a Convolutional Block.

We will now combine the Identity and Convolutional Blocks that we coded earlier to build the ResNet-34. So, now let’s code this.

def ResNet34(shape = (32, 32, 3), classes = 10): # Step 1 (Setup Input Layer) x_input = tf.keras.layers.Input(shape) x = tf.keras.layers.ZeroPadding2D((3, 3))(x_input) # Step 2 (Initial Conv layer along with maxPool) x = tf.keras.layers.Conv2D(64, kernel_size=7, strides=2, padding='same')(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation('relu')(x) x = tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='same')(x) # Define size of sub-blocks and initial filter size block_layers = [3, 4, 6, 3] filter_size = 64 # Step 3 Add the Resnet Blocks for i in range(4): if i == 0: # For sub-block 1 Residual/Convolutional block not needed for j in range(block_layers[i]): x = identity_block(x, filter_size) else: # One Residual/Convolutional Block followed by Identity blocks # The filter size will go on increasing by a factor of 2 filter_size = filter_size*2 x = convolutional_block(x, filter_size) for j in range(block_layers[i] - 1): x = identity_block(x, filter_size) # Step 4 End Dense Network x = tf.keras.layers.AveragePooling2D((2,2), padding = 'same')(x) x = tf.keras.layers.Flatten()(x) x = tf.keras.layers.Dense(512, activation = 'relu')(x) x = tf.keras.layers.Dense(classes, activation = 'softmax')(x) model = tf.keras.models.Model(inputs = x_input, outputs = x, name = "ResNet34") return model

Code for ResNet34 Model

You can now put the code together and run it. Also, have a look at the model summary. This can be done using the ‘model.summary()’ that will show you the details of all the layers in our architecture. You can also try building different types of ResNets using the basics now!

I have trained this model on the CIFAR-10 dataset for 15 epochs without any image augmentation. The results I obtained are as following if you are interested in looking at the code I have used, you can visit this Jupyter Notebook on Kaggle.

ResNet-34 on CIFAR-10 Dataset


The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.


You're reading How To Code Your Resnet From Scratch In Tensorflow?

A Complete Python Tutorial To Learn Data Science From Scratch


This article is a complete tutorial to learn data science using python from scratch

It will also help you to learn basic data analysis methods using python

You will also be able to enhance your knowledge of machine learning algorithms


It happened a few years back. After working on SAS for more than 5 years, I decided to move out of my comfort zone. Being a data scientist, my hunt for other useful tools was ON! Fortunately, it didn’t take me long to decide – Python was my appetizer.

I always had an inclination for coding. This was the time to do what I really loved. Code. Turned out, coding was actually quite easy!

I learned the basics of Python within a week. And, since then, I’ve not only explored this language to the depth, but also have helped many other to learn this language. Python was originally a general purpose language. But, over the years, with strong community support, this language got dedicated library for data analysis and predictive modeling.

Due to lack of resource on python for data science, I decided to create this tutorial to help many others to learn python faster. In this tutorial, we will take bite sized information about how to use Python for Data Analysis, chew it till we are comfortable and practice it at our own end.

A complete python tutorial from scratch in data science.

Are you a beginner looking for a place to start your journey in data science and machine learning? Presenting a comprehensive course, full of knowledge and data science learning, curated just for you!

You can also check out the ‘Introduction to Data Science‘ course – a comprehensive introduction to the world of data science. It includes modules on Python, Statistics and Predictive Modeling along with multiple practical projects to get your hands dirty.

Basics of Python for Data Analysis Why learn Python for data analysis?

Python has gathered a lot of interest recently as a choice of language for data analysis. I had basics of Python some time back. Here are some reasons which go in favour of learning Python:

Open Source – free to install

Awesome online community

Very easy to learn

Can become a common language for data science and production of web based analytics products.

Needless to say, it still has few drawbacks too:

It is an interpreted language rather than compiled language – hence might take up more CPU time. However, given the savings in programmer time (due to ease of learning), it might still be a good choice.

Python 2.7 v/s 3.4

This is one of the most debated topics in Python. You will invariably cross paths with it, specially if you are a beginner. There is no right/wrong choice here. It totally depends on the situation and your need to use. I will try to give you some pointers to help you make an informed choice.

Why Python 2.7?

Awesome community support! This is something you’d need in your early days. Python 2 was released in late 2000 and has been in use for more than 15 years.

Plethora of third-party libraries! Though many libraries have provided 3.x support but still a large number of modules work only on 2.x versions. If you plan to use Python for specific applications like web-development with high reliance on external modules, you might be better off with 2.7.

Some of the features of 3.x versions have backward compatibility and can work with 2.7 version.

Why Python 3.4?

Cleaner and faster! Python developers have fixed some inherent glitches and minor drawbacks in order to set a stronger foundation for the future. These might not be very relevant initially, but will matter eventually.

It is the future! 2.7 is the last release for the 2.x family and eventually everyone has to shift to 3.x versions. Python 3 has released stable versions for past 5 years and will continue the same.

There is no clear winner but I suppose the bottom line is that you should focus on learning Python as a language. Shifting between versions should just be a matter of time. Stay tuned for a dedicated article on Python 2.x vs 3.x in the near future!

How to install Python?

There are 2 approaches to install Python:

Download Python

You can download Python directly from its project site and install individual components and libraries you want

Install Package

Alternately, you can download and install a package, which comes with pre-installed libraries. I would recommend downloading Anaconda. Another option could be Enthought Canopy Express.

Second method provides a hassle free installation and hence I’ll recommend that to beginners

The imitation of this approach is you have to wait for the entire package to be upgraded, even if you are interested in the latest version of a single library. It should not matter until and unless, until and unless, you are doing cutting edge statistical research.

Choosing a development environment

Once you have installed Python, there are various options for choosing an environment. Here are the 3 most common options:

Terminal / Shell based

IDLE (default environment)

iPython notebook – similar to markdown in R

IDLE editor for Python

While the right environment depends on your need, I personally prefer iPython Notebooks a lot. It provides a lot of good features for documenting while writing the code itself and you can choose to run the code in blocks (rather than the line by line execution)

We will use iPython environment for this complete tutorial.

Warming up: Running your first Python program

You can use Python as a simple calculator to start with:

Few things to note

You can start iPython notebook by writing “ipython notebook” on your terminal / cmd, depending on the OS you are working on

The interface shows In [*] for inputs and Out[*] for output.

You can execute a code by pressing “Shift + Enter” or “ALT + Enter”, if you want to insert an additional row after.

Before we deep dive into problem solving, lets take a step back and understand the basics of Python. As we know that data structures and iteration and conditional constructs form the crux of any language. In Python, these include lists, strings, tuples, dictionaries, for-loop, while-loop, if-else, etc. Let’s take a look at some of these.

Python libraries and Data Structures Python Data Structures

Following are some data structures, which are used in Python. You should be familiar with them in order to use them as appropriate.

Lists – Lists are one of the most versatile data structure in Python. A list can simply be defined by writing a list of comma separated values in square brackets. Lists might contain items of different types, but usually the items all have the same type. Python lists are mutable and individual elements of a list can be changed.

Here is a quick example to define a list and then access it:

Strings – Strings can simply be defined by use of single ( ‘ ), double ( ” ) or triple ( ”’ ) inverted commas. Strings enclosed in tripe quotes ( ”’ ) can span over multiple lines and are used frequently in docstrings (Python’s way of documenting functions). is used as an escape character. Please note that Python strings are immutable, so you can not change part of strings.

Tuples – A tuple is represented by a number of values separated by commas. Tuples are immutable and the output is surrounded by parentheses so that nested tuples are processed correctly. Additionally, even though tuples are immutable, they can hold mutable data if needed.

Since Tuples are immutable and can not change, they are faster in processing as compared to lists. Hence, if your list is unlikely to change, you should use tuples, instead of lists.

Dictionary – Dictionary is an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}.

Python Iteration and Conditional Constructs

Like most languages, Python also has a FOR-loop which is the most widely used method for iteration. It has a simple syntax:

for i in [Python Iterable]: expression(i) fact=1 for i in range(1,N+1): fact *= i

Coming to conditional statements, these are used to execute code fragments based on a condition. The most commonly used construct is if-else, with following syntax:

if [condition]: __execution if true__ else: __execution if false__

For instance, if we want to print whether the number N is even or odd:

if N%2 == 0: print ('Even') else: print ('Odd')

Now that you are familiar with Python fundamentals, let’s take a step further. What if you have to perform the following tasks:

Multiply 2 matrices

Find the root of a quadratic equation

Plot bar charts and histograms

Make statistical models

Access web-pages

If you try to write code from scratch, its going to be a nightmare and you won’t stay on Python for more than 2 days! But lets not worry about that. Thankfully, there are many libraries with predefined which we can directly import into our code and make our life easy.

For example, consider the factorial example we just saw. We can do that in a single step as:


Off-course we need to import the math library for that. Lets explore the various libraries next.

Python Libraries

Lets take one step ahead in our journey to learn Python by getting acquainted with some useful libraries. The first step is obviously to learn to import them into our environment. There are several ways of doing so in Python:

import math as m from math import *

In the first manner, we have defined an alias m to library math. We can now use various functions from math library (e.g. factorial) by referencing it using the alias m.factorial().

In the second manner, you have imported the entire name space in math i.e. you can directly use factorial() without referring to math.

Tip: Google recommends that you use first style of importing libraries, as you will know where the functions have come from.

Following are a list of libraries, you will need for any scientific computations and data analysis:

SciPy stands for Scientific Python. SciPy is built on NumPy. It is one of the most useful library for variety of high level science and engineering modules like discrete Fourier transform, Linear Algebra, Optimization and Sparse matrices.

Matplotlib for plotting vast variety of graphs, starting from histograms to line plots to heat plots.. You can use Pylab feature in ipython notebook (ipython notebook –pylab = inline) to use these plotting features inline. If you ignore the inline option, then pylab converts ipython environment to an environment, very similar to Matlab. You can also use Latex commands to add math to your plot.

Pandas for structured data operations and manipulations. It is extensively used for data munging and preparation. Pandas were added relatively recently to Python and have been instrumental in boosting Python’s usage in data scientist community.

Scikit Learn for machine learning. Built on NumPy, SciPy and matplotlib, this library contains a lot of efficient tools for machine learning and statistical modeling including classification, regression, clustering and dimensionality reduction.

Statsmodels for statistical modeling. Statsmodels is a Python module that allows users to explore data, estimate statistical models, and perform statistical tests. An extensive list of descriptive statistics, statistical tests, plotting functions, and result statistics are available for different types of data and each estimator.

Seaborn for statistical data visualization. Seaborn is a library for making attractive and informative statistical graphics in Python. It is based on matplotlib. Seaborn aims to make visualization a central part of exploring and understanding data.

Bokeh for creating interactive plots, dashboards and data applications on modern web-browsers. It empowers the user to generate elegant and concise graphics in the style of chúng tôi Moreover, it has the capability of high-performance interactivity over very large or streaming datasets.

Blaze for extending the capability of Numpy and Pandas to distributed and streaming datasets. It can be used to access data from a multitude of sources including Bcolz, MongoDB, SQLAlchemy, Apache Spark, PyTables, etc. Together with Bokeh, Blaze can act as a very powerful tool for creating effective visualizations and dashboards on huge chunks of data.

Scrapy for web crawling. It is a very useful framework for getting specific patterns of data. It has the capability to start at a website home url and then dig through web-pages within the website to gather information.

SymPy for symbolic computation. It has wide-ranging capabilities from basic symbolic arithmetic to calculus, algebra, discrete mathematics and quantum physics. Another useful feature is the capability of formatting the result of the computations as LaTeX code.

Requests for accessing the web. It works similar to the the standard python library urllib2 but is much easier to code. You will find subtle differences with urllib2 but for beginners, Requests might be more convenient.

Additional libraries, you might need:

os for Operating system and file operations

networkx and igraph for graph based data manipulations

regular expressions for finding patterns in text data

BeautifulSoup for scrapping web. It is inferior to Scrapy as it will extract information from just a single webpage in a run.

Now that we are familiar with Python fundamentals and additional libraries, lets take a deep dive into problem solving through Python. Yes I mean making a predictive model! In the process, we use some powerful libraries and also come across the next level of data structures. We will take you through the 3 key phases:

Data Exploration – finding out more about the data we have

Data Munging – cleaning the data and playing with it to make it better suit statistical modeling

Predictive Modeling – running the actual algorithms and having fun 🙂

Exploratory analysis in Python using Pandas

In order to explore our data further, let me introduce you to another animal (as if Python was not enough!) – Pandas

Image Source: Wikipedia

Pandas is one of the most useful data analysis library in Python (I know these names sounds weird, but hang on!). They have been instrumental in increasing the use of Python in data science community. We will now use Pandas to read a data set from an Analytics Vidhya competition, perform exploratory analysis and build our first basic categorization algorithm for solving this problem.

Before loading the data, lets understand the 2 key data structures in Pandas – Series and DataFrames

Introduction to Series and Dataframes

Series can be understood as a 1 dimensional labelled / indexed array. You can access individual elements of this series through these labels.

A dataframe is similar to Excel workbook – you have column names referring to columns and you have rows, which can be accessed with use of row numbers. The essential difference being that column names and row numbers are known as column and row index, in case of dataframes.

Series and dataframes form the core data model for Pandas in Python. The data sets are first to read into these dataframes and then various operations (e.g. group by, aggregation etc.) can be applied very easily to its columns.

More: 10 Minutes to Pandas

Practice data set – Loan Prediction Problem

You can download the dataset from here. Here is the description of the variables:

VARIABLE DESCRIPTIONS: Variable Description Loan_ID Unique Loan ID Gender Male/ Female Married Applicant married (Y/N) Dependents Number of dependents Education Applicant Education (Graduate/ Under Graduate) Self_Employed Self employed (Y/N) ApplicantIncome Applicant income CoapplicantIncome Coapplicant income LoanAmount Loan amount in thousands Loan_Amount_Term Term of loan in months Credit_History credit history meets guidelines Property_Area Urban/ Semi Urban/ Rural Loan_Status Loan approved (Y/N) Let’s begin with the exploration

To begin, start iPython interface in Inline Pylab mode by typing following on your terminal/windows command prompt:

ipython notebook --pylab=inline

This opens up iPython notebook in pylab environment, which has a few useful libraries already imported. Also, you will be able to plot your data inline, which makes this a really good environment for interactive data analysis. You can check whether the environment has loaded correctly, by typing the following command (and getting the output as seen in the figure below):


I am currently working in Linux, and have stored the dataset in the following location:

Importing libraries and the data set:

Following are the libraries we will use during this tutorial:




Please note that you do not need to import matplotlib and numpy because of Pylab environment. I have still kept them in the code, in case you use the code in a different environment.

After importing the library, you read the dataset using function read_csv(). This is how the code looks like till this stage:

import pandas as pd import numpy as np import matplotlib as plt %matplotlib inline Quick Data Exploration

Once you have read the dataset, you can have a look at few top rows by using the function head()


This should print 10 rows. Alternately, you can also look at more rows by printing the dataset.

Next, you can look at summary of numerical fields by using describe() function


describe() function would provide count, mean, standard deviation (std), min, quartiles and max in its output (Read this article to refresh basic statistics to understand population distribution)

Here are a few inferences, you can draw by looking at the output of describe() function:

LoanAmount has (614 – 592) 22 missing values.

Loan_Amount_Term has (614 – 600) 14 missing values.

Credit_History has (614 – 564) 50 missing values.

We can also look that about 84% applicants have a credit_history. How? The mean of Credit_History field is 0.84 (Remember, Credit_History has value 1 for those who have a credit history and 0 otherwise)

The ApplicantIncome distribution seems to be in line with expectation. Same with CoapplicantIncome

Please note that we can get an idea of a possible skew in the data by comparing the mean to the median, i.e. the 50% figure.

For the non-numerical values (e.g. Property_Area, Credit_History etc.), we can look at frequency distribution to understand whether they make sense or not. The frequency table can be printed by following command:


Similarly, we can look at unique values of port of credit history. Note that dfname[‘column_name’] is a basic indexing technique to acess a particular column of the dataframe. It can be a list of columns as well. For more information, refer to the “10 Minutes to Pandas” resource shared above.

Distribution analysis

Now that we are familiar with basic data characteristics, let us study distribution of various variables. Let us start with numeric variables – namely ApplicantIncome and LoanAmount

Lets start by plotting the histogram of ApplicantIncome using the following commands:


Here we observe that there are few extreme values. This is also the reason why 50 bins are required to depict the distribution clearly.

Next, we look at box plots to understand the distributions. Box plot for fare can be plotted by:


This confirms the presence of a lot of outliers/extreme values. This can be attributed to the income disparity in the society. Part of this can be driven by the fact that we are looking at people with different education levels. Let us segregate them by Education:

df.boxplot(column='ApplicantIncome', by = 'Education')

We can see that there is no substantial different between the mean income of graduate and non-graduates. But there are a higher number of graduates with very high incomes, which are appearing to be the outliers.

Now, Let’s look at the histogram and boxplot of LoanAmount using the following command:


Again, there are some extreme values. Clearly, both ApplicantIncome and LoanAmount require some amount of data munging. LoanAmount has missing and well as extreme values values, while ApplicantIncome has a few extreme values, which demand deeper understanding. We will take this up in coming sections.

Categorical variable analysis

Now that we understand distributions for ApplicantIncome and LoanIncome, let us understand categorical variables in more details. We will use Excel style pivot table and cross-tabulation. For instance, let us look at the chances of getting a loan based on credit history. This can be achieved in MS Excel using a pivot table as:

Note: here loan status has been coded as 1 for Yes and 0 for No. So the mean represents the probability of getting loan.

Now we will look at the steps required to generate a similar insight using Python. Please refer to this article for getting a hang of the different data manipulation techniques in Pandas.

temp1 = df['Credit_History'].value_counts(ascending=True) temp2 = df.pivot_table(values='Loan_Status',index=['Credit_History'],aggfunc=lambda x:{'Y':1,'N':0}).mean()) print ('Frequency Table for Credit History:') print (temp1) print ('nProbility of getting loan for each Credit History class:') print (temp2)

Now we can observe that we get a similar pivot_table like the MS Excel one. This can be plotted as a bar chart using the “matplotlib” library with following code:

import matplotlib.pyplot as plt fig = plt.figure(figsize=(8,4)) ax1 = fig.add_subplot(121) ax1.set_xlabel('Credit_History') ax1.set_ylabel('Count of Applicants') ax1.set_title("Applicants by Credit_History") temp1.plot(kind='bar') ax2 = fig.add_subplot(122) temp2.plot(kind = 'bar') ax2.set_xlabel('Credit_History') ax2.set_ylabel('Probability of getting loan') ax2.set_title("Probability of getting loan by credit history")

This shows that the chances of getting a loan are eight-fold if the applicant has a valid credit history. You can plot similar graphs by Married, Self-Employed, Property_Area, etc.

Alternately, these two plots can also be visualized by combining them in a stacked chart::

temp3 = pd.crosstab(df['Credit_History'], df['Loan_Status']) temp3.plot(kind='bar', stacked=True, color=['red','blue'], grid=False)

You can also add gender into the mix (similar to the pivot table in Excel):

If you have not realized already, we have just created two basic classification algorithms here, one based on credit history, while other on 2 categorical variables (including gender). You can quickly code this to create your first submission on AV Datahacks.

We just saw how we can do exploratory analysis in Python using Pandas. I hope your love for pandas (the animal) would have increased by now – given the amount of help, the library can provide you in analyzing datasets.

Next let’s explore ApplicantIncome and LoanStatus variables further, perform data munging and create a dataset for applying various modeling techniques. I would strongly urge that you take another dataset and problem and go through an independent example before reading further.

Data Munging in Python : Using Pandas Data munging – recap of the need

While our exploration of the data, we found a few problems in the data set, which needs to be solved before the data is ready for a good model. This exercise is typically referred as “Data Munging”. Here are the problems, we are already aware of:

There are missing values in some variables. We should estimate those values wisely depending on the amount of missing values and the expected importance of variables.

While looking at the distributions, we saw that ApplicantIncome and LoanAmount seemed to contain extreme values at either end. Though they might make intuitive sense, but should be treated appropriately.

In addition to these problems with numerical fields, we should also look at the non-numerical fields i.e. Gender, Property_Area, Married, Education and Dependents to see, if they contain any useful information.

If you are new to Pandas, I would recommend reading this article before moving on. It details some useful techniques of data manipulation.

Check missing values in the dataset

Let us look at missing values in all the variables because most of the models don’t work with missing data and even if they do, imputing them helps more often than not. So, let us check the number of nulls / NaNs in the dataset

df.apply(lambda x: sum(x.isnull()),axis=0)

This command should tell us the number of missing values in each column as isnull() returns 1, if the value is null.

Though the missing values are not very high in number, but many variables have them and each one of these should be estimated and added in the data. Get a detailed view on different imputation techniques through this article.

Note: Remember that missing values may not always be NaNs. For instance, if the Loan_Amount_Term is 0, does it makes sense or would you consider that missing? I suppose your answer is missing and you’re right. So we should check for values which are unpractical.

How to fill missing values in LoanAmount?

There are numerous ways to fill the missing values of loan amount – the simplest being replacement by mean, which can be done by following code:

df['LoanAmount'].fillna(df['LoanAmount'].mean(), inplace=True)

The other extreme could be to build a supervised learning model to predict loan amount on the basis of other variables and then use age along with other variables to predict survival.

Since, the purpose now is to bring out the steps in data munging, I’ll rather take an approach, which lies some where in between these 2 extremes. A key hypothesis is that the whether a person is educated or self-employed can combine to give a good estimate of loan amount.

First, let’s look at the boxplot to see if a trend exists:

Thus we see some variations in the median of loan amount for each group and this can be used to impute the values. But first, we have to ensure that each of Self_Employed and Education variables should not have a missing values.

As we say earlier, Self_Employed has some missing values. Let’s look at the frequency table:

Since ~86% values are “No”, it is safe to impute the missing values as “No” as there is a high probability of success. This can be done using the following code:


Now, we will create a Pivot table, which provides us median values for all the groups of unique values of Self_Employed and Education features. Next, we define a function, which returns the values of these cells and apply it to fill the missing values of loan amount:

table = df.pivot_table(values='LoanAmount', index='Self_Employed' ,columns='Education', aggfunc=np.median) # Define function to return value of this pivot_table def fage(x): return table.loc[x['Self_Employed'],x['Education']] # Replace missing values df['LoanAmount'].fillna(df[df['LoanAmount'].isnull()].apply(fage, axis=1), inplace=True)

This should provide you a good way to impute missing values of loan amount.

NOTE : This method will work only if you have not filled the missing values in Loan_Amount variable using the previous approach, i.e. using mean.

How to treat for extreme values in distribution of LoanAmount and ApplicantIncome?

Let’s analyze LoanAmount first. Since the extreme values are practically possible, i.e. some people might apply for high value loans due to specific needs. So instead of treating them as outliers, let’s try a log transformation to nullify their effect:

df['LoanAmount_log'] = np.log(df['LoanAmount']) df['LoanAmount_log'].hist(bins=20)

Now the distribution looks much closer to normal and effect of extreme values has been significantly subsided.

Coming to ApplicantIncome. One intuition can be that some applicants have lower income but strong support Co-applicants. So it might be a good idea to combine both incomes as total income and take a log transformation of the same.

df['TotalIncome'] = df['ApplicantIncome'] + df['CoapplicantIncome'] df['TotalIncome_log'] = np.log(df['TotalIncome']) df['LoanAmount_log'].hist(bins=20)

Now we see that the distribution is much better than before. I will leave it upto you to impute the missing values for Gender, Married, Dependents, Loan_Amount_Term, Credit_History. Also, I encourage you to think about possible additional information which can be derived from the data. For example, creating a column for LoanAmount/TotalIncome might make sense as it gives an idea of how well the applicant is suited to pay back his loan.

Next, we will look at making predictive models.

Building a Predictive Model in Python

After, we have made the data useful for modeling, let’s now look at the python code to create a predictive model on our data set. Skicit-Learn (sklearn) is the most commonly used library in Python for this purpose and we will follow the trail. I encourage you to get a refresher on sklearn through this article.

Since, sklearn requires all inputs to be numeric, we should convert all our categorical variables into numeric by encoding the categories. Before that we will fill all the missing values in the dataset. This can be done using the following code:

df['Gender'].fillna(df['Gender'].mode()[0], inplace=True) df['Married'].fillna(df['Married'].mode()[0], inplace=True) df['Dependents'].fillna(df['Dependents'].mode()[0], inplace=True) df['Loan_Amount_Term'].fillna(df['Loan_Amount_Term'].mode()[0], inplace=True) df['Credit_History'].fillna(df['Credit_History'].mode()[0], inplace=True) from sklearn.preprocessing import LabelEncoder var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status'] le = LabelEncoder() for i in var_mod: df[i] = le.fit_transform(df[i]) df.dtypes

Next, we will import the required modules. Then we will define a generic classification function, which takes a model as input and determines the Accuracy and Cross-Validation scores. Since this is an introductory article, I will not go into the details of coding. Please refer to this article for getting details of the algorithms with R and Python codes. Also, it’ll be good to get a refresher on cross-validation through this article, as it is a very important measure of power performance.

#Import models from scikit learn module: from sklearn.linear_model import LogisticRegression from sklearn.cross_validation import KFold #For K-fold cross validation from sklearn.ensemble import RandomForestClassifier from chúng tôi import DecisionTreeClassifier, export_graphviz from sklearn import metrics #Generic function for making a classification model and accessing performance: def classification_model(model, data, predictors, outcome): #Fit the model:[predictors],data[outcome]) #Make predictions on training set: predictions = model.predict(data[predictors]) #Print accuracy accuracy = metrics.accuracy_score(predictions,data[outcome]) print ("Accuracy : %s" % "{0:.3%}".format(accuracy)) #Perform k-fold cross-validation with 5 folds kf = KFold(data.shape[0], n_folds=5) error = [] for train, test in kf: # Filter training data train_predictors = (data[predictors].iloc[train,:]) # The target we're using to train the algorithm. train_target = data[outcome].iloc[train]    # Training the algorithm using the predictors and target., train_target)    #Record error from each cross-validation run    error.append(model.score(data[predictors].iloc[test,:], data[outcome].iloc[test])) print ("Cross-Validation Score : %s" % "{0:.3%}".format(np.mean(error))) #Fit the model again so that it can be refered outside the function:[predictors],data[outcome]) Logistic Regression

Let’s make our first Logistic Regression model. One way would be to take all the variables into the model but this might result in overfitting (don’t worry if you’re unaware of this terminology yet). In simple words, taking all variables might result in the model understanding complex relations specific to the data and will not generalize well. Read more about Logistic Regression.

We can easily make some intuitive hypothesis to set the ball rolling. The chances of getting a loan will be higher for:

Applicants having a credit history (remember we observed this in exploration?)

Applicants with higher applicant and co-applicant incomes

Applicants with higher education level

Properties in urban areas with high growth perspectives

So let’s make our first model with ‘Credit_History’.

outcome_var = 'Loan_Status' model = LogisticRegression() predictor_var = ['Credit_History'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 80.945% Cross-Validation Score : 80.946%

#We can try different combination of variables: predictor_var = ['Credit_History','Education','Married','Self_Employed','Property_Area'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 80.945% Cross-Validation Score : 80.946%

Generally we expect the accuracy to increase on adding variables. But this is a more challenging case. The accuracy and cross-validation score are not getting impacted by less important variables. Credit_History is dominating the mode. We have two options now:

Feature Engineering: dereive new information and try to predict those. I will leave this to your creativity.

Better modeling techniques. Let’s explore this next.

Decision Tree

Decision tree is another method for making a predictive model. It is known to provide higher accuracy than logistic regression model. Read more about Decision Trees.

model = DecisionTreeClassifier() predictor_var = ['Credit_History','Gender','Married','Education'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 81.930% Cross-Validation Score : 76.656%

Here the model based on categorical variables is unable to have an impact because Credit History is dominating over them. Let’s try a few numerical variables:

#We can try different combination of variables: predictor_var = ['Credit_History','Loan_Amount_Term','LoanAmount_log'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 92.345% Cross-Validation Score : 71.009%

Here we observed that although the accuracy went up on adding variables, the cross-validation error went down. This is the result of model over-fitting the data. Let’s try an even more sophisticated algorithm and see if it helps:

Random Forest

Random forest is another algorithm for solving the classification problem. Read more about Random Forest.

model = RandomForestClassifier(n_estimators=100) predictor_var = ['Gender', 'Married', 'Dependents', 'Education', 'Self_Employed', 'Loan_Amount_Term', 'Credit_History', 'Property_Area', 'LoanAmount_log','TotalIncome_log'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 100.000% Cross-Validation Score : 78.179%

Here we see that the accuracy is 100% for the training set. This is the ultimate case of overfitting and can be resolved in two ways:

Reducing the number of predictors

Tuning the model parameters

Let’s try both of these. First we see the feature importance matrix from which we’ll take the most important features.

#Create a series with feature importances: featimp = pd.Series(model.feature_importances_, index=predictor_var).sort_values(ascending=False) print (featimp)

Let’s use the top 5 variables for creating a model. Also, we will modify the parameters of random forest model a little bit:

model = RandomForestClassifier(n_estimators=25, min_samples_split=25, max_depth=7, max_features=1) predictor_var = ['TotalIncome_log','LoanAmount_log','Credit_History','Dependents','Property_Area'] classification_model(model, df,predictor_var,outcome_var)

Accuracy : 82.899% Cross-Validation Score : 81.461%

Notice that although accuracy reduced, but the cross-validation score is improving showing that the model is generalizing well. Remember that random forest models are not exactly repeatable. Different runs will result in slight variations because of randomization. But the output should stay in the ballpark.

You would have noticed that even after some basic parameter tuning on random forest, we have reached a cross-validation accuracy only slightly better than the original logistic regression model. This exercise gives us some very interesting and unique learning:

Using a more sophisticated model does not guarantee better results.

Avoid using complex modeling techniques as a black box without understanding the underlying concepts. Doing so would increase the tendency of overfitting thus making your models less interpretable

Feature Engineering is the key to success. Everyone can use an Xgboost models but the real art and creativity lies in enhancing your features to better suit the model.

You can access the dataset and problem statement used in this post at this link: Loan Prediction Challenge


Now, its time to take the plunge and actually play with some other real datasets. So are you ready to take on the challenge? Accelerate your data science journey with the following Practice Problems:

Frequently Asked Questions

Q1. How to learn python programming?

A. To learn Python programming, you can start by familiarizing yourself with the language’s syntax, data types, control structures, functions, and modules. You can then practice coding by solving problems and building projects. Joining online communities, attending workshops, and taking online courses can also help you learn Python. With regular practice, persistence, and a willingness to learn, you can become proficient in Python and start developing software applications.

Q2. Why Python is used?

A. Python is used for a wide range of applications, including web development, data analysis, scientific computing, machine learning, artificial intelligence, and automation. Python is a high-level, interpreted, and dynamically-typed language that offers ease of use, readability, and flexibility. Its vast library of modules and packages makes it a popular choice for developers looking to create powerful, efficient, and scalable software applications. Python’s popularity and versatility have made it one of the most widely used programming languages in the world today.

Q3. What are the 4 basics of Python?

A. The four basics of Python are variables, data types, control structures, and functions. Variables are used to store values, data types define the type of data that can be stored, control structures dictate the flow of execution, and functions are reusable blocks of code. Understanding these four basics is essential for learning Python programming and developing software applications.

Q4. Can I teach myself Python?

A. Yes, you can teach yourself Python. Start by learning the basics and practicing coding regularly. Join online communities to get help and collaborate on projects. Building projects is a great way to apply your knowledge and develop your skills. Remember to be persistent, learn from mistakes, and keep practicing.

End Notes

I hope this tutorial will help you maximize your efficiency when starting with data science in Python. I am sure this not only gave you an idea about basic data analysis methods but it also showed you how to implement some of the more sophisticated techniques available today.

You should also check out our free Python course and then jump over to learn how to apply it for Data Science.

Python is really a great tool and is becoming an increasingly popular language among the data scientists. The reason being, it’s easy to learn, integrates well with other databases and tools like Spark and Hadoop. Majorly, it has the great computational intensity and has powerful data analytics libraries.

So, learn Python to perform the full life-cycle of any data science project. It includes reading, analyzing, visualizing and finally making predictions.

Note – The discussions of this article are going on at AV’s Discuss portal. Join here! If you like what you just read & want to continue your analytics learning, subscribe to our emails, follow us on twitter or like our facebook page.


Complete Guide To Tensorflow Opencl

Introduction to TensorFlow OpenCL

TensorFlow is a machine learning algorithm execution framework based on artificial intelligence concepts. We’re working on adding support for OpenCLTM devices to the TensorFlow framework using SYCLTM to give developers access to a wider range of processors. SYCL is an easy free, cross-platform C++ abstraction layer, while OpenCL(Open Computing Language) is a framework for building applications that execute across heterogeneous platforms. OpenCL is a standard parallel computing standard for event and data-based parallelism.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Overview of TensorFlow OpenCL CUDA vs OpenCL

Comparison CUDA OpenCL

Developed by NVIDIA Corporation. developed by Khronos Group

Definition Compute Unified Device Architecture (CUDA) is a parallel computing design that supports applications that demand a lot of parallel processing. OpenCL is an open standard that may be used on a wide range of hardware, including desktop and laptop GPUs.

Multiple OS Support

e.g., Windows XP and later, macOS

OpenCL, on the other hand, can run on practically any operating system and on a wide range of hardware.

e.g., Android, FreeBSD, Windows, Linux, macOS e

GPU Support 2 GPUs Utilize 1GPU

Language support C, C++, fortran C, C++

Templates CUDA is a C API and also constructs. C++ bindings and has C99

Function Compiler- build kernels Kernels at run time.

Libraries Has a large number of high-performance libraries Although it has a large number of libraries that may be used on any OpenCL-compliant hardware, it is not as comprehensive as CUDA.


TensorFlow OpenCL examples

There are no known vulnerabilities in TensorFlow-OpenCL and no known vulnerabilities in its dependent libraries. The Apache-2.0 License applies to TensorFlow-OpenCL. This is a permissive license. Permissive licenses offer the fewest limitations and can be used in almost any project.

Blender’s most recent versions support OpenCL rendering. Using the container that has been provided to the Sylabs library, you can run Blender as a graphical programme that will use a local Radeon GPU for OpenCL compute:

$ singularity exec --rocm --bind /etc/OpenCL library://sylabs/demo/blend blender

Set-Up and Run the TensorFlow OpenCL

To add OpenCL support to TensorFlow, we need to use ComputeCpp to create an OpenCL version of TensorFlow. TensorFlow now includes OpenCL support, which can be implemented using SYCL, thanks to Codeplay. TensorFlow is based on the Eigen linear algebra C++ library.

OpenCL installation


Install Packages

pip install -U –user numpy==1.14.5 wheel==0.31.1 six==1.11.0 mock==2.0.0 enum34==1.1.6

Configure Set-up

cd tensorflow

Environment variables Set-up

It’s a good idea to run the tests to ensure TensorFlow was constructed successfully. With the following command, you may perform a big set of roughly 1500 tests:

bazel test --test_lang_filters=cc,py --test_timeout 1500 --verbose_failures --jobs=1 --config=sycl --config=opt --

Build Tensor Flow

cd tensorflow

Set-Up operations

with tf.Session() as se1:

This line-up will build a new context manager, instructing TensorFlow to use the GPU to accomplish those tasks.

TensorFlow program

Program #1

>>> se1.close()

d_name = sys.argv[1] print(“n” * 6)


Python chúng tôi gpu 1500


OpenCL Acceleration for TensorFlow

OpenCL allows a wide range of accelerators to be used, involving multi-core CPUs, GPUs, DSPs, FPGAs, and specialized hardware like inferencing engines. An OpenCL system is divided into host and device components, with host software developed in a general programming language like C or C++ and generated for running on a host CPU using a normal compiler. TensorFlow to OpenCL translation would necessitate scribbling the kernels in OpenCL C and distinct codebases, both of which would be difficult to maintain. All of it is single-source C++ when using SYCL, therefore it’s possible to integrate the SYCL back-end to TensorFlow in a non-intrusive way.

Let’s see the sample code for registration



In general, OpenCL is successful. As a standard, it contains all of the necessary parts, namely run-time code creation and sufficient support for heterogeneous computing. Therefore, in this article, we have seen how tensor flow is acted on OpenCL.

Recommended Articles

This is a guide to TensorFlow OpenCL. Here we discuss the Introduction, overviews, examples with code implementation. You may also have a look at the following articles to learn more –

How To Defragment Your Hard Drives From The Context Menu In Windows

If you are using a regular hard drive with a spinning disk, over time it may get cluttered and fragmented. This fragmentation reduces the responsiveness of the hard disk. This is especially true if you are using the hard disk for a long time without defragmenting. To deal with this, Windows automatically defragments your hard drive once a week when you are not using your computer. Of course, it doesn’t defragment your SSD as it doesn’t need it.

Note: before making any changes to the Windows Registry Editor, make sure that you have a good backup. It helps you roll back if anything bad happens in the process of editing.

In the Registry Editor, navigate to the following location:

Name the new key runas and press the Enter button. After you create the key, this is how it will look.

Once you are done setting up the value data, this is how it looks in the Registry Editor.

Now, name the new value as “Extended,” and press the Enter button. There is no need to add any value data for this value. Just continue to the next step.

Name the new key as “command,” and press the Enter button to save the changes.

Since we used the verbose switch (-v), you can see all the details in the command window as Windows defragments the selected drive.

Vamsi Krishna

Vamsi is a tech and WordPress geek who enjoys writing how-to guides and messing with his computer and software in general. When not writing for MTE, he writes for he shares tips, tricks, and lifehacks on his own blog Stugon.

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.

How To Protect Your Iphone Data From Thieves

Our iPhones hold valuable personal information such as contacts, messages, photos, and sensitive data like bank account details and passwords. So, what if iPhone data gets stolen? Thieves and hackers are always looking for ways to gain unauthorized access to devices and data. Therefore, taking the necessary steps to protect your iPhone data from thieves is crucial. 

In this guide, I will discuss practical tips and techniques to safeguard your iPhone and its contents from prying eyes. Whether you’re at home or on the go, learning how to protect your iPhone data from thieves will help you keep everything secure, providing peace of mind.

What would happen if your iPhone is stolen?

Several potential threats could arise if your iPhone falls into the wrong hands after being stolen.

Financial theft: Thieves could use sensitive personal information, such as credit card details, bank account information, or passwords, for identity theft or financial fraud.

Access to private data: If you have private photos, messages, or documents on your iPhone, they could use the information maliciously.

Unauthorized access to accounts: The stored login credentials on iCloud Keychains would give unauthorized access to your online accounts.

Device misuse: Your iPhone can be used for making calls, sending messages, or accessing the internet, resulting in unauthorized charges.

Compromised security: If you don’t use a Lock Screen passcode or Face ID, it allows access to your data, compromising security.

Blackmail or extortion: Thieves could use personal data on your iPhone to blackmail or extort you for any favors.

Also, if the thief knows the passcode of your iPhone, they can change your Apple ID password, remove your trusted accounts or mobile number, and, most crucially, turn off Find My connection. So, you can’t delete your data remotely and lose access to your iCloud account. Moreover, they get access to use Apple Pay or send Apple Cash.  

These are just some potential data threats that could occur if your iPhone is stolen. It’s essential to take immediate action to secure your iPhone and data to minimize the risk of these threats.

What should you do first to protect your iPhone data?

I know it’s a very panicky situation, but you must act quickly to stop data breaches. I’d suggest going through our article about taking some strong measures after your iPhone is stolen.

Besides, I strongly recommend remotely disabling Apple Pay. So, you can minimize the risk of data breaches and prevent thieves from accessing your personal information. After you’ve gone through these details, you must take the following precautionary steps to secure your iPhone in case of theft.

5 Tips to protect iPhone data from thieves

Standard security protocols are easy to tamper with, and basic iPhone features may help criminals steal your entire digital life. So, while you can’t always prevent device theft, you must take precautions to save your iPhone from theft and reduce the ease with which criminals may access your data.

1. Use an alphanumeric passcode on iPhone

If you think the Face ID is the ultimate shield for data security, then it’s not so. When your iPhone restarts, it requests the passcode instead of the Face or Touch ID. Also, anyone can access the iCloud Keychain password manager, approve Apple Pay or authenticate anything using the passcode. 

So, don’t stick to your six-digit numeric passcode. Go for longer, trickier alphanumeric passcodes that are more challenging to crack. You can easily set an alphanumeric passcode on your iPhone. Also, you can modify Auto-Lock to 30 seconds under the Display & Brightness settings. It will ensure that your phone is never left unlocked for an extended period. If you are facing issues with your iPhone Auto-Lock , we got you covered.

2. Don’t type passwords in public

It may sound creepy, but the thief might be following you. And after understanding your passcode, they might steal your device.

Law enforcement officials claim criminals devise cunning techniques to discover people’s passcodes, including photographing them from a distance. Therefore, while in public, cover your screen. Also, use Face ID or Touch ID to avoid passcode snooping while you’re out. 

3. Add passwords to apps on iPhone

You can add different passcodes to apps like Venmo, PayPal, etc., or lock apps with sensitive data. Don’t use the same LockScreen password for any other accounts. Besides, I suggest setting a screen time passcode. Also, choose Don’t Allow in the Account Changes section. 

4. Don’t rely on iCloud Keychain solely

As stated, if the thief knows your passcode, they can access all your saved passwords from iCloud Keychain. It allows fraudsters to access bank accounts on stolen iPhones. So, you must change all sensitive passwords.

For heightened security, use password manager apps that support biometric authentication but request a master password if unsuccessful.

5. Never keep sensitive information as photos

If you ever need to scan crucial documents with sensitive information like passports, licenses, cards, SSNs, etc., immediately delete them from Photos after use. Instead, keep them in the password manager’s secure file storage. It immensely helps to protect your iPhone data from thieves.   

Stay safe and secure!

You must protect your iPhone data from thieves as it holds so much personal and sensitive information. Besides the tips mentioned above, avoid public Wi-Fi, and use two-factor authentication to secure your iPhone from hackers. Always be vigilant and mindful of the security of your iPhone and its contents.

Explore more…

Author Profile


Ava is an enthusiastic consumer tech writer coming from a technical background. She loves to explore and research new Apple products & accessories and help readers easily decode the tech. Along with studying, her weekend plan includes binge-watching anime.

How To Remove Malware From Your WordPress Site

WordPress is one of the most popular website management systems used worldwide. According to W3Techs, it powers 34% of all websites on the Internet. The popularity of WordPress is in part due to the enormous number of plugins and templates available that allow almost anything to be done on a website.

That broad range of functionalities come with vulnerabilities as well. Hackers are often able to access the code and infect WordPress sites with malware just as they might plant malware on a router.

Table of Contents

Malware can infect and destroy your site, so it’s important to act quickly to remove malware from your WordPress site.

Contact Your Web Host First

Before attempting any of the suggestions below, contact your hosting company first. It is possible that the host server, especially if you are on a shared server, is spreading malicious code from another site onto yours.

Ask them to do a scan of their server to ensure it is not the culprit before attempting to remove the malware from your own site. In addition, they can make suggestions to less technical website owners on how to safely scan and remove malware from their site. 

Web hosts have the expertise, tools, and options to deal with malware, so check with them first before attempting to do it on your own. 

Take Preventative Measures

It’s always better to try to prevent threats before they happen. The most crucial action users should take is to make sure they are always running the latest and most stable version of WordPress, even if they are only installing on a test version on their computer.

Newer versions are usually released to fix common vulnerabilities found in previous versions. The same is true for plugins and themes. Keep them up-to-date and remove those you aren’t using.

Some of the many negative issues that malware can cause on a WordPress site include:

Web and MySQL increased consumption of server resources.

Spam mail sent in bulk.

Theft of customers’ and users’ personal data.

Loss of information from your site.

Google penalties.

What can you do if your website is infected or hacked? In this article, we will outline the steps you can take to remove malware from a WordPress site.

Use WordPress Malware Removal Plugins

If you can log in and access your WP admin area, you may not have to reload your entire site. Using a suitable WordPress plugin may help remove malware from your WordPress website.

MalCare is a premium plugin that will instantly remove malware from your WP installation. Not only will it clean up a hacked site, but it will also protect against future security breaches.

One of the many benefits of MalCare is that it scans your site on its own servers. Your website will not experience any load on its resources and will continue to run smoothly.

There are four pricing levels starting at $99/year for one site (Personal) up to a Custom Agency Plus plan for more than 20 sites. 

Malcare is a comprehensive WP security plugin that includes many additional features such as:

Real-time email alerts.

Tracking small file changes.

Minimizing false alarms.

One of the most used plugins for WP security is WordFence. It includes a malware scanner and endpoint firewall.

From protection against brute force attacks to firewall blocks, the free version of WordFence is powerful enough for smaller websites.

One of the free security plugins with the most features is All in One WP Security & Firewall. It provides an easy visual interface using meters and graphs.

All in One WP Security will protect websites by:

Providing file and database security.

Enhancing user registration security.

Blocking forceful login attempts.

Additional features include the ability to back up .wp-config and .htaccess files. Users can also restore these files if anything goes wrong on their site.

For a full list of all WordPress security plugins, visit chúng tôi If you are unable to log in, you may have to reinstall your entire site. 

If you are more tech savvy, and run a site on your own server, carefully follow the steps below.

Keep in mind that backing up your site and erasing it can be dangerous and should only be attempted by highly technical web owners. 

Backup Your Database & All Files

If you’re infected and need to remove malware from your WordPress site, it’s important to protect your content immediately. Before doing anything, make a complete backup of your WordPress site so you can restore it in case anything goes wrong. 

Be sure to back up a clean version of your MySQL database and FTP account. There are several ways to back up a site, including via cPanel, phpMyAdmin, and WordPress plugins (such as Vaultpress). 

It is highly recommended that all WordPress users backup their site regularly. The steps below outline how to manually remove malware from your WordPress site.

Step 1: Examine Your Files

All core WordPress files.


.htaccess: This is a hidden file and includes the name, username, and password to your WordPress database. To make sure you backed this file up, use a code editing application or an FTP program that allows you to view hidden files. Be sure to check the Show Hidden Files option.

SQL database.

Step 2: Erase All Files & Folders From The Public_html Folder

When you are sure you have a complete backup of your website, go into your web hosting File Manager. 

Find the public_html folder and delete its contents except for wp-config.php, wp-content, and cgi-bin folders.

Make sure you are viewing the invisible files too, including .htaccess as it may be compromised.

If you are hosting multiple sites, you should assume they have also been compromised because cross-infection is common. Follow the same process for all hosted sites on the same server.

Open the chúng tôi file and compare it against a sample wp-config file. You can find this file in the WP GitHub repository.

Also, look through your file to see if anything looks suspicious such as long strings of code. If you are sure something should not be there, remove it. 

Now go to the wp-content directory and:

Make a list of all your installed plugins and then delete them.

Delete all themes, including the one you are using. You will reinstall it later.

Delete chúng tôi after you have deleted all the plugins.

Step 3: Install a Clean Version Of WordPress

Navigate to your web host control panel and reinstall WordPress into the same directory of the original location. 

Unzip the tar or zipped file and upload your files to your server. You will need to create a new chúng tôi file and enter the data from your website backup. You only need to enter the database name, password, and prefix.

Step 4: Reset Permalinks & Passwords

Log into your WP site and reset all usernames and passwords. If there are any unrecognized users, it means your database has been compromised. 

You can hire a professional to clean up your database to remove any malicious code.

Step 5: Reinstall Theme & Plugins

If you have customizations from your old site theme, look at the backup files you downloaded to your computer and replicate the changes on the fresh copy.

Step 6: Scan & Re-Upload Your Images & Documents From Your Backup

Use an up-to-date antivirus program to scan all the files to see if any of them are infected. Upload the clean files back to your server using an FTP client or the file manager. Keep the folder structure the same so you don’t end up with broken links.

Step 7: Notify Google

If you found out that your site was compromised by a warning from Google, you need to let them know that you have removed the malware so they can dismiss the notice on your account.

Go to Google Search Console and log in if you already have an account. If you don’t, register your website.

Update the detailed information about How To Code Your Resnet From Scratch In Tensorflow? on the 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!