Trending February 2024 # Complete Guide On How To Use Lightgbm In Python # Suggested March 2024 # Top 7 Popular

You are reading the article Complete Guide On How To Use Lightgbm In Python updated in February 2024 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 March 2024 Complete Guide On How To Use Lightgbm In Python

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


A Gradient Boosting Decision tree or a GBDT is a very popular machine learning algorithm that has effective implementations like XGBoost and many optimization techniques are actually adopted from this algorithm. The efficiency and scalability of the model are not quite up to the mark when there are more features in the data. For this specific behavior, the major reason is that each feature should scan all the various data instances to make an estimate of all the possible split points which is very time-consuming and tedious.

r EFB. So GOSS will actually exclude the significant portion of the data part which have small gradients and only use the remaining data to estimate the overall information gain. The data instances which have large gradients actually play a greater role for computation on information gain. GOSS can get accurate results with a significant information gain despite using a smaller dataset than other models.

With the EFB, It puts the mutually exclusive features along with nothing but it will rarely take any non-zero value at the same time to reduce the number of features. This impacts the overall result for an effective feature elimination without compromising the accuracy of the split point.

By combining the two changes, it will fasten up the training time of any algorithm by 20 times. So LGBM can be thought of as gradient boosting trees with the combination for EFB and GOSS. You can access their official documentation here.

The main features of the LGBM model are as follows :

Higher accuracy and a faster training speed.

Low memory utilization

Comparatively better accuracy than other boosting algorithms and handles overfitting much better while working with smaller datasets.

Parallel Learning support.

Compatible with both small and large datasets

Demystifying the Maths behind LGBM

We use a concept known as verdict trees so that we can cram a function like for example, from the input space X, towards the gradient space G. A training set with the instances like x1,x2 and up to xn is assumed where each element is a vector with s dimensions in the space X. In each of the restatements of a gradient boosting, all the negative gradients of a loss function with respect towards the output model are denoted as g1, g2, and up to gn. The decision tree actually divides each and every node at the most revealing feature, it also gives rise to the largest evidence gain. In this type of model, the data improvement can be measured by the variance after segregating. It can be represented by the following formula :


Explanation, Let O be a training dataset on a fixed node of a decision tree and then the variance gain of dividing measure j at a point d for a node is defined as :

Gradient One-Sided Sampling or GOSS utilizes every instance with a larger gradient and does the task of random sampling on the various instances with the small gradients. The training dataset is given by the notation of O for each particular node of the Decision tree. The variance gain of j or the dividing measure at the point d for the node is given by :

This is achieved by the method of GOSS in LightGBM models.

Coding an LGBM in Python

The LGBM model can be installed by using the Python pip function and the command is “pip install lightbgm” LGBM also has a custom API support in it and using it we can implement both Classifier and regression algorithms where both the models operate in a similar fashion. The Dataset used here is of the Titanic Passengers which will be used in the below code and can be found in my drive at this location.

Code :

Python Code:

Output :

Here we can see that there are 8 columns out of which the passenger ID will be dropped and the embarked will be finally chosen as a target variable for the following classification challenge.

Loading the variables: # To define the input and output feature x = data.drop(['Embarked','PassengerId'],axis=1) y = data.Embarked # train and test split x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.33,random_state=42) Loading and fitting the model:

The initial process of initializing a model is very similar to a normal model initializing and the main difference is that we will get much more parameter settings adjustments while we are initializing the model. We will define the max_depth, learning rate and random state in the following code. In the fit model, we have passed eval_matrix and eval_set to evaluate the model during training itself.

Code : model = lgb.LGBMClassifier(learning_rate=0.09,max_depth=-5,random_state=42),y_train,eval_set=[(x_test,y_test),(x_train,y_train)], verbose=20,eval_metric='logloss') Output:

Since our model has very low instances, we need to first check for overfitting with the following code and then we will proceed for the next few steps :

Code : print('Training accuracy {:.4f}'.format(model.score(x_train,y_train))) print('Testing accuracy {:.4f}'.format(model.score(x_test,y_test))) Output : Training accuracy 0.9647 Testing accuracy 0.8163

