0% found this document useful (0 votes)
2 views4 pages

Python Code For Binning

The document details the process of analyzing a dataset containing bank customer information using pandas in Python. It includes loading the dataset, creating age groups through custom and automatic binning, and visualizing the distribution of age and balance categories. Additionally, it examines the subscription rates for term deposits based on age and balance groups.

Uploaded by

satpal83421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Code For Binning

The document details the process of analyzing a dataset containing bank customer information using pandas in Python. It includes loading the dataset, creating age groups through custom and automatic binning, and visualizing the distribution of age and balance categories. Additionally, it examines the subscription rates for term deposits based on age and balance groups.

Uploaded by

satpal83421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pandas as pd

keyboard_arrow_down Load Dataset

df = pd.read_csv('[Link]', sep=';')
print([Link](10))

age job marital education default balance housing loan \


2781 42 management married tertiary no 1093 yes no
13188 33 services single secondary no 0 no no
2036 31 management married tertiary no 713 yes no
18535 56 technician divorced tertiary no 1593 yes no
12151 33 admin. married secondary no -111 yes yes
11448 41 blue-collar divorced secondary no 3622 no no
17367 38 admin. married secondary no 337 no no
25903 44 technician married primary no 7800 yes no
9887 47 admin. married secondary no 533 no yes
24978 57 retired single tertiary no 0 no no

contact day month duration campaign pdays previous poutcome y


2781 unknown 14 may 260 1 -1 0 unknown no
13188 cellular 8 jul 116 2 -1 0 unknown no
2036 unknown 9 may 1534 2 -1 0 unknown no
18535 cellular 31 jul 434 4 -1 0 unknown no
12151 unknown 20 jun 12 3 -1 0 unknown no
11448 unknown 19 jun 1135 4 -1 0 unknown yes
17367 cellular 28 jul 624 4 -1 0 unknown no
25903 cellular 19 nov 250 1 -1 0 unknown no
9887 unknown 9 jun 128 1 -1 0 unknown no
24978 cellular 18 nov 300 1 -1 0 unknown no

df['age'].min()

18

df['age'].max()

95

keyboard_arrow_down Custom(Manual) Binning

df['age_group'] = [Link](df['age'], bins=[17,30,45,60,95])


[Link]()

age job marital education default balance housing loan contact day month duration campaign pdays prev

0 58 management married tertiary no 2143 yes no unknown 5 may 261 1 -1

1 44 technician single secondary no 29 yes no unknown 5 may 151 1 -1

2 33 entrepreneur married secondary no 2 yes yes unknown 5 may 76 1 -1

3 47 blue-collar married unknown no 1506 yes no unknown 5 may 92 1 -1

4 33 unknown single unknown no 1 no no unknown 5 may 198 1 -1

import [Link] as plt

# Histogram plotting
bins=[17,30,45,60,95]
[Link](df['age'],bins=bins, edgecolor='black', color='yellow')
[Link]('Age Groups Distribution')
[Link]('Age Bins')
[Link]('Count')
[Link](bins)
[Link]()
# Check subscription rate of term deposit
[Link](df['age_group'], df['y'], normalize='index') * 100

y no yes

age_group

(17, 30] 83.712660 16.287340

(30, 45] 90.119243 9.880757

(45, 60] 90.218703 9.781297

(60, 95] 57.744108 42.255892

df['age_group'] = [Link](df['age'], bins=[17,30,45,60,95], labels=['Young','Middle','Senior','Old'])


[Link]()

age job marital education default balance housing loan contact day month duration campaign pdays prev

0 58 management married tertiary no 2143 yes no unknown 5 may 261 1 -1

1 44 technician single secondary no 29 yes no unknown 5 may 151 1 -1

2 33 entrepreneur married secondary no 2 yes yes unknown 5 may 76 1 -1

3 47 blue-collar married unknown no 1506 yes no unknown 5 may 92 1 -1

4 33 unknown single unknown no 1 no no unknown 5 may 198 1 -1

labels=['Young','Middle','Senior','Old']
df['age_group'].value_counts().reindex(labels).plot(kind='bar', color='teal', edgecolor='black')
[Link]("Age Group")
[Link]("Count")
[Link]("Age Groups Distribution")
[Link](rotation=0)
[Link]()
df['balance'].min()

-8019

df['balance'].max()

102127

df['balance_group'] = [Link](df['balance'],bins=[-10000, 0, 10000, 50000, 200000],labels=['Negative', 'Low', 'Medium', 'Hig


[Link](df['balance_group'], df['y'], normalize='index') * 100

y no yes

balance_group

Negative 93.104396 6.895604

Low 87.461592 12.538408

Medium 83.847102 16.152898

High 77.777778 22.222222

keyboard_arrow_down Equal Width Binning

# Creating 5 equal-width bins


df['Age_Equal_Width_Bins'], bin_edges = [Link](df['age'], bins=5,retbins=True)
print(df[['age', 'Age_Equal_Width_Bins']])

age Age_Equal_Width_Bins
0 58 (48.8, 64.2]
1 44 (33.4, 48.8]
2 33 (17.923, 33.4]
3 47 (33.4, 48.8]
4 33 (17.923, 33.4]
... ... ...
45206 51 (48.8, 64.2]
45207 71 (64.2, 79.6]
45208 72 (64.2, 79.6]
45209 57 (48.8, 64.2]
45210 37 (33.4, 48.8]

[45211 rows x 2 columns]

print("Bin Edges:", bin_edges)

Bin Edges: [17.923 33.4 48.8 64.2 79.6 95. ]

[Link](df['age'], bins=bin_edges, edgecolor='black')


[Link]('Age Groups Distribution')
[Link]('Age Bins')
[Link]('Count')
[Link](bin_edges)
[Link]()
keyboard_arrow_down Equal Frequency Binning

df['Age_Equal_Freq_Bins'], bin_edges = [Link](df['age'], q=2, retbins=True)

print(df[['age', 'Age_Equal_Freq_Bins']])

age Age_Equal_Freq_Bins
0 58 (39.0, 95.0]
1 44 (39.0, 95.0]
2 33 (17.999, 39.0]
3 47 (39.0, 95.0]
4 33 (17.999, 39.0]
... ... ...
45206 51 (39.0, 95.0]
45207 71 (39.0, 95.0]
45208 72 (39.0, 95.0]
45209 57 (39.0, 95.0]
45210 37 (17.999, 39.0]

[45211 rows x 2 columns]

[Link](df['age'], bins=bin_edges, edgecolor='black', color='lightcoral')


[Link]('Age Groups Distribution')
[Link]('Age Bins')
[Link]('Count')
[Link](bin_edges)
[Link]()

You might also like