Trending February 2024 # Build Resnet From Scratch With Python ! # Suggested March 2024 # Top 3 Popular

You are reading the article Build Resnet From Scratch With Python ! updated in February 2024 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 March 2024 Build Resnet From Scratch With Python !

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

Introduction

There have been a series of breakthroughs in the field of Deep Learning and Computer Vision. Especially with the introduction of very deep Convolutional neural networks, these models helped achieve state-of-the-art results on problems such as image recognition and image classification.

So, over the years, the deep learning architectures became deeper and deeper (adding more layers) to solve more and more complex tasks which also helped in improving the performance of classification and recognition tasks and also making them robust.

But when we go on adding more layers to the neural network, it becomes very much difficult to train and the accuracy of the model starts saturating and then degrades also. Here comes the ResNet to rescue us from that scenario, and helps to resolve this problem.

What is ResNet?

Residual Network (ResNet) is one of the famous deep learning models that was introduced by Shaoqing Ren, Kaiming He, Jian Sun, and Xiangyu Zhang in their paper. The paper was named “Deep Residual Learning for Image Recognition” [1] in 2024. The ResNet model is one of the popular and most successful deep learning models so far.

Residual Blocks

The problem of training very deep networks has been relieved with the introduction of these Residual blocks and the ResNet model is made up of these blocks.

Source: ‘Deep Residual Learning for Image Recognition‘ paper

The problem of training very deep networks has been relieved with the introduction of these Residual blocks and the ResNet model is made up of these blocks.

In the above figure, the very first thing we can notice is that there is a direct connection that skips some layers of the model. This connection is called ’skip connection’ and is the heart of residual blocks. The output is not the same due to this skip connection. Without the skip connection, input ‘X gets multiplied by the weights of the layer followed by adding a bias term.

Then comes the activation function, f() and we get the output as H(x).

H(x)=f( wx + b ) or H(x)=f(x)

Now with the introduction of a new skip connection technique, the output is H(x) is changed to

H(x)=f(x)+x

But the dimension of the input may be varying from that of the output which might happen with a convolutional layer or pooling layers. Hence, this problem can be handled with these two approaches:

· Zero is padded with the skip connection to increase its dimensions.

· 1×1 convolutional layers are added to the input to match the dimensions. In such a case, the output is:

H(x)=f(x)+w1.x

Here an additional parameter w1 is added whereas no additional parameter is added when using the first approach.

These skip connections technique in ResNet solves the problem of vanishing gradient in deep CNNs by allowing alternate shortcut path for the gradient to flow through. Also, the skip connection helps if any layer hurts the performance of architecture, then it will be skipped by regularization.

Architecture of ResNet

There is a 34-layer plain network in the architecture that is inspired by VGG-19 in which the shortcut connection or the skip connections are added. These skip connections or the residual blocks then convert the architecture into the residual network as shown in the figure below.

Source: ‘Deep Residual Learning for Image Recognition‘ paper

Using ResNet with Keras:

Keras is an open-source deep-learning library capable of running on top of TensorFlow. Keras Applications provides the following ResNet versions.

– ResNet50

– ResNet50V2

– ResNet101

– ResNet101V2

– ResNet152

– ResNet152V2

Let’s Build ResNet from scratch:

  Source: ‘Deep Residual Learning for Image Recognition‘ paper

Let us keep the above image as a reference and start building the network.

ResNet architecture uses the CNN blocks multiple times, so let us create a class for CNN block, which takes input channels and output channels. There is a batchnorm2d after each conv layer.

import torch import chúng tôi as nn class block(nn.Module): def __init__( self, in_channels, intermediate_channels, identity_downsample=None, stride=1 ): super(block, self).__init__() self.expansion = 4 self.conv1 = nn.Conv2d( in_channels, intermediate_channels, kernel_size=1, stride=1, padding=0, bias=False ) chúng tôi = nn.BatchNorm2d(intermediate_channels) self.conv2 = nn.Conv2d( intermediate_channels, intermediate_channels, kernel_size=3, stride=stride, padding=1, bias=False ) chúng tôi = nn.BatchNorm2d(intermediate_channels) self.conv3 = nn.Conv2d( intermediate_channels, intermediate_channels * self.expansion, kernel_size=1, stride=1, padding=0, bias=False ) chúng tôi = nn.BatchNorm2d(intermediate_channels * self.expansion) chúng tôi = nn.ReLU() self.identity_downsample = identity_downsample self.stride = stride def forward(self, x): identity = x.clone() x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.conv2(x) x = self.bn2(x) x = self.relu(x) x = self.conv3(x) x = self.bn3(x) if self.identity_downsample is not None: identity = self.identity_downsample(identity) x += identity x = self.relu(x) return x

