Trending November 2023 # Using The Replace() Method In Python # Suggested December 2023 # Top 15 Popular

You are reading the article Using The Replace() Method In Python updated in November 2023 on the website Minhminhbmm.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 December 2023 Using The Replace() Method In Python

The replace() method is a built-in function in Python that allows you to replace a specific substring or character in a string with another substring or character. It takes two arguments, the first being the substring or character you want to replace, and the second being the new substring or character you want to replace it with. The replace() method returns a new string with the replaced characters.

Syntax for the Replace Method string.replace(old, new[, count])

Where:

string: The string to be modified.

old: The substring or character to be replaced.

new: The new substring or character to replace the old one.

count (optional): The maximum number of occurrences to replace. If not specified, all occurrences will be replaced.

Examples of Python Replace Method

Let’s look at some examples to understand how the replace() method works in Python.

Example 1: Replace a Character in a String

In this example, we will replace a character in a string with another character.

string = 'Hello World!' new_string = string.replace('o', '0') print(new_string)

Output:

Hell0 W0rld!

In the above example, we replaced all occurrences of the character ‘o’ with ‘0’ in the string ‘Hello World!’.

Example 2: Replace a Substring in a String

In this example, we will replace a substring in a string with another substring.

string = 'Python is a popular programming language' new_string = string.replace('programming', 'scripting') print(new_string)

Output:

Python is a popular scripting language

In the above example, we replaced the substring ‘programming’ with ‘scripting’ in the string ‘Python is a popular programming language’.

Example 3: Replace Multiple Occurrences of a Character

In this example, we will replace multiple occurrences of a character in a string.

string = 'Mississippi' new_string = string.replace('s', 'z') print(new_string)

Output:

Mizzizzippi

In the above example, we replaced all occurrences of the character ‘s’ with ‘z’ in the string ‘Mississippi’.

Example 4: Replace a Character in a String with a Limit

In this example, we will replace a character in a string with a limit on the number of occurrences to replace.

string = 'Hello World!' new_string = string.replace('o', '0', 1) print(new_string)

Output:

Hell0 World!

In the above example, we replaced the first occurrence of the character ‘o’ with ‘0’ in the string ‘Hello World!’.

Example 5: Replace a Substring in a String with a Limit

In this example, we will replace a substring in a string with a limit on the number of occurrences to replace.

string = 'Python is a popular programming language' new_string = string.replace('language', 'tool', 1) print(new_string)

Output:

Python is a popular programming tool

In the above example, we replaced the first occurrence of the substring ‘language’ with ‘tool’ in the string ‘Python is a popular programming language’.

Conclusion

In this article, we discussed what the replace() method is, how it works, and how to use it in Python. The replace() method is a powerful function that allows you to replace specific characters or substrings in a string with new characters or substrings. We hope that this guide has helped you understand the replace() method better and how to use it in your Python projects.

Python’s replace() method is an important tool to have in your arsenal as a developer, whether you’re working with text manipulation, data cleaning, or any other string-related tasks. The examples provided in this tutorial demonstrate various use cases and should serve as a solid foundation for utilizing the replace() method effectively in your own Python projects.

You're reading Using The Replace() Method In Python

Optimizing Exploratory Data Analysis Using Functions In Python!

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

“The more, the merrier”.

It is a perfect saying for the amount of analysis done on any dataset.

As more and more opt for a career in Data Science, the more is the need to have a Fastrack way to guide each and everyone through the path. I learned python as the base to start and then gradually added skills that helped me grow in the data science domain.

In this post, I will be adding all the important steps and python functions you can use for Exploratory Data Analysis (EDA) on any dataset.

Okay, today’s plan is to run our fingers through data and figure out as much as we can but all in an optimized way. I am writing this article to share user-defined functions to help and shorten the EDA coding time.

The most important steps to follow in a project are:

Importing the data

Data validation Column datatype

Imputing null/missing values

Data exploration (EDA) Univariate

Bivariate

Multivariate

Feature Engineering

Transformation/Scaling

Model building (applying machine-learning algorithms) and tuning

Score calculation

Index Introduction

Univariate analysis

Bi-variate analysis

Multi-variate analysis

Helpful functions

Summary

Introduction

The most important and time-consuming part of any analytics problem is understanding the data. It is better to spend time studying the data rather than coding the same thing again and again.

The functions we are going to build today are pretty general and you can adapt them as per your requirement.

