Dec-24-2018, 09:58 PM
Hi, I am a newbie (relatively). In the following code, I have calculated function called get_standardised_matrix. Before this function, I have created another function which is get_standard_deviation. I need to standardise a matrix by using get_standard_deviation. This all should be done without using numpy.
Now I am getting the following error, I am stuck in this code from many hours now and quite frustrated at the moment
. THerefore, any URGENT help will be highly appreciated please?
get_k_nearest_labels('Data.csv','Learning_Data.csv','Learning_Data_Labels.csv',1)
[21:51, 12/24/2018] Nida: Traceback (most recent call last):
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 87, in <module>
get_k_nearest_labels('Data.csv','Learning_Data.csv','Learning_Data_Labels.csv',1)
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 84, in get_k_nearest_labels
distance=get_distance(file_open[0],fopen[i])
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 23, in get_distance
euclidean += pow((test2[i][k]-test1[j][k]),2)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>
Following is my script.
Now I am getting the following error, I am stuck in this code from many hours now and quite frustrated at the moment

get_k_nearest_labels('Data.csv','Learning_Data.csv','Learning_Data_Labels.csv',1)
[21:51, 12/24/2018] Nida: Traceback (most recent call last):
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 87, in <module>
get_k_nearest_labels('Data.csv','Learning_Data.csv','Learning_Data_Labels.csv',1)
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 84, in get_k_nearest_labels
distance=get_distance(file_open[0],fopen[i])
File "C:/Users/Nida/Desktop/Nida-CSEE/Python/python-assignment.py", line 23, in get_distance
euclidean += pow((test2[i][k]-test1[j][k]),2)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>
Following is my script.
import csv def load_from_csv(filename): with open(filename,'r') as csvfile: readCSV=list(csv.reader(csvfile,delimiter=',')) for row in readCSV: return readCSV print('Data from "Data.csv" file :',load_from_csv('Data.csv')) import math def get_distance(test1,test2): euclidean = 0 euclidean_list_complete = [] for i in range(len(test2)): euclidean_list = [] for j in range(len(test1)): for k in range(len(test1[0])): euclidean += pow((test2[i][k]-test1[j][k]),2) euclidean_list.append(math.sqrt(euclidean)) euclidean = 0 euclidean_list.sort(reverse=True) euclidean_list_complete.append(euclidean_list) del euclidean_list print ('Euclidean Distance between two matrix :\n',euclidean_list_complete) get_distance([[2,3,5],[12,3,5],[2,3,6]],[[1,2,3],[3,4,6],[4,5,8]]) import statistics def get_standard_deviation(matrix, col): #list1=matrix #print(list1) #print(matrix.columns.values) i=col-1 new_list=[item[i] for item in matrix] result= statistics.stdev(new_list) return result print('The Standard Deviation of the elements in the column number passed as a parameter :',get_standard_deviation([[2,5,8],[4,8,2]],1)) def ave(matrix): average=sum(matrix)/len(matrix) print('Average of a matrix :',average) #print(ave[2,3,4]) def get_standardised_matrix(matrix): col = 0 i = 0 getdeviation=get_standard_deviation(matrix, col) mycolunmlist = [] while i < len(matrix): mycolunmlist.append(matrix[i][col]) i += 1 print('My colunm List :',mycolunmlist) for i in range(len(mycolunmlist)): #print(matrix[i][col]) standard =(matrix[i][col]-ave(mycolunmlist))/get_standard_deviation(matrix, col) matrix.insert(matrix[i][col],standard) print('standardised matrix is :',matrix) #not working #print('The standardised matrix',get_standardised_matrix([[2,5,8],[4,8,2]])) def get_k_nearest_labels(rowmatrix, matrix, new_matrix, k): file_open=load_from_csv(rowmatrix) print(file_open[0]) fopen=load_from_csv(matrix) newfile=load_from_csv(new_matrix) rowmatrix[0] for i in range(len(fopen)): print(fopen[i]) distance=get_distance(file_open[0],fopen[i]) print(distance)