As we can clearly see that there is absolutely no significant difference between both the accuracies and hence the model has made an estimation that is quite accurate.

LGBM also comes with additional plotting functions like plotting the various feature importance, metric evaluation and the tree plot.

Code : lgb.plot_importance(model)

Output :

If you do not mention the eval_set during the fitment, then you will actually get an error while plotting the metric evaluation

Code : lgb.plot_metric(model) Output

And as you can clearly see here, the validation curve will tend to increase after it has crossed the 100th evaluation. This can be totally fixed by tuning and setting the hyperparameters of the model. We can also plot the tree using a function.




Now we will plot a few metrics by using the sklearn library

Code :


Output :

Code :


Output :

Now as we can clearly see from the confusion matrix combined with the classification report, the model is struggling to predict class 1 because of the few instances that we have but if we compare the same result with the other various ensemble algorithm, then LGBM performs the best. We can also perform the same process for the regressor model but there we need to change the estimator to the LGBMRegressor()

End Notes:

From this article, we can see and understand how to use an LGBM model and how it can tackle the problem by using a GODD and EFB and then we implemented it for a real-life classification problem and the overall process is also very similar to the other ML algorithms. The in-built plotting functionality also makes the library much more attractive and reduces the overall effort for the evaluation side.

Stay Safe and get vaccinated everyone.

Arnab Mondal

Collab Notebook Link :

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


You're reading Complete Guide On How To Use Lightgbm In Python

How To Use Chatgpt: Complete Guide

Open AI’s ChatGPT was the most popular invention of 2023. It is an AI chatbot that interacts with users in a human-like language using the GPT-3.5 LLM. The platform gained huge traction soon after its release and has a massive user base of over 100 million.

ChatGPT offers an intuitive interface with numerous functionalities making it easy to use for everyone. This intelligent app can perform tons of text-generation tasks within a few minutes. However, to get the best results, it’s necessary to know the right way to use ChatGPT.

This descriptive guide shares several methods to use ChatGPT to accomplish your tasks. 

How to use ChatGPT

ChatGPT is not an app or program to install on your device. It is an online site accessible from ChatGPT’s official website. It’s necessary to create an account to explore this platform. After setting up the account, you can instruct ChatGPT to generate content according to your requirement using appropriate prompts.

Don’t worry if the above process overwhelms you. The steps below will help you create your ChatGPT account to use the platform immediately.

1. Create an OpenAI account

The first step to using ChatGPT is to create an Open AI account. Accessing ChatGPT without an account is forbidden. To create your account, follow the steps below:

Step 4: Verify your account from the email sent to your email address.

Step 6: Enter your Phone number and verify it. Your account is ready for use after successful verification.

Note: You can also select to sign up using your Microsoft or Google account at step 2.

2. Accept ChatGPT terms

3. Login

Now that your account is ready, you can log in and use it. To sign into your account, follow the steps below:

Step 2: Enter your email address and Password.

4. Start writing

For example, write a poem on the sun and moon.

How to use ChatGPT as a developer?

Aside from its generic uses, ChatGPT offers extensive functionalities for developers. Developers can integrate this platform into other apps, use it for debugging or writing codes, or learn to code.

This section illustrates how developers can use ChatGPT for various purposes:

Explain why a piece of code isn’t working

ChatGPT can find bugs in the program and provide solutions to fix them. The app can detect bugs the coder may take hours to figure out and share details to solve.

For instance, the app can read code and provide suggestions to improve and enhance its efficiency. It can tell whether you are missing a certain variable, using incorrect syntax, or running the code incorrectly.

Explain what a piece of code means

If you want to know the program’s purpose or understand it, ChatGPT can describe it. The platform analyzes and explains the code in a simple, understandable language. Developers can save time and effort by instructing this tool to explain the code.

The platform generates accurate and informative explanations for simple and complex codes. You can also use it to explain a program of a hundred lines.

Rewrite the code using the specified language

Converting a program written in one computer language to another requires a deep understanding of both languages and the program. It also consumes a lot of time and effort. Luckily, ChatGPT can solve this tedious task within a few minutes.

