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'
#4
I've done some refactoring. Everything should still work,; I just made it more pythonic.

You can't have an infinite loop - for loops are inherently finite - but the code does have an exponential problem in get_distance(). If each list contains three items and each item is a list of three numbers, then the code iterates through them a total of 27 times; four items each would be 64 times; etc.

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_length = len(test1)
    inner_length = len(test1[0])

    for i in range(outer_length):
        euclidean_list = []
        for j in range(outer_length):
            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
    mycolunmlist = [item[col] for item in matrix]
    print('My colunm List :', mycolunmlist)

    for item in mycolunmlist:
        standard = (item[col] - ave(mycolunmlist)) / get_standard_deviation(matrix, col)
        matrix.insert(item[col], standard)

    return matrix

def ave(matrix):
    return sum(matrix)/len(matrix)
    
def get_standard_deviation(matrix, col):
    return statistics.stdev([item[col - 1] 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(item)
        print(
            'Euclidean 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]],1))

    print('Average of a matrix :', ave([2,3,4]))

    print('standardised matrix is :', get_standardised_matrix([[2,5,8],[4,8,2]]))

main()
Reply


Messages In This Thread
RE: TypeError: unsupported operand type(s) for -: 'str' and 'str' - by stullis - Dec-25-2018, 12:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Type Error: Unsupported Operand jhancock 2 1,343 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,447 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,280 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 33,007 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,111 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,478 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,117 Feb-09-2021, 05:40 PM
Last Post: soft
  TypeError: 'type' object is not subscriptable Stef 1 4,610 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,667 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,235 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