Then create a ResNet class that takes the input of a number of blocks, layers, image channels, and the number of classes.

blocks, out channel, and strides.

class ResNet(nn.Module): def __init__(self, block, layers, image_channels, num_classes): super(ResNet, self).__init__() self.in_channels = 64 self.conv1 = nn.Conv2d(image_channels, 64, kernel_size=7, stride=2, padding=3, bias=False) chúng tôi = nn.BatchNorm2d(64) chúng tôi = nn.ReLU() self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) # Essentially the entire ResNet architecture are in these 4 lines below self.layer1 = self._make_layer( block, layers[0], intermediate_channels=64, stride=1 ) self.layer2 = self._make_layer( block, layers[1], intermediate_channels=128, stride=2 ) self.layer3 = self._make_layer( block, layers[2], intermediate_channels=256, stride=2 ) self.layer4 = self._make_layer( block, layers[3], intermediate_channels=512, stride=2 ) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512 * 4, num_classes) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.reshape(x.shape[0], -1) x = self.fc(x) return x def _make_layer(self, block, num_residual_blocks, intermediate_channels, stride): identity_downsample = None layers = [] # we need to adapt the Identity (skip connection) so it will be able to be added # to the layer that's ahead if stride != 1 or self.in_channels != intermediate_channels * 4: identity_downsample = nn.Sequential( nn.Conv2d( self.in_channels, intermediate_channels * 4, kernel_size=1, stride=stride, bias=False ), nn.BatchNorm2d(intermediate_channels * 4), ) layers.append( block(self.in_channels, intermediate_channels, identity_downsample, stride) ) # The expansion size is always 4 for ResNet 50,101,152 self.in_channels = intermediate_channels * 4 # For example for first resnet layer: 256 will be mapped to 64 as intermediate layer, # then finally back to 256. Hence no identity downsample is needed, since stride = 1, # and also same amount of channels. for i in range(num_residual_blocks - 1): layers.append(block(self.in_channels, intermediate_channels))

return nn.Sequential(*layers)

Then define different versions of ResNet

– For ResNet50 the layer sequence is [3, 4, 6, 3].

– For ResNet101 the layer sequence is [3, 4, 23, 3].

– For ResNet152 the layer sequence is [3, 8, 36, 3]. (refer the ‘Deep Residual Learning for Image Recognition‘ paper)

def ResNet50(img_channel=3, num_classes=1000): return ResNet(block, [3, 4, 6, 3], img_channel, num_classes) def ResNet101(img_channel=3, num_classes=1000): return ResNet(block, [3, 4, 23, 3], img_channel, num_classes) def ResNet152(img_channel=3, num_classes=1000): return ResNet(block, [3, 8, 36, 3], img_channel, num_classes)

Then write a small test code to check whether the model is working fine.

def test(): net = ResNet101(img_channel=3, num_classes=1000) device = "cuda" if torch.cuda.is_available() else "cpu" y = net(torch.randn(4, 3, 224, 224)).to(device) print(y.size()) test()

For the above test case the output should be:

The entire code can be accessed here:

Thank you.

Related

You're reading Build Resnet From Scratch With Python !

How To Learn Data Science From Scratch

Data science is the branch of science that deals with the collection and analysis of data to extract useful information from it. The data can be in any form, be it text, numbers, images, videos, etc. The results from this data can be used to train a machine to perform tasks on its own, or it can be used to forecast future outcomes. We are living in a world of data. More and more companies are turning towards data science, artificial intelligence and machine learning to get their job done. Learning data science can equip you for the future. This article will discuss how to learn data science from scratch.  

Why is data science important?

You are always surrounded by zettabytes and yottabytes of data. Data can be structured or unstructured. It is important for businesses to use this data. This data can be used to:

visualize trends

reduce costs

launch new products and services

extend business to different demographics