The pseudo-code for a user-defined function in python is:

Function Definition:

def func_name(parameters ): # function name and parameters "function_steps" function_commands return [return_value] Function call:

func_name(parameters) Function for Univariate analysis: Categorical:

Below function plots count plot for the feature being passed to the function.

def plot_cat(var, l=8,b=5): plt.figure(figsize = (l, b)) sns.countplot(df1[var], order = df1[var].value_counts().index) Continuous: For a simple distplot for continuous feature

def plot_cont(var, l=8,b=5): plt.figure(figsize=(l, b)) sns.distplot(df1[var]) plt.xlabel(var)

2. To view a detailed kde plot with all details:

# plot kde plot with median and Std values def plot_cont_kde(var, l=8,b=5): mini = df1[var].min() maxi = df1[var].max() ran = df1[var].max()-df1[var].min() mean = df1[var].mean() skew = df1[var].skew() kurt = df1[var].kurtosis() median = df1[var].median() st_dev = df1[var].std() points = mean-st_dev, mean+st_dev fig, axes=plt.subplots(1,2) sns.boxplot(data=df1,x=var, ax=axes[0]) sns.distplot(a=df1[var], ax=axes[1], color='#ff4125') sns.lineplot(points, [0,0], color = 'black', label = "std_dev") sns.scatterplot([mini, maxi], [0,0], color = 'orange', label = "min/max") sns.scatterplot([mean], [0], color = 'red', label = "mean") sns.scatterplot([median], [0], color = 'blue', label = "median") fig.set_size_inches(l,b) plt.title('std_dev = {}; kurtosis = {};nskew = {}; range = {}nmean = {}; median = {}'.format((round(points[0],2),round(points[1],2)), round(kurt,2),round(skew,2),(round(mini,2),round(maxi,2), round(ran,2)),round(mean,2), round(median,2))) Functions for Bi-variate analysis:

The bi-variate analysis is very helpful in finding out correlation patterns and to test our hypothesis. This will help us infer and build different features to feed into our model.

Categorical-Categorical:

def BVA_categorical_plot(data, tar, cat): '''take data and two categorical variables, calculates the chi2 significance between the two variables and prints the result with countplot & CrossTab ''' #isolating the variables data = data[[cat,tar]][:] #forming a crosstab table = pd.crosstab(data[tar],data[cat],) f_obs = np.array([table.iloc[0][:].values, table.iloc[1][:].values]) #performing chi2 test from scipy.stats import chi2_contingency chi, p, dof, expected = chi2_contingency(f_obs) #checking whether results are significant if p<0.05: sig = True else: sig = False #plotting grouped plot sns.countplot(x=cat, hue=tar, data=data) plt.title("p-value = {}n difference significant? = {}n".format(round(p,8),sig)) #plotting percent stacked bar plot #sns.catplot(ax, kind='stacked') ax1 = data.groupby(cat)[tar].value_counts(normalize=True).unstack() ax1.plot(kind='bar', stacked='True',title=str(ax1)) int_level = data[cat].value_counts()

Categorical-Continuous:

Here, I have used two functions, one to calculate z-value and the others to plot the relation between our features.

def TwoSampleZ(X1, X2, sigma1, sigma2, N1, N2): ''' function takes mean, standard dev., and no. of observations and returns: p-value calculated for 2-sampled Z-Test ''' from numpy import sqrt, abs, round from scipy.stats import norm ovr_sigma = sqrt(sigma1**2/N1 + sigma2**2/N2) z = (X1 - X2)/ovr_sigma pval = 2*(1 - norm.cdf(abs(z))) return pval def Bivariate_cont_cat(data, cont, cat, category): #creating 2 samples x1 = data[cont][data[cat]==category][:] # all categorical features x2 = data[cont][~(data[cat]==category)][:] # all continuous features #calculating descriptives n1, n2 = x1.shape[0], x2.shape[0] m1, m2 = x1.mean(), x2.mean() # calculates mean std1, std2 = x1.std(), x2.mean() # calculates standard deviation #calculating p-values z_p_val = TwoSampleZ(m1, m2, std1, std2, n1, n2) #table table = pd.pivot_table(data=data, values=cont, columns=cat, aggfunc = np.mean) #plotting plt.figure(figsize = (15,6), dpi=140) #barplot plt.subplot(1,2,1) sns.barplot([str(category),'not {}'.format(category)], [m1, m2]) plt.ylabel('mean {}'.format(cont)) plt.xlabel(cat) plt.title(' n z-test p-value = {}n {}'.format(z_p_val,table)) # boxplot plt.subplot(1,2,2) sns.boxplot(x=cat, y=cont, data=data) plt.title('categorical boxplot') Continuous-Continuous: #Defining a function to calculate correlation among columns: def corr_2_cols(Col1, Col2): res = pd.crosstab(df1[Col1],df1[Col2]) # res = df1.groupby([Col1, Col2]).size().unstack() res['perc'] = (res[res.columns[1]]/(res[res.columns[0]] + res[res.columns[1]])) return res Functions for Multi-variate analysis: def Grouped_Box_Plot(data, cont, cat1, cat2): #boxplot sns.boxplot(x=cat1, y=cont, hue=cat2, data=data, orient='v') plt.title('Boxplot') Summary

