0% found this document useful (0 votes)
22 views9 pages

#Group: B (ML) : Numpy NP Pandas PD

code3

Uploaded by

Siddhesh Asati
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)
22 views9 pages

#Group: B (ML) : Numpy NP Pandas PD

code3

Uploaded by

Siddhesh Asati
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
You are on page 1/ 9

[1]: #GROUP: B(ML)

[2]:

import numpy as np
import pandas as pd

[3]: df = pd.read_csv("Churn_Modelling.csv")
df

[3]: RowNumber CustomerId Surname CreditScore Geography Gender Age \


0 1 15634602 Hargrave 619 France Female 42
1 2 15647311 Hill 608 Spain Female 41
2 3 15619304 Onio 502 France Female 42
3 4 15701354 Boni 699 France Female 39
4 5 15737888 Mitchell 850 Spain Female 43
... ... ... ... ... ... ... ...
9995 9996 15606229 Obijiaku 771 France Male 39
9996 9997 15569892 Johnstone 516 France Male 35
9997 9998 15584532 Liu 709 France Female 36
9998 9999 15682355 Sabbatini 772 Germany Male 42
9999 10000 15628319 Walker 792 France Female 28

Tenure Balance NumOfProducts HasCrCard IsActiveMember \


0 2 0.00 1 1 1
1 1 83807.86 1 0 1
2 8 159660.80 3 1 0
3 1 0.00 2 0 0
4 2 125510.82 1 1 1
... ... ... ... ... ...
9995 5 0.00 2 1 0
9996 10 57369.61 1 1 1
9997 7 0.00 1 0 1
9998 3 75075.31 2 1 0
9999 4 130142.79 1 1 0

1
EstimatedSalary Exited
0 101348.88 1
1 112542.58 0
2 113931.57 1
3 93826.63 0
4 79084.10 0
... ... ...
9995 96270.64 0
9996 101699.77 0
9997 42085.58 1
9998 92888.52 1
9999 38190.78 0

[10000 rows x 14 columns]

[4]:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 14 columns):
# Column Non-Null Count Dtype

0 RowNumber 10000 non-null int64


1 CustomerId 10000 non-null int64
2 Surname 10000 non-null object
3 CreditScore 10000 non-null int64
4 Geography 10000 non-null object
5 Gender 10000 non-null object
6 Age 10000 non-null int64
7 Tenure 10000 non-null int64
8 Balance 10000 non-null float64
9 NumOfProducts 10000 non-null int64
10 HasCrCard 10000 non-null int64
11 IsActiveMember 10000 non-null int64
12 EstimatedSalary 10000 non-null float64
13 Exited 10000 non-null int64
dtypes: float64(2), int64(9), object(3)
memory usage: 1.1+ MB

[5]:

[5]: RowNumber CustomerId Surname CreditScore Geography Gender Age \


0 1 15634602 Hargrave 619 France Female 42
1 2 15647311 Hill 608 Spain Female 41
2 3 15619304 Onio 502 France Female 42
3 4 15701354 Boni 699 France Female 39
4 5 15737888 Mitchell 850 Spain Female 43

2
Tenure Balance NumOfProducts HasCrCard IsActiveMember\ 0
2 0.00 1 1 1
1 1 83807.86 1 0 1
2 8 159660.80 3 1 0
3 1 0.00 2 0 0
4 2 125510.82 1 1 1

EstimatedSalary Exited
0 101348.88 1
1 112542.58 0
2 113931.57 1
3 93826.63 0
4 79084.10 0

[6]:

[6]: RowNumber CustomerId Surname CreditScore Geography Gender Age \


9995 9996 15606229 Obijiaku 771 France Male 39
9996 9997 15569892 Johnstone 516 France Male 35
9997 9998 15584532 Liu 709 France Female 36
9998 9999 15682355 Sabbatini 772 Germany Male 42
9999 10000 15628319 Walker 792 France Female 28

Tenure Balance NumOfProducts HasCrCard IsActiveMember \


9995 5 0.00 2 1 0
9996 10 57369.61 1 1 1
9997 7 0.00 1 0 1
9998 3 75075.31 2 1 0
9999 4 130142.79 1 1 0

EstimatedSalary Exited
9995 96270.64 0
9996 101699.77 0
9997 42085.58 1
9998 92888.52 1
9999 38190.78 0

[7]:

[7]: (10000, 14)

[8]:

[8]: RowNumber CustomerId CreditScore Age Tenure \


count 10000.00000 1.000000e+04 10000.000000 10000.000000 10000.000000
mean 5000.50000 1.569094e+07 650.528800 38.921800 5.012800
std 2886.89568 7.193619e+04 96.653299 10.487806 2.892174

3
min 1.00000 1.556570e+07 350.000000 18.000000 0.000000
25% 2500.75000 1.562853e+07 584.000000 32.000000 3.000000
50% 5000.50000 1.569074e+07 652.000000 37.000000 5.000000
75% 7500.25000 1.575323e+07 718.000000 44.000000 7.000000
max 10000.00000 1.581569e+07 850.000000 92.000000 10.000000

Balance NumOfProducts HasCrCard IsActiveMember \