Your Learning Plan 1. Technical Skills

We will start with technical skills. Understanding technical skills will help you understand the algorithms with mathematics better. Python is the most widely used language in data science. There is a whole bunch of developers working hard to develop libraries in Python to make your data science experience smooth and easy. However, you should also polish your skills in R programming. 1.1. Python Fundamentals Before using Python to solve data science problems, you must be able to understand its fundamentals. There are lots of free courses available online to learn Python. You can also use YouTube to learn Python for free. You can refer to the book Python for Dummies for more help. 1.2. Data Analysis using Python Now we can move towards using Python in data analysis. I would suggest chúng tôi as the starting point. It is free, crisp and easy to understand. If you want a more in-depth knowledge of the topic, you can always buy the premium subscription. The price is somewhere between $24 and $49 depending on the type of package you opt for. It is always useful to spend some money for your future. 1.3. Machine Learning using Python The premium package for chúng tôi already equips you with the fundamentals of ML. However, there are a plethora of free resources online to acquire skills in ML. Make sure whichever course you follow, it deals with scikit-learn. Scikit-learn is the most widely used Python library for data science and machine learning. At this stage, you can also start attending workshops and seminars. They will help you gain practical knowledge on this subject. 1.4. SQL In data science, you always deal with data. This is where SQL comes into the picture. SQL helps you organize and access data. You can use an online learning platform like Codeacademy or YouTube to learn SQL for free. 1.5. R Programming It is always a good idea to diversify your skills. You don’t need to depend on Python alone. You can use Codeacademy or YouTube to learn the basics of R. It is a free course. If you can spend extra money, then I would say opt for the pro package for Codeacademy. It may cost you somewhere around $31 to $15  

2. Theory

While you are learning about the technical aspects, you will encounter theory too. Don’t make the mistake of ignoring the theory. Learn the theory alongside technicalities. Suppose you have learned an algorithm. It’s fine. Now is the time to learn more about it by diving deep into its theory. The Khan Academy has all the theory you will need throughout this course.  

 3. Math

Maths is an important part of data science. 3.1. Calculus Calculus is an integral part of this curriculum.  Every machine learning algorithm makes use of calculus. So, it becomes inevitable to have a good grip on this topic. The topics you need to study under calculus are: 3.1.1. Derivatives

Derivative of a function

Geometric definition

Nonlinear function

3.1.2. Chain Rule

Composite functions

Multiple functions

Derivatives of composite functions

3.1.3. Gradients

Directional derivatives

Integrals

Partial derivatives

  3.2. Linear Algebra Linear algebra is another important topic you need to master to understand data science. Linear algebra is used across all three domains – machine learning, artificial intelligence as well as data science. The topics you need to study under linear algebra are: 3.2.1. Vectors and spaces

Vectors

Linear dependence and independence

Linear combinations

The vector dot and cross product

3.2.2. Matrix transformations

Multiplication of a matrix

Transpose of a matrix

Linear transformations

Inverse function

3.3. Statistics Statistics are needed to sort and use the data. Proper organization and maintenance of data need the use of statistics. Here are the important topics under this umbrella: 3.3.1. Descriptive Statistics

Types of distribution

Central tendency

Summarization of data

Dependence measure

3.3.2. Experiment Design

Sampling

Randomness

Probability

Hypothesis testing

Significance Testing

3.2.3. Machine Learning

Regression

Classification

Inference about slope

4. Practical experience

Now you are ready to try your hands in some real-world data science problem. Enroll in an internship or contribute in some open-source project. This step will help you enrich your skills.  

Data Science Lifecycle

Every data science project goes through a lifecycle. Here we describe each of the phases of the cycle in detail.

Discovery: In this phase, you define the problem to be solved. You also make a report regarding the manpower, skills and technology available to you. This is the step where you can approve or reject a project.

Data Preparation: Here you will need to prepare an analytical sandbox that will be used in the remaining part of the project. You also need to condition the data before modeling. First, you prepare the analytical sandbox, then prepare ETLT, then data conditioning and finally visualization.

Model Planning: Here you will need to draw a relationship among the variables. You need to understand the data. These relationships will be the basis of the algorithm used in your project. You can use any of the following model planning tools: SAS/ACCESS, SQL or R.