ChatGPT can instantly transform any program from one language to another. Simply enter the program in ChatGPT and specify the language into which you want it to be transformed. The app will soon produce the results on your screen.

Code an entire software program

ChatGPT can also be used to code an software program. The app can write algorithms and generate code for the entire software. However, it is not recommended to use ChatGPT to create software since the app is in its development phase and prone to errors. Instead, developers can use ChatGPT as a companion while writing programs for software.

Generate regular expressions (regex)

ChatGPT can be used to generate regular expressions. Developers can copy and paste these regular expressions into a regex-enabled text editor or programming language. The app can generate regex to match email addresses and characters in a string, find or replace characters, etc.

Change the CSS of a line of code

Developers can use ChatGPT to modify some lines within a CSS code. For example, use it to change the font color, font size, background color, etc., within a CSS program.

Change the HTML of a line of code

ChatGPT can also modify an HTML code. You have to tell it which line to modify, and it will make the necessary changes. It can also add or remove some sections from the code.

7 Ways to Use Chat GPT (Ethically)

After signing into your ChatGPT account, you can use this app to perform several text generation tasks. Different industries have started using ChatGPT to reduce their time and effort with its AI capabilities. Below are some ethical ways of using ChatGPT.

Organizing travel

ChatGPT cannot access the Internet. So, you cannot use it to book tickets or hotels. However, you can use ChatGPT to create a travel schedule. The app can develop an itinerary for your next trip. It can consider conditions like the number of days, budget, location, accommodation types, etc., while generating the itinerary.

Scheduling appointments or meetings

Organizing appointments or meetings is another tedious task. Finding free slots within your calendar or organizing a meeting with multiple team members requires a lot of coordination. Fortunately, you can save your efforts by instructing ChatGPT to refer to your calendar and organize a meeting at a time suitable for all.

Researching information

Until now, people used search engines like Google, Bing, Yahoo, etc., to surf the internet for information. But do you know that ChatGPT can also help in researching information?

ChatGPT can answer almost all questions regarding any topic. It can also quote helpful information and provide references for a topic. It refers to YouTube videos, online journals, websites, etc., to provide accurate responses.

The only catch is that ChatGPT cannot provide the latest information since it responds to questions based on its knowledge base, updated in 2023.

Translating text or speech

ChatGPT supports several languages, including English, French, German, Chinese, Japanese, Greek, etc. It can produce text in all these languages and translate a given text to another. It provides accurate translations for every supported language. So, you can use it as a companion to interact with your clients in different languages.

Creating content

The primary task of ChatGPT is generating content. This app can generate articles, blog posts, sales copies, scripts, essays, etc. It can also create outlines and brainstorms ideas in any niche. If you want to unleash your creativity, use ChatGPT for innovative content creation ideas.

Providing recommendations

Often you may be stuck while making decisions between several options. Luckily, ChatGPT can help you pick one option from multiple ones. It can also recommend products or services based on your preferences. For instance, you can ask ChatGPT to recommend a plant to grow at your house.

Finding cooking recipes

ChatGPT can help in the kitchen! While that can be strange, this AI tool can generate recipes. It can suggest recipes based on cuisines, available ingredients, diet types, or other preferences. It can generate many yummy recipe ideas for which you would have otherwise scratched your head! To test out this feature, try asking it to provide instructions for any recipe.


How to Use Chat GPT for Free?

ChatGPT is accessible for free with an Open AI account. You can create a new account or sign in to an existing one to explore the platform for free. However, the free version has some limitations.

How to Use Chat GPT for Work?

ChatGPT is a helpful tool for workspaces. It can be used to organize meetings, analyze reports, write emails, content generation, research information, translate content, and perform repetitive tasks. You can use ChatGPT for Work in almost every industry.

How to Use ChatGPT to Summarize an Article?

You can use ChatGPT to summarize articles and other content. ChatGPT can summarize articles or long pieces of text in different ways. It can write a short or long summary paragraph or summarize the content in bullet points. It can also explain or translate the summary or highlight key points.

How to Use ChatGPT for Business?

