# Password Strength Checker with Machine Learning

## How to Create a Password Strength Checker?

A password strength checker works by understanding the combination of digits, letters, and special symbols you use in your password. It is created by training a machine learning model on a labelled dataset of different combinations of letters and special symbols people use in passwords. The model learns from data about what combinations of letters and symbols can be classified as a solid or weak password.

So to create an application to check the strength of passwords, we need to have a labelled dataset about different combinations of letters and symbols. I found a dataset on Kaggle to train a machine learning model to predict the strength of a password. We can use that data for this task. You can download the dataset from here.

In the section below, I will take you through how to use Machine Learning to create a password strength checker using Python.

## Password Strength Checker using Python

Let鈥檚 start by importing the necessary Python libraries and the dataset we need for creating a password strength checker:

```import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

```      password  strength
0     kzde5577         1
1     kino3434         1
2    visi7k1yr         1
3     megzy123         1
4  lamborghin1         1```

The dataset has two columns; password and strength. In the strength column:

1. 0 means: the password鈥檚 strength is weak;
2. 1 means: the password鈥檚 strength is medium;
3. 2 means: the password鈥檚 strength is strong;

Before moving forward, I will convert 0, 1, and 2 values in the strength column to weak, medium, and strong:

```data = data.dropna()
data["strength"] = data["strength"].map({0: "Weak",
1: "Medium",
2: "Strong"})
print(data.sample(5))```
```            password strength
476676       xupet0n     Weak
112569   cdm06690669   Medium
267402  bluerose1291   Medium
237407    2298409uur   Medium
336018       jejien8     Weak```

Now let鈥檚 move to train a machine learning model to predict the strength of the password. Before we start preparing the model, we need to tokenize the passwords as we need the model to learn from the combinations of digits, letters, and symbols to predict the password鈥檚 strength. So here鈥檚 how we can tokenize and split the data into training and test sets:

```def word(password):
character=[]
character.append(i)
return character

y = np.array(data["strength"])

tdif = TfidfVectorizer(tokenizer=word)
x = tdif.fit_transform(x)
xtrain, xtest, ytrain, ytest = train_test_split(x, y,
test_size=0.05,
random_state=42)```

Now here鈥檚 how to train a classification model to predict the strength of the password:

```model = RandomForestClassifier()
model.fit(xtrain, ytrain)
print(model.score(xtest, ytest))```
`0.956991816498417`

Now here鈥檚 how we can check the strength of a password using the trained model:

```import getpass
data = tdif.transform([user]).toarray()
output = model.predict(data)
print(output)```
```Enter Password: 路路路路路路路路路路
['Strong']```

### Summary

So this is how you can use machine learning to create a password鈥檚 strength checker using the Python programming language. A password strength checker works by understanding the combination of digits, letters, and special symbols you use in your password. I hope you liked this article on creating a password鈥檚 strength checker with Machine Learning using Python. Feel free to ask valuable questions in the comments section below.

##### Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data馃搱.

Articles:聽1364