Python Forum
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unsupported operand type(s) for -: 'str' and 'str'
#11
thank you so much for your continuous help
however there is still error in the code

Traceback (most recent call last):
File "C:/Users/Python/new-assignment-forum.py", line 76, in <module>
main()
File "C:/Users/Python/new-assignment-forum.py", line 75, in main
print('Nearest vale :',get_k_nearest_labels('Data.csv','Data_Labels.csv','Learning_Data.csv',1))
File "C:/Users/Python/new-assignment-forum.py", line 56, in get_k_nearest_labels
get_distance(file_open[0], item)
File "C:/Users/Python/new-assignment-forum.py", line 20, in get_distance
euclidean += pow(float(test2[i][k]) - float(test1[j][k]), 2)
IndexError: list index out of range
Reply
#12
Had them backwards. Are you having any issues now?

import csv
import math
import statistics
   
def load_from_csv(filename):
    with open(filename,'r') as csvfile:
        return list(csv.reader(csvfile,delimiter=','))
   
def get_distance(test1,test2):
    euclidean_list_complete = []
    outer_length1 = len(test1)
    outer_length2 = len(test2)
    inner_length = len(test2[0])
   
    for i in range(outer_length2):
        euclidean_list = []
        for j in range(outer_length1):
            euclidean = 0
            for k in range(inner_length):
                euclidean += pow(float(test2[i][k]) - float(test1[j][k]), 2)
   
            euclidean_list.append(math.sqrt(euclidean))
   
        euclidean_list.sort(reverse = True)
        euclidean_list_complete.append(euclidean_list)
   
    return euclidean_list_complete
   
def get_standardised_matrix(matrix):
    col = 0
    out_matrix = []
    mycolumnlist = [item[col] for item in matrix]
    print('My column List :', mycolumnlist)
   
    for item in mycolumnlist:
        print(matrix)
        standard = (item - ave(mycolumnlist)) / get_standard_deviation(matrix, col)
        out_matrix.insert(item, standard)
   
    return out_matrix
   
def ave(matrix):
    return sum(matrix)/len(matrix)
   
def get_standard_deviation(matrix, col):
    return statistics.stdev([item[col] for item in matrix])
   
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)
    for item in fopen:
         print(
            'Nearest Distance between two matrix :\n',
            get_distance(file_open[0], item)
        )
   
def main():
    print('Data from "Data.csv" file :', load_from_csv('Data.csv'))
   
    print(
        'Euclidean Distance between two matrix :\n',
        get_distance(
            [[2,3,5],[12,3,5],[2,3,6]],
            [[1,2,3],[3,4,6],[4,5,8]]
        )
    )
   
    print('The Standard Deviation of the elements in the column number passed as a parameter :', get_standard_deviation([[2,5,8],[4,8,2]], 0))
   
    print('Average of a matrix :', ave([2,3,4]))
   
    print('standardised matrix is :', get_standardised_matrix([[2,5,8],[4,8,2]]))
    print('Nearest vale  :',get_k_nearest_labels('Data.csv','Data_Labels.csv','Learning_Data.csv',1))

main()
Reply
#13
Thanks a million for all your support. No errors, finally. Will let you know if I stuck somewhere else. So much thankful. Cheers!!!

The problem which I am facing now is the nearest labels function need to locate the N rows of the matrix from the file learning data. This should be nearest to the list passed in the function as a parameter.

Like if we assume the value of N is three (03), and the function is able to find the nearest row of the relevant list like 12, 25 and 32 from the file learning data labels the function should return only these three rows as list of lists.

Now the problem is how should I move the value in N?
Reply
#14
I don't follow. Are you trying to get multiple lists of results? Such as a list of the three closest and then the five closest. Or are you trying to change N inside the function?
Reply
#15
hi thank you for the reply
i need to calculate nearest distance between two csv files in get_k_nearest_labels ,if N(no. of nearest row) is 2 the it return a list that only contains these rows
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Type Error: Unsupported Operand jhancock 2 1,068 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,208 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,099 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,709 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,967 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,331 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,023 Feb-09-2021, 05:40 PM
Last Post: soft
  TypeError: 'type' object is not subscriptable Stef 1 4,445 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,573 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,152 May-17-2020, 05:56 PM
Last Post: shockwave

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020