ChatGPT is a helpful tool for expanding a business. You can use ChatGPT for business to create marketing strategies, sales copies, form generation, solve legal issues, etc. You can also integrate a chatbot powered by ChatGPT to improve the customer experience. 

How to Use ChatGPT to Generate Images?

How to Use ChatGPT 3?

How to Use ChatGPT 4 for Free?

ChatGPT 4 is not available for free users. You can use ChatGPT 4 by upgrading to Plus at $20 monthly. Alternatively, you can use ChatGPT 4 for free on third-party platforms supporting the app. Sites like chúng tôi chúng tôi Hugging Face, and Microsoft Bing offer ChatGPT-4 for free.

How to Use ChatGPT Without Logging In?

Officially, ChatGPT requires users to create an account to access the platform. However, you can use ChatGPT without log in via third-party apps or browser extensions. But be careful. These methods have some limitations and may cause privacy and security concerns.

How to Use ChatGPT to Make Money?

The release of ChatGPT has generated several income streams for internet users. You can use ChatGPT to make money online in various ways. For example, use it for content creation, generating business ideas, translation services, blogging, app development, etc.

How to Use ChatGPT API?

ChatGPT offers API keys to integrate the platform into other apps. You can use ChatGPT API by generating API Keys from Open AI’s website and adding them to a relevant code. Currently, ChatGPT offers five free API keys.

How to Use ChatGPT on iPhone?

Iphone supports ChatGPT. So, you can officially use ChatGPT on your iPhone by visiting Open AI’s website and signing into your account. You can also create a new account if you don’t have an existing one.

How to Use ChatGPT on Mobile?

Unfortunately, ChatGPT is not available as a mobile application. You cannot download it to use on mobile devices. However, you can use ChatGPT on mobile phones by visiting Open AI’s website and logging into your account.

How to Use ChatGPT Without Phone Number?

ChatGPT asks for the user’s phone numbers while creating a new account. It also verifies this number before providing access to its users. However, to use ChatGPT without phone number, you can add a temporary number or VoIP while signing into the platform.

How to Use ChatGPT in Hong Kong?

ChatGPT is yet to be accessible in Hong Kong. You cannot officially access ChatGPT in Hong Kong but can use it via a VPN. You can connect to a server where ChatGPT is available and use it to access the website in Hong Kong via VPN.

How to Use ChatGPT to Write an Essay?

Students can use ChatGPT to write essays on almost every topic. The tool can help brainstorm ideas, create an outline, find sources, and draft the essay. It can also edit the essay to eliminate errors.

What can I use ChatGPT for?

ChatGPT is capable of accomplishing several tasks. You can use ChatGPT to generate essays, recipes, music lyrics, and poems, write and debug codes, translate content, tell jokes, and eliminate errors from a text.

How to use ChatGPT to write a cover letter?

ChatGPT can write a cover letter to help you get your dream job faster. It can draft an outstanding cover letter based on the job description and your profile. The app can also edit or modify an existing cover letter.

How to use ChatGPT to write a CV?

Job seekers can use ChatGPT to write a CV that makes them stand out. You can use ChatGPT to generate a resume from scratch, tailor your resume to meet certain job descriptions, modify some sections within your resume, or add new sections.

How to use ChatGPT to write code?

ChatGPT is a coder too! ChatGPT can write codes in different programming languages like JavaScript, Python, HTML, CSS, and C. It can also debug or explain programs.

Final Thoughts

As you see, ChatGPT can perform tasks beyond your imagination. This tool is helpful for every industry and can be used by individuals, freelancers, business corporations, etc. All you need is a ChatGPT account to start exploring the functionalities of ChatGPT. You can use ChatGPT for free or subscribe to ChatGPT Plus to access its full features, including GPT-4.

The Dir() Function In Python: A Complete Guide (With Examples)

In Python, the dir() function returns a list of the attributes and methods that belong to an object. This can be useful for exploring and discovering the capabilities of an object, as well as for debugging and testing.

For example, let’s list the attributes associated with a Python string:

# List the attributes and methods of a string dir('Hello')


The result is a complete list of all the methods and attributes associated with the str data type in Python. If you’ve dealt with strings before, you might see methods that look familiar to you, such as split, join, or upper.

