Question: whats wrong with this code? import numpy as np def main ( filename , filter _ value,type _ of _ card ) :

whats wrong with this code?
import numpy as np
def main(filename,filter_value,type_of_card):
"""read dataset and stores transaction records"""
data=read_csv(filename)
#[ALANA MAKE SURE U READ THIS LATER]
filtered_data=filter_data(data,filter_value,type_of_card)
display_results(filtered_data)
def read_csv(filename):
data=[]
with open(filename,'r') as file:
reader=csv.reader(file)
next(reader)
for row in reader:
data.append(row)
return data
def filter_data(data,filter_value,type_of_card):
filtered_data=[]
for row in data:
if filter_value in row and type_of_card in row:
filtered_data.append(row)
return filtered_data
def display_results(filtered_data):
for row in filtered_data:
print(row)
def task1(data,filter_value,type_of_card):
"""data contains records
filter_value is an area name
type_of_card is name of card provider
return list containing values to 2 decimal"""
answer=[]
#finding cosine difference between normal & malicious transactions
#based on IP_validity_score
normaltransaction=data[data["Transaction_type"]=='normal']["IP_validity_score"]
maltransaction=data[data["Transaction_type"]=='malicious']["IP_validity_score"]
#treat transactions as vectors
normalvector=np.mean(normaltransaction)
malvector=np.mean(maltransaction)
dotproduct=np.dot(normalvector,malvector)
#find norm of 'normal' transaction and 'malicious' transaction vectors
norm_normal=np.linalg.norm(normalvector)
norm_mal=np.linalg.norm(malvector)
#plug into formula for cosine similarity
cosine_simil=dotproduct/(norm_normal*norm_mal)
#find cosine distance
cos_dist=1-cosine_simil
answer.append(round(cos_dist,2))
#find variance for specific area
area_filtered_data=data[data['Actual_area']==filter_value]
transaction_amounts=area_filtered_data['Transaction_amount']
variance=np.var(transaction_amounts,ddof=1)
answer.append(round(variance,2))
#find median of authentication score for lower 25th and upper 75th percentile
#based on type of card
filteredcarddata=data[data['Type_of_card']==type_of_card]
authenticationscores=filteredcarddata['Authentication_score']
low25percentile=np.percentile(authenticationscores,25,interpolation='lower')
high75percentile=np.percentile(authenticationscores,75,interpolation='higher')
lowmedian=np.median(authenticationscores[authenticationscores<=low25percentile])
highmedian=np.median(authenticationscores[authenticationscores>=high75percentile])
answer.append([round(lowmedian,2),round(highmedian,2)])
#filter mal transactions where 'actual' and 'origin' are different
#find elementwie product between authentication score and ip validation score
#perform correlation between resultant vector and amount column
malsusspots=data[(data['Transaction_Type']=='Malicious')&(data['Actual']!=data['Origin'])]
authscores=malsusspots['Authentication_score']
ipscores=malsusspots['IP_validity_score']
dot_product=np.dot(authscores,ipscores)
correlation=np.corrcoef(dot_product, malsusspots['Transaction_amount'])[0,1]
answer.append(round(correlation,2))
#create Nx5 matrix where N is number of rows in data set
#find principal component analysis (PCA) to reduce dimensionality to Nx1
transactiontypemap={'ATM':1,'EFTPOS':2,'Internet':3}
entrymodemap={'Magnetic Stripe':1,'Manual':2,'Chip Card Read':3,'NFC':4}
datacopy['Transaction_Type']=datacopy['Transaction_Type'].map(transactiontypemap)
datacopy['Entry_mode']=datacopy['Entry_mode'].map(entrymodemap)
featurematrix=data_copy[['Transaction_Type','Entry_mode','Transaction_amount','Authentication_score','IP_validity_score']].values
pca=PCA(n_components=1)
pcaresult=pca.fit_transform(featurematrix)
answer.append(list(np.round(pcaresult.flatten(),2)))
print(f"Cosine Distance: {cos_dist}")
print(f"Variance: {var}")
print(f"Median: {median}")
print(f"Correlation: {corr}")
print(f"PCA: {pca}")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!