All the above functions help us cut the time and reduce redundancy in our code.

There are times when you will be in need to change the type of plot or add more details in the same. You can alter any function as per your requirement. Do note “Always follow a structure to complete your EDA”. I have shared the steps above you should follow while working with the dataset.

-Rohit

Related

Introduction To Redis Using Python

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

Introduction

Redis is a popular in-memory key-value data store, which is a type of NoSQL database. Redis is chiefly used as a cache database, but its application does not end there. You can find many articles explaining how Redis can be the all-in-one database for all kinds of applications. In this article, we will understand how to connect and use Redis in python.

Installing Redis on Windows

Redis is not officially supported on Windows. However, you can install Redis by setting up Windows Subsystem for Linux 2 and configuring it. Alternatively, you can run Redis on a container using Docker, which I will be covering in this article. The first step is to install Docker on your Windows machine. You can download Docker Desktop from here. The installation process is fairly simple and direct. Now that we have Docker on our machine, enter the following command on a command prompt to fetch the Redis image from Docker Hub, which can be used to build and run a container.

docker pull redis

Once this is done, the third step is to start a container using the Redis image that we downloaded earlier.

Please refer to the below picture for reference.

Congrats, you have now successfully started the Redis server on your machine.

Installing Redis-py

To connect and use Redis in python, we will be using a python module called redis-py. It can be installed by running the following command in the command prompt.

pip install redis

Now that we have everything ready, let’s get our hands dirty and dive into the programming part.

Using Redis-py

Before performing any CRUD operations, we first need to connect to the Redis server, which is very direct in redis-py. We need to set the decode_responses parameter as true so that we do not get the response in byte format.

import redis r = redis.Redis(decode_responses=True) r.ping() True

Now that we have connected to the Redis server, let’s start performing simple CRUD operations.

To set a key-value pair, we use the “set” function that accepts the key and the value as the parameter. Please note that the key should always be either of string data type or bytes.

r.set('hello', 'world')

To get the value for a specific key, we use the “get” function that accepts the key for which we want the value to be returned.

r.get('hello') world

To set multiple key-value pairs, we use the “mset” function that stands for multiple-set and accepts multiple key-value pairs as parameters.

data = { 'hello': 'world', 'lorem': 'ipsum' } r.mset(data) r.get('lorem') ipsum

To set a key-value pair where the value is of a set data type, we use the “sadd” function. The set data type contains only unique elements, unlike the list data type. Let’s now store a set of fruits without the fruits being repeated.

fruits = ["avocado", "strawberry", "strawberry", "mango", "orange"] r.sadd('fruits', *fruits)

To get all the values of fruits that we just stored, we can use the “smembers” function.

r.smembers('fruits') {'avocado', 'mango', 'orange', 'strawberry'}

Since we have seen how to store set data type values in Redis, let us now learn to store list data types. It can be done using the “lpush” function. Let us store a list of programming languages using this function.

programming_languages = ['python', 'C#', 'C++', 'C++', 'javascript'] r.lpush('languages', *programming_languages)

To get all the elements in the list, we use the “lrange” function that helps us traverse the elements in the list. The “-1” denotes that the function is expected to return all the elements, but if you want to return only the first 3 elements of the list, you can do so by just replacing “-1” with “2”. Note that it is not “3” because it will traverse till the index is three and return four elements in total.

r.lrange('languages', 0, -1) ['javascript', 'C++', 'C++', 'C#', 'python'] r.lrange('languages', 0, 2) ['javascript', 'C++', 'C++']