This is a comprehensive guide to understanding what is the dir() function in Python. You will learn how to call the function on any object and more importantly how to analyze the output. Besides, you’ll learn when you’ll most likely need the dir() function.

What Is the dir() Function in Python?

The dir() function in Python lists the attributes and methods of an object. It takes an object as an argument and returns a list of strings, which are the names of its attributes and methods. Using the dir() function can be useful for inspecting objects to get a better understanding of what they do.

For example, you can use dir() to list the attributes of a built-in data type like a list or a dictionary, or you can use it on a custom class to see what’s in it. Besides, you can explore a poorly documented module or library with the dir() function.

Syntax and Parameters

The syntax of the dir() function in Python is as follows:


The dir() function takes a single parameter:

object: This is the object or type whose attributes you’re interested in. This can be any type of object in Python, such as a built-in data type like a list or a dictionary, or a user-defined class.

The function returns a list of strings that represent the methods and attributes of that object.


Let’s call the dir() function on a string ‘Hello’:

# List the attributes and methods of a string print(dir('Hello'))


The above result is a list that has all the attributes and methods of a string object.

For example, the upper() method can be used to convert a string to uppercase, and the find() method can be used to search for a substring within a string. Both these methods belong to the string class and are thus present in the output of the dir() function call.

Speaking of the variety of string methods in Python, make sure to read my complete guide to Strings in Python.

Notice that you can call the dir() function on any object in Python, not just built-in types like string. In other words, you can list the attributes your custom class has. You’ll find more examples of this later on.

What Are the Double Underscore Methods (E.g. ‘__add__’)?

In the previous example, you saw a bunch of methods that start with __, such as __add__, or __class__.

The double underscore methods that appear in the outputs of the dir() function are called “magic methods” or “dunder methods” in Python.

These methods are special methods that are defined by the Python language itself, and they are used to implement some of the built-in behavior of objects in Python.

For example, the __len__() method is called when you use the len() function on an object, and it returns the length of the object. The __add__() method is called when you use the + operator on two objects, and it returns the result of the operation.

The dunder methods are not meant to be called directly, but they are invoked automatically by the Python interpreter when certain operations are performed on an object.

Anyway, let’s go back to the topic.

Using dir() to Inspect Classes and Instances

The dir() function is useful if you want to inspect the attributes of classes and their instances.

When you use dir() with a class, lists the attributes defined in the class, including any inherited attributes and methods from the class’s superclasses.

In the earlier examples, you called the dir() function on a Python string instance. But you can call it on any other class instance, including custom classes created by you. More importantly, you can call the dir() function directly on a class instead of an instance of it.

Here’s an example of using dir() to inspect the attributes and methods of a custom class you’ve just created:

class MyClass: def __init__(self, x, y): self.x = x self.y = y def my_method(self): return self.x + self.y print(dir(MyClass))


In this example, there’s a custom class called MyClass that has an __init__() method and a my_method() method. Calling the dir() function returns a big list of strings. These are all the attributes of the custom class MyClass.

The very last name on the above list is ‘my_method‘. This is the method we defined in MyClass. But notice that the variables x and y aren’t there. This is because you called the dir() function on the class, not on an instance of it. Because of this, the variables x and y aren’t technically there. Those instance variables are created upon instantiating the objects.

As another example, let’s call dir() on a class instance instead of a class:

class MyClass: def __init__(self, x, y): self.x = x self.y = y def my_method(self): return self.x + self.y my_instance = MyClass(1, 2) print(dir(my_instance))


