<jupyter_start><jupyter_text>IMPORTING LIBRARIES<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
dataset= pd.read_csv('creditcard.csv')
dataset.head()
# In class 1 for fraudulent transactions, 0 otherwise<jupyter_output><empty_output><jupyter_text>HANDLING CATEGORICAL DATA<jupyter_code>import pandas as pd 
from sklearn.preprocessing import LabelEncoder
dataset = pd.read_csv('creditcard.csv') 
x = dataset.iloc[:, :-1].values 
y = dataset.iloc[:, -1].values 
labelencoder_X = LabelEncoder()
x[:,0] = labelencoder_X.fit_transform(x[:,0])
x[:,1] = labelencoder_X.fit_transform(x[:,1])
print(x)
#Here we are using LabelEncoder
x = dataset[['V1','V2','V3','V4','V5','V6','V7','V8','V9','V10','V11','V12',	'V13',	'V14',	'V15',	'V16',	'V17',	'V18',	'V19',	'V20',	'V21'	,'V22',	'V23',	'V24',	'V25',	'V26','V27',	'V28'	]]
#separate the other attributes from the predicting attribute
y = dataset[['Class']]
#separte the predicting attribute into Y for model training<jupyter_output><empty_output><jupyter_text>GRAPHS<jupyter_code>dataset.plot(x='Time', y='Amount' , style='o')
plt.title('occurences')
plt.show()
plt.figure(figsize=(15,10))
plt.tight_layout()
seaborn.distplot(dataset['Class'])
plt.show()<jupyter_output><empty_output><jupyter_text>SPLITTING OF DATASET INTO TRAIN AND TEST SET<jupyter_code>x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2, random_state=0)<jupyter_output><empty_output><jupyter_text>NORMALIZING DATA<jupyter_code># data normalization with sklearn
from sklearn.preprocessing import MinMaxScaler

# fit scaler on training data
norm = MinMaxScaler().fit(x_train)

# transform training data
X_train_norm = norm.transform(x_train)

# transform testing data
X_test_norm = norm.transform(x_test)

print(X_train_norm)
print(X_test_norm)<jupyter_output>[[0.92765854 0.75384603 0.80071667 ... 0.35823502 0.65658631 0.33906135]
 [0.97518775 0.74009456 0.79312517 ... 0.56279193 0.64797653 0.3411726 ]
 [0.98933739 0.73939433 0.7754647  ... 0.24807282 0.6514884  0.34016734]
 ...
 [0.98095022 0.73060292 0.77703599 ... 0.34760283 0.64992833 0.3412243 ]
 [0.98675592 0.72181681 0.77882125 ... 0.38059922 0.64900085 0.34017174]
 [0.93831926 0.75047664 0.82356823 ... 0.36811236 0.65246324 0.34455712]]
[[0.94371538 0.75410214 0.78100038 ... 0.43060902 0.65309411 0.34581125]
 [0.94318027 0.7526404  0.78544342 ... 0.45138302 0.65218206 0.34623136]
 [0.9175248  0.71354094 0.78969764 ... 0.25766143 0.65821509 0.35385596]
 ...
 [0.97309737 0.71491762 0.70958397 ... 0.58777798 0.64303129 0.34089925]
 [0.99215057 0.74505958 0.72436384 ... 0.53848991 0.64699767 0.33931907]
 [0.99796161 0.72620269 0.75102425 ... 0.40473096 0.65017847 0.33961353]]<jupyter_text>ALGORITHM<jupyter_code>model =LogisticRegression()
#loading the model constructor<jupyter_output><empty_output><jupyter_text>FITTING OF TRAINING DATA<jupyter_code>model.fit(x_train,y_train)
print('Intercept is :',model.intercept_)
print('Coefficient is :' ,model.coef_)<jupyter_output>Coefficient is : [[ 0.05634186 -0.04748479  0.02864347  0.68895927  0.00780592 -0.10107519
  -0.04871149 -0.18447327 -0.21636813 -0.72852255  0.06198264  0.01643049
  -0.27013137 -0.53842135 -0.11502246 -0.12990055 -0.00546259 -0.07520044
   0.0303424  -0.24921952  0.37807731  0.51092904 -0.09418991  0.08693994
  -0.09104165  0.16411539 -0.63203324 -0.21155589]]<jupyter_text>PREDICTIONS FOR OUR PROBLEM STATEMENT WITH THE RELATED GRAPHS<jupyter_code>predictions = model.predict(x_test)
plt.scatter(y_test,predictions)
#to visualise the predictions and the test Y !!it is almost forming a linear line with less deviation<jupyter_output><empty_output><jupyter_text>EVALUATION<jupyter_code>print('Mean abolute error is:', metrics.mean_absolute_error(y_test,predictions))
print('Mean squared error is:', metrics.mean_squared_error(y_test,predictions))
print('Root mean squared error is:', np.sqrt(metrics.mean_squared_error(y_test,predictions)))
print(metrics.accuracy_score(y_test, predictions))<jupyter_output>0.9992099996488887<jupyter_text>SAVING THE MODEL USING PICKLE LIBRARY<jupyter_code>import pickle 

# Save the trained model as a pickle string. 
saved_model = pickle.dumps(model) 

# Load the pickled model 
model_from_pickle = pickle.loads(saved_model) 

# Use the loaded pickled model to make predictions 
model_from_pickle.predict(x_test)<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TRAINED DATA Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix
y_train = y_train[0:56962]
results =confusion_matrix(y_train, predictions)
print(results)<jupyter_output>[[56792    72]
 [   98     0]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support


precision_recall_fscore_support(y_train, predictions, average='macro')<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TEST DATA Confusion Matrix<jupyter_code>results =confusion_matrix(y_test, predictions)
print(results)<jupyter_output>[[56853     8]
 [   37    64]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support


precision_recall_fscore_support(y_test, predictions, average='macro')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12557455414598273, 'fraction_numerical': 0.18348961206104064, 'mean_word_length': 6.555555555555555, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '516905', 'license': 'MIT', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2161, 'n_words': 388, 'path': 'Logistic Regression/ML-LOR/Assignments/ML-ASGN-LOR-04/code/Credit_Card_Fraud_Detection___LOR.ipynb', 'repo_name': 'RASHI3004/Project-Internship---ML-DL', 'size': '5439'}