Now, let us try to save a nested object in Redis. For storing nested objects, we can make use of the “hset” function but it allows only one level of nesting.

r.hset('person', 'name', 'Ram') r.hget('person', 'name') Ram

If you want to store deeply nested objects with different data types, serialization techniques like using json or pickle can be used. Let’s see this in action to better understand.

import json personal_information = { 'name': 'Rahul', 'age': 20, 'address':{ 'house_no': 189, 'flat_name': 'Golden Flower', 'area': 'Guindy' }, 'languages_known': ['english', 'hindi', 'tamil'] } r.set('personal_information', json.dumps(personal_information))

To extract the information stored, we can directly use “get” function and then undo the stringification performed by json.

{'name': 'Rahul', 'age': 20, 'address': {'house_no': 189, 'flat_name': 'Golden Flower', 'area': 'Guindy'}, 'languages_known': ['english', 'hindi', 'tamil']}

Since it is an in-memory data store, it is important that old key-value pairs get deleted or rather expired to make room for storing new data. For this, Redis has the key expiry option available. Let us now try to store a key-value pair with an expiration time. We can make use of the “setex” function to set the expiry for a key-value pair. It accepts the TTL in seconds. If you want to set the TTL in milliseconds, you can use the “psetex” function.

r.setex('lorem', 10, 'ipsum') # 10 seconds r.psetex('hello', 10, 'world') # 10 milli seconds

To know how much time is remaining for a key to expire, we can use the “ttl” function which will return the time remaining in seconds. Similarly, the “pttl” function will return the same but in milliseconds. If the key has expired, the returned value will be negative.

r.ttl('lorem') 2 r.pttl('lorem') -2

Suppose we want to check if a key has expired or not, we can make use of the “exists” function that will return 1 if it is available and 0 if it has expired.

r.exists("lorem") 1 r.exists("hello") 0

To delete a key-value pair in Redis, we can use the “delete” function. It accepts the key that you want to delete.

r.delete('lorem') Conclusion

In this article, we discussed and covered the following:

What is Redis?

Installing docker on windows

Running it on a docker container

Connecting using redis-py

Performing simple CRUD operations using redis-py

That’s it for this article. Hope you enjoyed reading this article and learned something new. Thanks for reading and happy learning!

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

Related

How To Iterate Over Tuples In Dictionary Using Python

In this article, we will learn how to iterate over the tuples in Dictionary in python.

Methods Used

The following are the various methods used to accomplish this task −

Using Indexing

Using dictionary.values() Method

Using dictionary.items() Method

Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable.

Method 1: Using Indexing

len() function − The number of items in an object is returned by the len() chúng tôi len() function returns the number of characters in a string when the object is a string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

Create a variable to store the input dictionary containing the values as a tuple.

Print the whole tuple mapped with the specific key(here 10) of the input dictionary by using the key

Traverse through the tuple elements of the dictionary using in operator to print the elements of the tuple mapped with the key of the dictionary one by one.

Traverse till the length tuple elements of the dictionary using range() function to print the elements of the tuple mapped with the key of the dictionary.

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using Indexing −

# input dictionary inputDict = {10: ("Hello", "Tutorialspoint", "Python"), 20: ("dhoni", "virat", "pandya", "rohit sharma"), 30: ("this", "is", "a", "dictionary")} print("The Tuple mapped with the key 10 of the dictionary is:"), # printing the whole tuple mapped with the key 10 of the dictionary print(inputDict[10]) print() print("The Tuple mapped with the key 20 of the dictionary is:"), # printing the elements of the tuple mapped with # the key 20 of the dictionary one-by-one using for loop and in operator for i in inputDict[20]: # printing the corresponding element print(i), print() print("The Tuple mapped with the key 30 of the dictionary is:"), # printing the elements of the tuple mapped with # the key 20 of the dictionary one-by-one using for loop and range() for k in range(0, len(inputDict[30])): # accessing the corresponding element print(inputDict[30][k]) Output

On executing, the above program will generate the following output −

The Tuple mapped with the key 10 of the dictionary is: ('Hello', 'Tutorialspoint', 'Python') The Tuple mapped with the key 20 of the dictionary is: dhoni virat pandya rohit sharma The Tuple mapped with the key 30 of the dictionary is: this is a dictionary Method 2: Using values() Function

Use the dictionary.values() function to traverse through the tuples in a dictionary with the for loop.

values() function(the dict.values() method provides a view object that displays a list of all the values in the dictionary in order of insertion)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