count 10000.000000 10000.000000 10000.00000 10000.000000
mean 76485.889288 1.530200 0.70550 0.515100
std 62397.405202 0.581654 0.45584 0.499797
min 0.000000 1.000000 0.00000 0.000000
25% 0.000000 1.000000 0.00000 0.000000
50% 97198.540000 1.000000 1.00000 1.000000
75% 127644.240000 2.000000 1.00000 1.000000
max 250898.090000 4.000000 1.00000 1.000000

EstimatedSalary Exited
count 10000.000000 10000.000000
mean 100090.239881 0.203700
std 57510.492818 0.402769
min 11.580000 0.000000
25% 51002.110000 0.000000
50% 100193.915000 0.000000
75% 149388.247500 0.000000
max 199992.480000 1.000000

[9]:

[9] : RowNumber int64


CustomerId int64
Surname object
CreditScore int64
Geography object
Gender object
Age int64
Tenure int64
Balance float64
NumOfProducts int64
HasCrCard int64
IsActiveMember int64
EstimatedSalary float64
Exited int64
dtype: object

[10]:

4
[10] : RowNumber 0
CustomerId 0
Surname 0
CreditScore 0
Geography 0
Gender 0
Age 0
Tenure 0
Balance 0
NumOfProducts 0
HasCrCard 0
IsActiveMember 0
EstimatedSalary 0
Exited 0
dtype: int64

[11]: df.columns

[11] : Index(['RowNumber', 'CustomerId', 'Surname', 'CreditScore', 'Geography',


'Gender', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard',
'IsActiveMember', 'EstimatedSalary', 'Exited'],
dtype='object')

[12]:

[12]: RowNumber CustomerId Surname CreditScore Geography Gender Age \


0 1 15634602 Hargrave 619 France Female 42
1 2 15647311 Hill 608 Spain Female 41
2 3 15619304 Onio 502 France Female 42
3 4 15701354 Boni 699 France Female 39
4 5 15737888 Mitchell 850 Spain Female 43

Tenure Balance NumOfProducts HasCrCard IsActiveMember\ 0


2 0.00 1 1 1
1 1 83807.86 1 0 1
2 8 159660.80 3 1 0
3 1 0.00 2 0 0
4 2 125510.82 1 1 1

EstimatedSalary Exited
0 101348.88 1
1 112542.58 0
2 113931.57 1
3 93826.63 0
4 79084.10 0

5
[13]: #Distinguish the feature and target set, and divide the data into training and␣
‹→test sets

# Input Data
x =␣
‹→df[['CreditScore','Age','Tenure','Balance','NumOfProducts','HasCrCard','IsActiveMember','Esti

# Output Data
y = df['Exited']

[14]:

[14]: CreditScore Age Tenure Balance NumOfProducts HasCrCard \


0 619 42 2 0.00 1 1
1 608 41 1 83807.86 1 0
2 502 42 8 159660.80 3 1
3 699 39 1 0.00 2 0
4 850 43 2 125510.82 1 1
... ... ... ... ... ... ...
9995 771 39 5 0.00 2 1
9996 516 35 10 57369.61 1 1
9997 709 36 7 0.00 1 0
9998 772 42 3 75075.31 2 1
9999 792 28 4 130142.79 1 1

IsActiveMember EstimatedSalary
0 1 101348.88
1 1 112542.58
2 0 113931.57
3 0 93826.63
4 1 79084.10
... ... ...
9995 0 96270.64
9996 1 101699.77
9997 1 42085.58
9998 0 92888.52
9999 0 38190.78

[10000 rows x 8 columns]

[15]: =

6
[16]:

[16]: Exited
0 7963
1 2037
Name: count, dtype: int64

[29]:
ros

[30]:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)
x_scaled

[30]: array([[-0.32622142, 0.29351742, -1.04175968, ..., 0.64609167,


0.97024255, 0.02188649],
[-0.44003595, 0.19816383, -1.38753759, ..., -1.54776799,
0.97024255, 0.21653375],
[-1.53679418, 0.29351742, 1.03290776, ..., 0.64609167,

7
-1.03067011, 0.2406869 ],
...,
[ 0.60498839, -0.27860412, 0.68712986, ..., -1.54776799,
0.97024255, -1.00864308],
[ 1.25683526, 0.29351742, -0.69598177, ..., 0.64609167,
-1.03067011, -0.12523071],
[ 1.46377078, -1.04143285, -0.35020386, ..., 0.64609167,
-1.03067011, -1.07636976]])

[31]:
from sklearn.model_selection import train_test_split

x.shape

[31]: (10000, 8)

[32]:

[32]: (2500, 8)

[33]:

[33]: (7500, 8)

[34]: from sklearn.neural_network import MLPClassifier

[34]: MLPClassifier(hidden_layer_sizes=(100, 100, 100), max_iter=100, random_state=0)

[35]:
from sklearn.metrics import ConfusionMatrixDisplay, classification_report
from sklearn.metrics import accuracy_score

[36]: y_pred = ann.predict(x_test)

[37]:

[37] : Exited
0 1991
1 509
Name: count, dtype: int64

[38]: ConfusionMatrixDisplay.from_predictions(y_test, y_pred)

[38] : <sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x2a865a2e4d0>

8
[39]:

[39]: 0.7968

[40]:

precision recall f1-score support

0 0.80 1.00 0.89 1991


1 1.00 0.00 0.00 509

accuracy 0.80 2500


macro avg 0.90 0.50 0.45 2500
weighted avg 0.84 0.80 0.71 2500

[ ]:

[ ]:

You might also like