Model Building: Here you need to develop data sets to train your system. You have to make a choice between your existing tools or a new more robust environment. Various model-building tools available in the market are SAS Enterprise Manager, MATLAB, WEKA, Statistica, Alpine Miner, etc.

Operationalize: In this step, you deliver a final report, code of the system and technical briefings. You also try to test the system in pilot mode to ascertain how it functions before deploying it in the real world.

Communicate Results: Now your work is done. In this step, you communicate with the stakeholders, whether or not your system complies with all their requirements ascertained in step 1. If they accept the system, your project is a success, or else it is a failure.

Data Science Components

Data: Data is the basic building block of data science. Data is of two types: structured data (is basically in tabular form) and unstructured data (images, emails, videos, PDF files, etc.)

Programming: R and Python are the most widely used programming language in data science. Programming is the way to maintain, organize and analyze data.

Mathematics: In the field of mathematics, you don’t need to know everything. Statistics and probability are mostly used in data science. Without the proper knowledge of mathematics and probability, you will most probably make incorrect decisions and misinterpret data.

Machine Learning: As a data scientist, you will be working with machine learning algorithms on a daily basis. Regression, classification, etc. are some of the well-known machine learning algorithms.

Big Data: In this era, raw data is compared with crude oil. Like we refine crude oil and use it to drive automobiles, similarly, the raw data must be refined and used to drive technology. Remember, raw data is of no use. It is the refined data that is used in all machine learning algorithms.

Now you know everything about data science. Now you have a clear road map on how to master data science. Remember this will not be an easy career. Data science is a very young market. Breakthrough developments are taking place almost every day. It is your job to keep yourself acquainted with all the happenings in the market. A little effort and a bright future await you.    

About Author:

Senior Data Scientist and Alumnus of IIM- C (Indian Institute of Management – Kolkata) with over 25 years of professional experience Specialized in Data Science, Artificial Intelligence, and Machine Learning. PMP Certified ITIL Expert certified APMG, PEOPLECERT and EXIN Accredited Trainer for all modules of ITIL till Expert Trained over 3000+ professionals across the globe Currently authoring a book on ITIL “ITIL MADE EASY” Conducted myriad Project management and ITIL Process consulting engagements in various organizations. Performed maturity assessment, gap analysis and Project management process definition and end to end implementation of Project management best practices   Name: Ram Tavva Designation: Director of ExcelR Solutions Location: Bangalore

How To Create An Ecommerce App From Scratch In 2023?

In the post-pandemic world, business operations have changed, and many organizations have experienced it. Even though eCommerce businesses have been popular before, they have become more famous in these two years.

As per reports, 62% who own a smartphone have purchased products using their device in the last six months. It indicates that this time is perfect to create an eCommerce app. If you wish to make an application, the following steps while designing the application are explained here. You must spend some time reading this blog.

1. Identifying The Niche: Create An eCommerce App

The first step to consider while building an eCommerce application is to decide on the niche. It would include several aspects like the products to sell, different categories to host in virtual stores, like accessories, clothing, etc. While identifying the niche, you must study the location as it helps determine the demand for specific products. When you create an eCommerce app as per the requirement, many people will prefer using it.

2. Planning Essential Features

Also read: 10 Best Saas Marketing Tools And Platforms For 2023

3. Balancing The Design Elements

Have you ever wanted to create your eCommerce website? Have you been browsing to find a simple guide that will explain the step-by-step process? At Aimprosoft, we know everything when it comes to designing an eCommerce site. Their blog section has in-depth information on the designing process of how to create an eCommerce app that will help you build an application. And here, we would like to inform you about the process in a more abbreviated version. Let’s start.

4. Platform Development And Selection

Also read: 10 Types of Developer Jobs: IT Jobs

5. Intensive Testing

Did you check out the architecture of an eCommerce application? Well, to develop an eCommerce application, you must interact with staff, customers, supply-chain operators, etc. When it comes to intensive testing, the essential focus areas are the workflow of the application, compatibility with other web browsers, the functionality of payment gateway, mobile responsiveness, SEO, etc. Opting for intensive testing will help in understanding which features are appropriate and which are not.

6. Push Notifications

Do not forget to include the push notification feature in your mobile application. It helps in notifying your customers instantly when you are introducing a new product. The added benefit of push notification is that it gives the necessary information that will help gain users’ attention.