Create a variable to store the input dictionary containing the values as a tuple.

Use the for loop, to traverse through the values(all tuples) of a dictionary using values() function.

Use another nested for loop, to traverse within each tuple again.

Print the corresponding element of the tuple

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using dictionary.values() function −

# input dictionary containing the values as a tuple inputDict = {10: ("Hello", "Tutorialspoint", "Python"), 20: ("dhoni", "virat", "pandya", "rohit sharma"), 30: ("this", "is", "a", "dictionary")} # traversing through the values(tuples) of a dictionary using values() function for p in inputDict.values(): # traversing within each tuple again using another nested for loop for q in p: # printing the elements of the corresponding tuple one by one print(q) print(" ") Output

On executing, the above program will generate the following output −

Hello Tutorialspoint Python dhoni virat pandya rohit sharma this is a dictionary Method 3: Using dictionary.items() Method

Use the dictionary.items() function to traverse through the tuples in a dictionary with the for loop.

The items() function returns a view object i.e, it contains the key-value pairs of the dictionary, as tuples in a list.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

Create a variable to store the input dictionary containing the values as a tuple.

Use the for loop, to traverse through the keys, and values(all tuples) of a dictionary using the items() function.

Traverse through all the elements of the tuple using another nested for loop.

Print the tuple elements.

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using dictionary.items() function −

# input dictionary containing the values as a tuple inputDict = {10: ("Hello", "Tutorialspoint", "Python"), 20: ("dhoni", "virat", "pandya", "rohit sharma"), 30: ("this", "is", "a", "dictionary")} # traversing through keys,values(tuples) of a dictionary using items() function for key, tuplevalues in inputDict.items(): # traversing within values of each tuple again using another nested for loop for value in tuplevalues: # printing the elements of the corresponding tuple elements one by one print(value) print(" ") Output

On executing, the above program will generate the following output −

Hello Tutorialspoint Python dhoni virat pandya rohit sharma this is a dictionary Conclusion

This article provided us with three different methods for iterating through tuples in the given dictionary. We learned how to use the values() function to get the dictionary’s values. Additionally, we learned how to use the items() method to traverse through the dictionary’s keys and values.

The Truth About The Rhythm Method

Fertility Awareness or the Rhythm Method

Pregnancy may be avoided using the fertility awareness technique (FAM) or the Rhythm method(RM).

Methods other than hormone replacement therapy are used to identify ovulation, so it’s essential to learn about your body and how it works by keeping a fertility diary and monitoring your monthly cycle.

The rhythm approach involves keeping a record of your menstrual cycles to anticipate when you will ovulate in the future. The RM takes the Rhythm Method one step further by paying close attention to the body to predict more accurately when ovulation will occur and avoid unwanted pregnancies.

Periodic abstinence is practiced in the RM during a woman’s most reproductive days. Other forms of birth control may be used when fertility is high.

Several combinations of tracking may increase or decrease RM’s efficiency. It’s okay to rely on natural birth control methods when various medical interventions may be used instead. Medication or other forms of medical intervention are required.

Nonetheless, FAM is one of the least effective methods of birth control. Yet, conscientious and self-aware individuals may find it an effective contraception method.

The Rhythm Method and Its Mechanisms

Abstaining entirely from sexual activity is the only technique to avoid becoming pregnant. Nonetheless, many couples use periodic abstinence as a method of contraception, which involves going without sexual activity during the woman’s reproductive days.

We know that the window of opportunity for the fertilization of an egg is relatively small. As a result, conception should be complex during certain phases of the menstrual cycle if the woman has not ovulated and is not likely to do so within the next week. So, the challenge is to predict when your ovulation will occur.

If a couple wants to conceive via the rhythm technique, they must abstain from sexual activity during the woman’s ovulation and for many days before and after. Because sperm survive in the female reproductive system far longer than eggs do.

An egg’s fertility can last over 1-2 days; however, sperm may survive [inside the female] for 5 to 7 days. She argues that a woman may become pregnant even if they had intercourse many days before ovulation since sperm can wait in the reproductive tube for an egg to drop.

How do you Keep Track of Your Period?

Each woman has a different menstrual cycle. You need to know exactly when your ovulation is to get the most out of FAM. Monitoring your ovulation cycle and conceiving a child may be done in several ways. Pregnancy prevention is more successful when many methods are used.

The following are examples of strategies used in fertility education −

