<jupyter_start><jupyter_text>Importing the necessary dependencies<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>Importing the dataset<jupyter_code>df = pd.read_csv('C:/Users/Hemant/jupyter_codes/ML Project 1/Credit card fraud detection/creditcard.csv')
#print fisrt 5 rows of the dataset
df.head()
#print the last 5 rows of the dataset
df.tail()
#shape of the dataset
df.shape
#dataset information
df.info()
#distribution of class values
df['Class'].value_counts()<jupyter_output><empty_output><jupyter_text>This dataset is highly unbalanced as the number of fraud cases are much much smaller than legit transactions.0 ----> legit transactions1 ----> fraud transactions<jupyter_code>#separating the data for analysis
legit = df[df.Class == 0]
fraud = df[df.Class == 1]
print(legit.shape, fraud.shape)
#stastical measures of the dataset
legit.Amount.describe()
fraud.Amount.describe()
#compare the values for both transactions
df.groupby('Class').mean()<jupyter_output><empty_output><jupyter_text>Under Sampling Build a sample dataset containing similar distributions of normal and fraudlent transactions.No. of fraudlent transactions ---> 492<jupyter_code>legit_sample = legit.sample(n = 492)
#concatenate two dataframes
new_df = pd.concat([legit_sample, fraud], axis = 0)
#print first 5 rows
new_df.head()
#print last 5 rows
new_df.tail()
#shape of new_df
new_df.shape
#distribution of class values
new_df['Class'].value_counts()
#compare the values for both transactions
new_df.groupby('Class').mean()<jupyter_output><empty_output><jupyter_text>Splitting the data into features and targets<jupyter_code>X = new_df.drop('Class', axis = 1)
Y = new_df['Class']
print(X.shape, Y.shape)<jupyter_output>(984, 30) (984,)<jupyter_text>Train and test data and model evaluation<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = .2, stratify = Y, random_state = 2)
print(x_train.shape, x_test.shape)
print(y_train.shape, y_test.shape)<jupyter_output>(787, 30) (197, 30)
(787,) (197,)<jupyter_text>Model evaluationLogistic regression<jupyter_code>model = LogisticRegression()
#training the model
model.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>Model evaluationAccuracy score<jupyter_code>#on training data
training_prediction = model.predict(x_train)

training_accuracy = accuracy_score(training_prediction, y_train)
print('THE TRAINING ACCURACY IS :', training_accuracy)
#on testing data
testing_prediction = model.predict(x_test)

testing_accuracy = accuracy_score(testing_prediction, y_test)
print('THE TRAINING ACCURACY IS :', testing_accuracy)<jupyter_output>THE TRAINING ACCURACY IS : 0.8934010152284264<jupyter_text>Building a predictive system<jupyter_code>model_input = input()

input_list = [float(i) for i in model_input.split(',')]

input_array = np.asarray(input_list)

reshaped_array = input_array.reshape(1, -1)

prediction = model.predict(reshaped_array)
if prediction == 1:
    print('\nTHIS IS A CASE OF FRAUDLENT TRANSACTION')
else:
    print('THE IS A LEGIT TRANSACTION')<jupyter_output>406,-2.3122265423263,1.95199201064158,-1.60985073229769,3.9979055875468,-0.522187864667764,-1.42654531920595,-2.53738730624579,1.39165724829804,-2.77008927719433,-2.77227214465915,3.20203320709635,-2.89990738849473,-0.595221881324605,-4.28925378244217,0.389724120274487,-1.14074717980657,-2.83005567450437,-0.0168224681808257,0.416955705037907,0.126910559061474,0.517232370861764,-0.0350493686052974,-0.465211076182388,0.320198198514526,0.0445191674731724,0.177839798284401,0.261145002567677,-0.143275874698919,0

THIS IS A CASE OF FRAUDLENT TRANSACTION
{'fraction_non_alphanumeric': 0.10886142519063897, 'fraction_numerical': 0.13200105180120958, 'mean_word_length': 7.665148063781321, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '356109', 'license': 'MIT', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1370, 'n_words': 281, 'path': 'credit card fraud prediction model.ipynb', 'repo_name': 'Hemant2801/Credit-card-fraud-prediction', 'size': '3803'}