7. Launch Of The Application And Support

Also read: 9 Best Cybersecurity Companies in the World

The Ending Note

In today’s market, you will come across hundreds of eCommerce applications. But to drive sales and increase the revenue earnings, it is essential to understand what users look for to create an eCommerce app. With that, you can design an application and create one to fulfill the business needs.

Python: Remove Punctuation From String, 4 Ways

One common task when working with text data in Python is removing punctuation marks from strings. Punctuation marks can be a nuisance when performing text analysis or natural language processing tasks, and removing them lets you clean up your data and focus on more important aspects, such as word frequency and sentiment analysis.

To remove punctuation from a string in Python, you can use the str.translate() method with the .maketrans() method. You can also use a “for” loop to construct a new string excluding punctuation marks.

In this guide, we’ll explore different methods and techniques to remove punctuation marks from strings in Python. This will help you choose the most suitable method for your project.

Let’s get into it!

Before we start writing the code for removing punctuation characters from a string, let’s quickly review some of the basic concepts of removing punctuation from a string in Python.

In Python, strings are sequences of characters enclosed in either single or double quotes. These characters can include letters, digits, special characters, or special symbols, such as punctuation marks.

You can use built-in functions and methods specifically designed for Python string manipulation to manipulate strings and perform substitutions to remove that random errant question mark.

Punctuation marks are symbols used in written language to clarify sentence structure. These marks include commas, periods, exclamation marks, question marks, colons, semicolons, and more.

You may want to eliminate punctuation marks from a string for processing purposes, such as text analysis or natural language processing.

The following code is an example of removing punctuation from a string:

import string def remove_punctuation(input_string): # Make a translation table that maps all punctuation characters to None translator = str.maketrans("", "", string.punctuation) # Apply the translation table to the input string result = input_string.translate(translator) return result # Sample string with punctuation marks and spaces text = "Hello, world! This is a sample string with punctuation. And spaces!" # Remove punctuation from the string output = remove_punctuation(text) # Print the original and modified strings print("Original string:", text) print("String without punctuation:", output)

In the example above, the remove_punctuation() function takes an input string and creates a translation table using str.maketrans() that maps every punctuation character to None.

Then it applies the translation table to the input string using the translate() method. The result is a new string without any punctuation marks.

This method also preserves spaces between words, making it suitable for processing text data in various applications.

The output of the above code is:

Original string: Hello, world! This is a sample string with punctuation. And spaces! String without punctuation: Hello world This is a sample string with punctuation And spaces

We’ll now discuss different ways to remove punctuation from a string in Python.

Specifically, we’ll cover the following methods:

Using replace() method

Using translate() method

Using regular expressions

Using “for” loop

You can use the replace() method in Python to remove punctuation marks from a string. This method takes an initial pattern and a final pattern as parameters.

It returns a resultant string where characters of the initial pattern are replaced by characters in the final pattern.

By replacing each punctuation string with an empty string, we can effectively remove them from original string.

This example demonstrates using replace() method to remove punctuation from string:

s = "Hello, World!" s = s.replace(",", "") s = s.replace("!", "") print(s)

When you run this code, it’ll output:

Hello World

In this example, replace() removes the comma and the exclamation mark from the original string “Hello, World!”.

You can also use the translate() method to remove unwanted characters from a string in Python.

This method requires a translation table, which you can create using the maketrans() method. With the translation table set to strip punctuation marks, you can then use the translate() method to get the desired output.

This example shows you how to use the translate() method:

import string def remove_punctuation(input_string): # Make a translator object to replace punctuation with none translator = str.maketrans('', '', string.punctuation) # Use the translator return input_string.translate(translator) test_string = "Hello, World! It's a beautiful day." print(remove_punctuation(test_string))

In this script, we are using the str.maketrans() function to create a translation table, which maps each punctuation character into None. Then we use str.translate() function to apply this table to our string.

When you run this code, it’ll print:

Hello World Its a beautiful day

A regular expression pattern can be a powerful tool for text processing.

You can use the Python re module to create empty strings with a pattern that matches all punctuation marks. You can then use the sub() method to replace them with an empty string.

The following example demonstrates this method:

import re import string def remove_punctuation(input_string): # Make a regular expression that matches all punctuation # Use the regex return regex.sub('', input_string) test_string = "Hello, World! It's a beautiful day." print(remove_punctuation(test_string))

In this script, we’re compiling a regular expression that matches all punctuation. The re.escape(string.punctuation) part is needed because some punctuation string marks have special meaning in regular expression.

The sub() function then replaces any matched pattern string (in this case, any punctuation) with an empty string.

This will output:

Hello World Its a beautiful day

You can use a simple for loop to remove punctuation marks from a string. By iterating through each character in the input string and checking if it is a punctuation, you can build a new string that excludes all the punctuation marks.

The following example shows you how to use for loop to remove punctuation:

import string def remove_punctuation(input_string): # Make an empty string to hold the new string without punctuation no_punct = "" for char in input_string: # If the character is not punctuation, add it to the new string if char not in string.punctuation: no_punct += char return no_punct test_string = "Hello, World! It's a beautiful day." print(remove_punctuation(test_string))

In this script, we’re creating a new string no_punct and filling it with each character from the original string that’s not a punctuation mark.

When you run this code, it’ll output:

Hello World Its a beautiful day

So, all punctuation in the test_string has been removed.

To learn more about string manipulation in Python, check the following video:

Understanding how to manipulate and clean text data, such as removing punctuation from a string, is an essential skill when programming in Python. This is because text data is everywhere, in web pages, files, databases, and more. Therefore, knowing how to process and transform this data is invaluable.

Consider tasks like data analysis, machine learning, or even web development. Often, you’ll find yourself working with text. Punctuation can cause issues in these scenarios. It might skew your analysis or confuse your machine learning algorithm.

By learning how to remove unnecessary symbols, you’re equipping yourself with a powerful tool to prepare your data better. You make your text data cleaner, more uniform, and ready for further processing!

In this section, you’ll find some frequently asked questions you might have when removing punctuation from a string.

The “best” method depends on your specific situation and needs.

For simple, one-off replacements, str.replace() could be sufficient.

If you need to remove all punctuation, using str.translate() with str.maketrans(), or re.sub() with a regular expression, are more efficient.

A for loop can also be used for more control over the process.

Yes, context is key. In some natural language processing tasks, punctuation can provide valuable information about sentence structure or sentiment (like the difference between a statement and a question).

Yes, you can replace punctuation with any string you want, such as a space, using the techniques discussed in this article.

Yes, the re module works with any Unicode characters, so it can be used with text in any language.

However, the string.punctuation constant only includes punctuation commonly used in the English language.

You can customize all the methods mentioned to only remove specific punctuation marks.

For example, with str.replace(), you can specify the exact punctuation you want to replace. In the case of str.translate(), re.sub(), and the for loop method, you would adjust the punctuation list to include only the marks you want to remove.

Python – Drop Multiple Levels From A Multi

To drop multiple levels from a multi-level column index, use the columns.droplevel() repeatedly. We have used the Multiindex.from_tuples() is used to create indexes column-wise.

At first, create indexes column-wise −

items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")])

Next, create a multiindex array and form a multiindex dataframe −

arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])] # forming multiindex dataframe dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items)

Label the index −

dataFrame.index.names = ['level 0', 'level 1']

Drop a level at index 0 −

dataFrame.columns = dataFrame.columns.droplevel(0)

We deleted a level at 0 index. After deleting, the level 1 is now level 0. To delete another level, just use the above again i.e.

dataFrame.columns = dataFrame.columns.droplevel(0)

Following is the code

Example import numpy as np import pandas as pd items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")]) # multiindex array arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])] # forming multiindex dataframe dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items) # labelling index dataFrame.index.names = ['one', 'two'] print"DataFrame...n",dataFrame print"nDropping a level...n"; dataFrame.columns = dataFrame.columns.droplevel(0) print"Updated DataFrame..n",dataFrame print"nDropping another level...n"; dataFrame.columns = dataFrame.columns.droplevel(0) print"Updated DataFrame..n",dataFrame Output

This will produce the following output −