A technique based on the calendar’s regular rhythms. Ovulation timing may be estimated using data from previous cycles. This is the least effective form of contraception when used alone. Whether your menstrual cycle is 26 days or less or 32 days or more, you should not use it.

The use of temperature. Here, you use a very sensitive basal thermometer to record your temperature each morning before you leave, a practice known as basal body temperature (BBT). During three to four days following ovulation, your BBT rises due to hormonal surges. The BBT method is not reliable enough for real-time ovulation forecasting. You may use it to reflect on your cycle and see whether you ovulated.

The hypothermal approach, in which the three techniques mentioned above are combined, maximizes the efficiency of FAM. Before relying on FAM as a method of birth control, you should keep track of your periods for at least six to twelve cycles.

Before choosing whether FAM suits you, seeing your doctor or enrolling in a fertility awareness course is a good idea. Know your body and its rhythms inside and out. It takes a lot of work and dedication to maintain a FAM.

To What Extent Does the Rhythm Method Work?

The success of RM is dependent on the following −

How do you keep track of your fertility, if at all?

How consistent is your period is

The accuracy with which you have charted your periods

How long of sexual abstinence did you observe around the time of ovulation

When used appropriately and regularly, RM has a high success rate for couples. In some instances, that may be challenging. Inconsistent or improper RM usage is associated with an annual pregnancy rate of up to 24 per 100 users.

Hence, it is one of the least effective forms of contraception that does not rely on abstinence.

How can the Rhythm Approach Help you?

There are a few pluses to RM. In certain cases, for instance, it

Has a low price tag

Uses safe practices

Needing no drugs

May be promptly reversed if you change your mind about becoming pregnant.

You and your spouse may get valuable insight into fertility via RM practice. This might assist if you decide to try for a baby in the future.

The Drawbacks of the Rhythm Approach

Sexually transmitted illnesses (STIs) include chlamydia, herpes, and HIV, all of which may be spread via Contact. Several more drawbacks exist as well.

The accuracy of this approach depends on your ability to maintain a regular menstrual cycle log for at least six months.

During your reproductive days, you must refrain from sexual activity or use an additional form of contraception.

A couple’s participation is required.

Compared to other methods of contraception, like condoms, diaphragms, and oral contraceptives, including tablets, patches, and injections, RM is less effective in preventing pregnancy. The key is knowing how to utilize it properly to achieve the desired results.

RM may not be the best solution for women who have painful or infrequent periods. Find out more about your birth control choices by seeing your doctor.

Implementing Particle Swarm Optimization Using Python

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

Introduction

The solution works by the logic where a candidate solution is worked upon to make it better by a specific measure of quality and then the operation is performed iteratively until a better solution is not available and thus reaching an optimal state itself. The language here will be Python and we will see a hands-on implementation of it using a python package “PySwarms”. We will cover the following topics here :

PSO: Particle Swarm Optimization

The inner workings

Variants or types of PSO

Implementing PSO with PySwarms

What is Particle Swarm Optimization (PSO)?

Image 1

The studies concluded that there was knowledge or wisdom sharing amongst animals that helped them get insight into a situation they haven’t faced before. In simpler words, finding food or evading prey becomes easy when all the birds of a flock share their observations and knowledge. The optimal solution hence becomes easier to pin down with swarm intelligence.

Inner working of Particle Swarm Optimization

Swarm behaviour can be of two types. First is the exploratory behaviour where animals seek their object in a larger solution space. Second is the exploitative behaviour where swarm intelligence helps them search a smaller more optimum area. PSOs and their parameters are mostly designed to find an optimum balance between exploration and exploitation. This ensures a good rate of convergence to the optimum.

2.1 Convergence of Particle Swarm Optimization

In a PSO convergence, it does not matter how a swarm will operate but the convergence to a specific local optimum when various personal bests which let’s say P or the best-known position of the swarm or also known as G will reach a local optimum for the problem. The ability which a PSO algorithm has so that it can exploit and explore can get affected by the topology structure. So for different structures, the algorithm can converge in a different manner because of the shape of the topology which is generally either a local ring or a global star. The topology structure defines the factors like searching, information sharing, speed, and even the direction in which each particle should follow.

Image 2

A PSO which has a global star structure where all particles are connected with each other has one benefit of the shortest average distance but a local ring structure where one particle is connected with the two nearest ones has the highest average distance in the whole swarm as shown in the above image. The figure on the left is the global star structure whereas the left one is the local ring structure. Each group consists of 16 particles and you can see why the average distance differs for both cases.

