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'
#1
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 Angry . 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.

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)
Reply


Messages In This Thread
TypeError: unsupported operand type(s) for -: 'str' and 'str' - by shan1403 - Dec-24-2018, 09:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 3,475 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  Type Error: Unsupported Operand jhancock 2 5,500 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 11,672 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 3,848 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 35,588 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 4,096 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 8,911 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,892 Feb-09-2021, 05:40 PM
Last Post: soft
  TypeError: 'type' object is not subscriptable Stef 1 7,000 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 3,576 Jun-08-2020, 04:11 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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