['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'my_method', 'x', 'y']

Again, toward the end of the list, you can see the attribute my_method. But this time, because you called the dir() on an instance, you will also see the variables x and y.

When Use the dir() Function?

The dir() function is useful in Python for finding out what attributes an object has. This is helpful when you are working with an object that you are not familiar with, or when you just want to see all of the available methods of an object.

Another common use for the dir() function is to find out what attributes and methods are available in a particular module or package.

For example, to see what attributes and methods are available in the math module, call dir() on it:

import math print(dir(math))

This prints out all the attributes and methods that are available in the math module. It’s helpful for quickly finding out what functions and other objects are available in the module, and might save you time when you are working with a module that you are not familiar with. This is especially true if the module is poorly documented (unlike the math module, though)


In conclusion, the dir() function is a valuable tool in Python for finding out what attributes and methods are available for a given object. It can be used to quickly explore new objects and modules, and can save time when you are working with an object or module that you are not familiar with.

Thanks for reading. Happy coding!

Read Also

How to Inspect a Python Object

Complete Guide On Pytorch Max In Detail

Introduction to PyTorch max

Web development, programming languages, Software testing & others

What is PyTorch max? How to use PyTorch max?


torch.max(specified input)


By using the above syntax, we can implement the max() function into deep learning; in this syntax, we use a torch with max function as shown here; we only pass the specified input that we want. Then, finally, we get the max element from the tensor.

Now let’s see a different example of the max() function, so we will get more details as follows.

First, we need to import the torch, as shown below.

import torch

Now we need to create the tensor by using the following statement as follows.

input = torch.randn([3, 4]) print(input) max_e = torch.max(input) print(max_e)


In the above example, we first import the torch; after that, we created a tensor by using the randn function as shown. After that, we pass the input value to the max() function and print the result. We illustrated the final output of the above program by using the following screenshot as follows.

PyTorch max over multiple dimensions

Now let’s see how we can use the max() function with multiple dimensions in Pytorch as follows.

Sometimes we need to get the maximum dimension as tensor instead of single; at that time, we can also use the max() function. We need to specify the dimension in multiple dimensions either by using an axis or dim variable. After execution, it returns the max element as well as max indices of the tensor.


max_ele, max_indice = torch.max(specified input tensor, dim)


In the above syntax, we use max() with two parameters such as max_ele and max_indices, as shown in the above syntax.

Example #1


import torch input = torch.randn([3, 4]) print(input) max_ele, max_indic = torch.max(input, dim=0) print(max_ele) print(max_indic)


By using the above example, we try to implement the multiple dimension with max() function; in this example, we first import the torch as shown; after that, we create a tensor by using the randn function as shown. After that, we pass the input value to the max() function and print the result. In this example, we assign the maximum dimension that is dim = 0 as shown. Finally, we just print the max element and max indices. We illustrated the final output of the above program by using the following screenshot as follows.

Now let’s same example of max() function with dimension value is 1 as follows.

Example #2


import torch input = torch.randn([3, 4]) print(input) max_ele, max_indic = torch.max(input, dim=1) print(max_ele) print(max_indic)


By using the above example, we try to implement the multiple dimension with max() function; in this example, we first import the torch as shown; after that, we create a tensor by using the randn function as shown. After that, we pass the input value to the max() function and print the result. In this example, we assign the maximum dimension that is dim = 1, as shown. Finally, we just print the max element and max indices. We illustrated the final output of the above program by using the following screenshot as follows.

Let’s consider we have two tensors, such P and Q, and they have the same dimension, and we need to compare this tensor and get the max element.

Now let’s see the example for better understanding as follows.

Example #3


import torch i_tensor1 = torch.randn([3, 4]) i_tensor2 = torch.randn([3, 4]) print("First Tensor:", i_tensor1) print("Second Tensor:", i_tensor2) max_ele = torch.max(i_tensor1, i_tensor2) print(max_ele)


In the above program, first, we define the two tensors that are tensor1 and tensor2 as shown; here, the difference is only in the max() function; here, we need to send two tensors to the max() function. Then, we illustrated the final output of the above program by using the following screenshot as follows.

PyTorch max performance

Now let’s see the performance of the max() function as follows.

Using the max() function can speed up the operation because we can easily, or we can say that efficiently find out the max element from the input tensor as per requirement. Using the max() function, we can increase deep learning performance, or we can say that machine learning is as per our requirement.


We hope from this article you learn more about the Pytorch max. From the above article, we have taken in the essential idea of the Pytorch max, and we also see the representation and example of Pytorch max. From this article, we learned how and when we use the Pytorch max.

Recommended Articles

We hope that this EDUCBA information on “PyTorch max” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

Complete Guide On Tensorflow Federated

Introduction to TensorFlow Federated

Hadoop, Data Science, Statistics & others

This article will try to understand tensorflow federated, how we can use it, its Model, characteristics, computation API, and finally conclude our view.

What is TensorFlow federated?

The framework helps you perform machine learning on completely decentralized data. We train the models that are shared globally and include the clients participating in placing their data for training locally. One of the examples which will be helpful to understand where we make the use of tensorflow federation is for the training of keyboard prediction model on mobile phones and making sure at the same time that the sensitive and secured data is not being uploaded on the data server.

The developers can use and include Federated learning algorithms in their data and models. At the same time, the novel algorithms are available and open for any experimentation for the developers. Therefore, the people performing the research on this can find ample examples and the point where they can start for various experiment topics. Federated analytics is the computation that is non-learning based and can be implemented using the interface of tensorflow federated.

How and where to use TensorFlow federated?

We can make the use of federated learning in various ways that include –

By using FC API, design and create new federated learning algorithms.

Assisting the development and optimization of computation structures that are generated.

Apply the APIs of the federated learning to the models of TensorFlow that exist currently.

Integrate the Tensorflow Federated framework with other environments of development.

You can make use of it by following the below steps –

Installation of TFF –

This can be done by opening the terminal or command prompt and typing in the following command for execution –

pip install tensorflow-federated –upgrade

Create a notebook and import the package and other dependencies.

Prepare the dataset for simulation.

The data should be of NIST or MNIST format and is by default provided when you go for creating a leaf project.

Make the use of federated data to train the Model.

After that, you can train the Model and make it aware of various functionalities that it should perform and be aware as you do with any of the TensorFlow models.

Print the summarized information about the implementation of tensorflow federated.

Finally, you can print out the machine learning tensorflow federated model results.

TensorFlow federated Model

The two models used in TensorFlow federated FL API are tff.learning.Model and create_keras_model().

TensorFlow federated characteristics

The main characteristics are listed below –

Effort saving – Whenever any developer approaches to create a learning system of federated, the pain points where the developers mostly face the problem are targeted here, and the platform of tensorflow federated is designed keeping the mitigations of those points in mind for the convenience of developers. The challenges faced by most of the developers include local and global communication perspectives, logic interleaving of various types, and execution and construction order tension.

Architecture agnostic – It can compile the whole code and provide the representation of the same in an abstract way, which facilitates the developer to deploy its Model acrModel diverse environment.

Availability of many extensions – Quantization, compression, and differential privacy are some of the extensions available in Tensorflow Federated.

TensorFlow federated Computation API

There are two types of computation APIs, which are described below –

Federated Core API, also known as FC –

The low-level interface used at the system’s core part is included in this API. Federated algorithms can be concisely expressed along with the combination of TensorFlow using this API. It also consists of a functional programming environment that is typed strongly and includes the distributed operators for communication. This API layer is the base over which we have created the building of federated learning.

Federated Learning API, referred to as FL –

The developers can include the evaluation and federated training models to the existing models of TensorFlow by using the high-level interfaces provided in this federated learning API layer.

Conclusion Recommended Articles

This is a guide to TensorFlow Federated. Here we discuss the Introduction, How and where to use TensorFlow federated, and Examples with code implementation. You may also have a look at the following articles to learn more –

Complete Guide To Mongodb Commands

Introduction to MongoDB Commands

MongoDB is a cross-platform, document-oriented, open-source database management system with high availability, performance, and scalability. MongoDB, a NoSQL database, finds extensive use in big data applications and other complex data processing tasks that do not align well with the relational database model. Instead of using the relational database notion of storing data in tables, MongoDB architecture is built on collections and documents. Here we discuss the MongoDB commands.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Why MongoDB Commands?

It can easily control global data, ensuring fast performance and compliance.

It provides a flexible data model. This goes with the case where the app needs to be built from scratch or the case of updating a single record.

Scaling the application ensures that there is no downtime.


MongoDB command uses a master-slave replication concept. To prevent database downtime, this replica feature is essential.

MongoDB command comes with the auto-sharding feature, which distributes data across multiple physical partitions known as shards. The result of which automatic load balancing happens.

It’s schema-less. Hence more efficient.

Basic of MongoDB Commands 1. Create database

In MongoDB use, DATABASE_NAME is used to create a database. If this name database doesn’t exist, it will get created, and else it will return the existing one.

To check the current database now:

By default, the MongoDB command comes with the database name “test.” Suppose you inserted a document without specifying the database; MongoDB will automatically store it in a “test” database.

2. Drop Database

If the database is not specified, it will delete the default database, “test.”

3. Create Collection

To create a collection, the MongoDB command used is: db.createCollection(name, options)

Here, the name is the Collection’s name & options are a document used to specify the Collection’s configuration. Though the “Options” parameter is optional, it’s good to provide it.

4. Drop Collection

5. Insert Document

To insert data into a database collection in MongoDB, you can use the “insert()” or “save()” method.

Here “mycol” is the collection name. If the Collection doesn’t exist, then the MongoDB command will create the database collection, which will be inserted.

6. Query Document

Querying Collection is done by the find() method.

As the find() method will show the findings in a non-structured way, a structured pretty() method is used to get the results.

Intermediate MongoDB Commands 1. Limit()

This MongoDB command limits the no. of records need to use in MongoDB. The argument of this function accepts only number types. The argument is the number of the Document that needs to be displayed.

2. Sort()

This is to the records of MongoDB. 1 & -1 are used to sort the documents. 1 is for ascending, whereas -1 is for descending.

3. Indexing is the concept that helps MongoDB to scan documents in an inefficient way

Advanced Commands of  MongoDB 1. Aggregate ()

This MongoDB command helps process the data, which returns the calculated result. This can group values from multiple documents together.

2. Replication

Replication in MongoDB is achieved using a replication set. A replica set is a group of MongoDB processes with the same dataset. Replica set provides:

High availability

Redundancy hence faults tolerant/disaster recovery.

In replica, one node is the primary node, and the rest are the secondary node. All write operations remain with the primary node.

Let’s see; you can convert a standalone MongoDB instance into a replica set.

Here are the steps for that:

Close is already running the MongoDB server.

Now Start the MongoDB server by specifying — replSet option.


3. Create & restore Backup

To create the Backup, the mongodump command is used. The server’s data will be dumped into a dump directory(/bin/dump/). Options are there to limit the data.

To restore a backup in MongoDB, you would use the “mongorestore” command.

4. Monitor Deployment

To check the status of all your running processes/instances, a mongostat command is helpful. It tracks and returns the counter of database operations. These counters include inserts, updates, queries, deletes, and cursors. This MongoDB command is beneficial as it shows your status about low running memory, some performance issues, etc.

You must go to your MongoDB installation bin directory and run mongostat.

Tips and Tricks to Use MongoDB Commands

Pre-allocate space: When you know your Document will grow to a certain size. This is an optimization technique in MongoDB. Insert a document and add a garbage field.

Try fetching data in a single query.

As MongoDB is, by default, case sensitive.


db.people.find({name: ‘Russell’}) &

db.people.find({name: ‘russell’}) are different.

While performing a search, it’s a good habit to use regex. Like:

db.people.find({name: /russell/i})

Prefer Odd No. of Replica Sets: Using replica sets is an easy way to add redundancy and enhance read performance. All nodes replicate the data, and it can be retrieved in case of a primary node failure. They vote amongst themselves and elect a primary node. Using the odd number of the replica will make voting more accessible in case of failure.

Secure MongoDB using a firewall: As MongoDB itself doesn’t provide any authentication, it’s better to secure it with a firewall and mapping it to the correct interface.

No joins: MongoDB, a NoSQL database, does not support joins. One must write multiple queries to retrieve data from more than two collections. Writing queries can become hectic if the schema is not well organized. This may result in the re-designing of the schema. It’s always better to spend some extra time to design a schema.


MongoDB commands are the best practice solution to maintain high availability, efficient and scalable operations, which is today’s business demand.

Recommended Articles

Update the detailed information about Complete Guide On How To Use Lightgbm In Python 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!