2.2 Adaptive mechanism

In an Adaptive mechanism, one can implement it with the required trade-off between convergence and divergence or exploitation and exploration. An APSO or Adaptive Particle Swarm Optimisation can outperform a regular PSO. With a faster convergence time, an APSO could also execute any global search across the whole search space.

Variants of Particle Swarm Optimization

PSO algorithms can be of different types, even simple ones. The particles and velocities can be initiated in different ways. Update the Swarm and then set values for Pi and G and so forth.

3.1 Gradient Particle Swarm Optimization

We can construct gradient-based PSOs by combining the efficiency of the PSO at exploring many local minimums with the gradient-based local search algorithms. This helps us to accurately calculate a local minimum. The PSO algorithm can be used for gradient-based PSO also where several local minima are discovered along with a deep basin of attraction for a deep local minimum. The deep minimum can then be confirmed using the local gradient-based search techniques.

3.2 Hybrid Particle Swarm Optimization

Image 4

4. Implementing Particle Swarm Optimization using PySwarms

As the name suggests, PySwarms is a python based tool that helps with swarm optimisation. Researchers, practitioners, and students alike use this tool in order to apply the PSO algorithm using a high-level interface. PySwarms is the best tool to integrate swarm optimisation with basic optimization.

This tool allows you to implement and use a number of many-particle swarm optimisation techniques. It’s also extremely user-friendly and adaptable to different projects. Your optimisation problem can also benefit from the support modules.

Now we will move on to using a global best optimizer using PySwarm’s functional API which is “pyswarms.single.GBestPSO” . We will plot it in both 2D and 3D manner.

You can install PySwarm by the following commands :

!pip install pyswarms

Now we will begin by importing some modules first :

Code :

# Import all the modules import numpy as np # Importing PySwarms import pyswarms as ps from pyswarms.utils.functions import single_obj as fx import matplotlib.pyplot as plt from pyswarms.utils.plotters import plot_contour, plot_surface from pyswarms.utils.plotters.formatters import Designer from pyswarms.utils.plotters.formatters import Mesher

After importing them all, we will begin with improving our sphere function, you can put any arbitrary settings to begin within our optimizer. The main three steps here are :

1. Set the hyperparameters to configure the swarm as a dictionary

2. To create the instance of an optimizer, pass the dictionary with all the relevant input parameters

3. The best cost and position in a variable can be saved by invoking the “optimize()” function.



4.1 Now on Visualizing the function

PySwarm already comes with various tools which will help you to visualise the behaviours of the swarm. These behaviours are constructed on the top of matplotlib which results in high customizable charts and user friendly too. There are also animation methods which are  “plot_contour()” and “plot_surface()” methods. These methods plot the particle in a 2D and 3D space respectively. To plot the sphere function, we need to add a specific function called mesh function into our swarm.

These help to visualise the particles graphically. We have already imported the “Mesher” class which helps us to achieve this. The “pyswarms.utils.plotters.formatters” module has many different formats so that you can customize the plots. Apart from Mesher, there is a “Designer” class that modifies the figure size, font size, and more along with an animator class for setting repeats and animation delays.

Code for 2D Plot :

m = Mesher(func=fx.sphere) animation = plot_contour(pos_history=optimizer.pos_history, mesher=m, mark=(0,0))

Output :

#Code for 3D Plot : # The preprocessing # Adjusting the figure d = Designer(limits=[(-1,1), (-1,1), (-0.1,1)], label=['x-axis', 'y-axis', 'z-axis']) animation3d = plot_surface(pos_history=pos_history_3d, # The cost_history that we computed mesher=m, designer=d, # Various Customizations mark=(0,0,0)) # Mark the minima

Output :

Note: Support for .gif will soon be added and hence youtube upload for now. The First 10 seconds of the video are essential, the rest is not much. The collab notebook link is given below :

Conclusion

So in this post, we have seen the theory and how a PSO works and explored the inner mechanism. We saw some of its variants and how it has been used in various domains and communities. Last but not least we also implemented a small python based swarm of our own. Feel free to text me if you face any issues or run into any problems.

Ankita Roy

Class of 2023 IT, Open to full-time/internships

[email protected]

Links to images :

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

Related

Update the detailed information about Using The Replace() Method In Python on the Minhminhbmm.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!