<jupyter_start><jupyter_text>Importing all nescessary packages.<jupyter_code>import pandas as pd
import tensorflow as tf
import numpy as np
import matplotlib as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Downloading & Printing the dataset<jupyter_code>url = 'https://raw.githubusercontent.com/MilanBinsMathew/Software-Defect-Prediction-JM1/main/jm1_csv.csv'
df = pd.read_csv(url)
print(df.head())

df.isnull().sum()<jupyter_output>loc  v(g)  ev(g)  iv(g)  ...  total_Op  total_Opnd  branchCount  defects
0    1.1   1.4    1.4    1.4  ...       1.2         1.2          1.4        0
1    1.0   1.0    1.0    1.0  ...       1.0         1.0          1.0        1
2   72.0   7.0    1.0    6.0  ...     112.0        86.0         13.0        1
3  190.0   3.0    1.0    3.0  ...     329.0       271.0          5.0        1
4   37.0   4.0    1.0    4.0  ...      76.0        50.0          7.0        1

[5 rows x 22 columns]<jupyter_text>Shuffling the dataset.Segementation into training and test set (80% - 20%)Normalizing the values<jupyter_code>from sklearn.model_selection import train_test_split

X = df.drop(['defects'], axis =1).values

y = df['defects'].values<jupyter_output><empty_output><jupyter_text>Feature Scaling<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=[0,1])
X = scaler.fit_transform(X) 

train_X,test_X,train_Y,test_Y = train_test_split(X, y, test_size=0.2, random_state = 2)<jupyter_output><empty_output><jupyter_text>Keras Model Definition<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Activation,BatchNormalization, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy


model = Sequential([ 
(Dense(32,activation = 'relu',input_shape = (21,))),
(BatchNormalization()),
(Dense(32,activation = 'relu')),
(BatchNormalization()),
(Dense(64,activation = 'relu')),
(Dense(64,activation = 'relu')),
(Dense(64,activation = 'relu')),
(Dense(64,activation = 'relu')),
(BatchNormalization()),
(Dense(32,activation = 'relu')),
(Dense(32,activation = 'relu')),
(Dense(1,activation = 'linear'))
])
ad = Adam(learning_rate = 0.00001)


model.compile(optimizer = ad, loss='hinge',metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>model.fit(x = train_X, y = train_Y, batch_size = 64, epochs = 250)
model.summary()


from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=10)
x_train = model.predict(train_X)
knn.fit(x_train,train_Y)
x_test = model.predict(test_X)
y_pred = knn.predict(x_test)




from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score


knn_accuracy = accuracy_score(test_Y, y_pred)
print('Accuracy (RES + KNN): ', "%.2f" % (knn_accuracy*100))
precision = precision_score(test_Y, y_pred)
print('Precision: %f' % precision)

recall = recall_score(test_Y, y_pred)
print('Recall: %f' % recall)

f1 = f1_score(test_Y, y_pred)
print('F1 score: %f' % f1)<jupyter_output>Accuracy (RES + KNN):  81.20
Precision: 0.545977
Recall: 0.223529
F1 score: 0.317195<jupyter_text>Results<jupyter_code>'''
from sklearn import metrics


fig = plt.pyplot.plot(figsize=(10,5))
plt.pyplot.bar(test_Y,y_pred)

plt.pyplot.plot(test_Y,test_Y,'r')
'''
'''from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score


y_pred = model.predict(test_X)
y_pred = (y_pred >= 0.6).astype(int).ravel()
 

accuracy = accuracy_score(test_Y, y_pred)
print('Accuracy: %f' % accuracy)

precision = precision_score(test_Y, y_pred)
print('Precision: %f' % precision)

recall = recall_score(test_Y, y_pred)
print('Recall: %f' % recall)

f1 = f1_score(test_Y, y_pred)
print('F1 score: %f' % f1)
'''<jupyter_output>Accuracy: 0.810662
Precision: 0.622642
Recall: 0.077647
F1 score: 0.138075
{'fraction_non_alphanumeric': 0.13320128744738796, 'fraction_numerical': 0.05323099777172567, 'mean_word_length': 4.511596180081855, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '503915', 'license': 'MIT', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1547, 'n_words': 308, 'path': 'Software_Defect_Prediction.ipynb', 'repo_name': 'MilanBinsMathew/Software-Defect-Prediction-JM1', 'size': '4039'}