DataFrame...                      Col 1      Col 2      Col 3                      Col 1      Col 2      Col 3                      Col 1      Col 2      Col 3 one      two car      valueA   0.425077   0.020606   1.148156          valueB  -1.720355   0.502863   1.184753          valueC   0.373106   1.300935  -0.128404 bike     valueA  -0.648708   0.944725   0.593327          valueB  -0.613921  -0.238730  -0.218448          valueC   0.313042  -0.628065   0.910935 truck    valueA   0.286377   0.478067  -1.000645          valueB   1.151793  -0.171433  -0.612346          valueC  -1.358061   0.735075   0.092700 Dropping a level... Updated DataFrame..                      Col 1      Col 2      Col 3                      Col 1      Col 2      Col 3 one      two car      valueA   0.425077   0.020606   1.148156          valueB  -1.720355   0.502863   1.184753          valueC   0.373106   1.300935  -0.128404 bike     valueA  -0.648708   0.944725   0.593327          valueB  -0.613921  -0.238730  -0.218448          valueC   0.313042  -0.628065   0.910935 truck    valueA   0.286377   0.478067  -1.000645          valueB   1.151793  -0.171433  -0.612346          valueC  -1.358061   0.735075   0.092700 Dropping another level... Updated DataFrame..                      Col 1     Col 2     Col 3 one      two car      valueA   0.425077  0.020606  1.148156          valueB  -1.720355  0.502863  1.184753          valueC   0.373106  1.300935 -0.128404 bike     valueA -0.648708 0.944725 0.593327          valueB -0.613921 -0.238730 -0.218448         valueC 0.313042 -0.628065 0.910935 truck valueA 0.286377 0.478067 -1.000645 valueB 1.151793 -0.171433 -0.612346 valueC -1.358061 0.735075 0.092700

How To Build A Package From Source In Linux

Besides its open-source nature, customizability is one of the other reasons many users love Linux: you can modify and configure almost every file to meet your specific needs and style. This includes the ability to rebuild a package from source.

The ability to rebuild a package from the source can be beneficial to any Linux power user because it allows you to change packages, enable or disable a feature, or even apply custom modifications.

This article describes simple steps to rebuild a package from the source.

1. Enable Source Repositories

The first step before rebuilding a source package is to enable the source repositories for your distribution. Enabling the source repos allows you to search and download source packages using the default apt package manager.

In Debian systems, you can add the source packages by editing the “/etc/apt/sources.list” file.

For example, the following are the contents of the “sources.list” for Debian buster with the source packages enabled.

Once enabled, save the file and update the system using the command:

sudo

apt-get update

2. Fetch Source Package

After running the update process, you can then fetch the source package to modify your system.

For example, let us use the tar package. Start by creating a directory to store the source packages:

mkdir

apt-rebuilds

cd

apt-rebuilds

Next, download the source package using the command:

apt-get

source

tar

To view the files in the directory:

ls

-la

3. Check and Install Build Dependencies

The next step involves checking and installig the required build dependencies for the package you wish to rebuild.

Inside the directory location for the source packages, enter the command below to check for the unmet build dependencies.

sudo

dpkg-checkbuilddeps

The command will display all the unmet dependencies for the package. Although you can install them manually, a simpler method is to use apt to install the source packages.

To do this, use the command:

sudo

apt-get build-dep

tar

The command above will fetch the dependencies and install them for you.

4. Modify the Package

At this stage, you will want to make the changes to the package and enable or disable any feature you need. (Doing this is a broad aspect, and thus, we cannot possibly cover every change you can make for each package.)

Once you make all the changes and personal tweaks, recompile the source and save it with a different version number. You can do this using the command:

dch

--local

tar

Running this command will prompt you for your desired editor and launch the changelog for you to edit.

You can add a few lines to describe the changes made and change the version.

5. Build Package

The final step is to build the source package. Ensure you are in the source package directory and run the command:

dpkg-buildpackage

--force-sign

The command will initialize the build process using all the changes made in the above step.

Depending on the changes and the package to rebuild, the process can take a few minutes or longer.

6. Install Package

Once the build process completes, it will generate a binary package in the parent directory. To install, all you have to do is use the dpkg command:

sudo

dpkg

-i

*

.deb In closing

Building packages is a must-have skill for any Linux administrator and a good skill to have as a regular Linux user. Now that you know how to build a package from source, you can also learn how to easily rename files in Linux and how to use and edit the Hosts file in Linux.

John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.

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 Build Resnet From Scratch With Python ! 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!