Python Forum
Problem Sending a Dataset to a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Sending a Dataset to a function
#1
I'm have a problem operating on a set of integers to determine the gradient of the sequence.
in principle it works ok.

I have posted the main code "SourceData" and the function "gradients" to demonstrate the problem by running "SourceData".


Background: in Part B of SourceData I have created an example dataset with a format identical to that which is outside my control as received from a third party.
I have reproduced the same dataset in Part A to show that it can operate correctly, thereby validating the function code.


However, inputting the dataset as the reconstructed variable in Part B fails with the error message
"TypeError: unsupported operand type(s) for +: 'int' and 'str'"

The datasets are identical at the point after they enter the function ( see K1) whether from in both Part A or Part B.

So why the error message in Part B?



Many thanks

Astrikor

#gradients
def Trend(newdata):
    y = newdata

    print("k1 (in trend function)  = ",y)

    
    x = range(1,len(y)+1)
    def var(X):
        S = 0.0
        SS = 0.0
        for x in X:
            S += x
            SS += x*x
        xbar = S/float(len(X))
        return (SS - len(X) * xbar * xbar) / (len(X) -1.0)

    def cov(X,Y):
        n = len(X)
        xbar = sum(X) / n
        ybar = sum(Y) / n
        return sum([(x-xbar)*(y-ybar) for x,y in zip(X,Y)])/(n-1)

    def beta(x,y):
        return cov(x,y)/var(x)
    Gradient = beta(x,y)
    return Gradient
import gradients
print("Part A - direct input to Trend function")
k = (2, 4,6,8)
Gradient = gradients.Trend(k)
print()
print("Gradient = ",Gradient)
print()
print("######################")
print()
print()
print("Part B - indirect input to Trend function")
print()
print("Create example data list k")
x = k = 2
while x < 8:
        x = x + 2
        k = k,x     
print("k2 = ",k)

# remove all brackets from k:
k = str(k).replace('(', '').replace(')', '')
print("k3 = ",k)
k = "({})".format(k)
print("k4 formatted to match direct newdata :   ",k)
print()

#send data to Trend function:
Gradient = gradients.Trend(k)
print()
print("Gradient = ",Gradient)
Reply
#2
You're trying to do math on this info, so I'd expect you should be passing in ints or floats. In part A you do, since line 2 is k = (2, 4,6,8) (a list of ints)

But in part B you turn it into a string starting at line 21 and deal with it as a string. When you pass that string into your function, it doesn't know how to add the terms.

k = (2, 3, 4) isn't a string with parentheses and ascii digits, it's a tuple with integer elements and your function expects something similar.

If you're getting a string from your other dataset, you'll want to convert it back into a collection of numbers. And how to do that depends on exactly how you're getting it.
Reply
#3
Thanks Bowlofred, very instructive !

Although the two versions of k1 look similar when printed, clearly Python sees them differently.
Your suggestion solves the problem, and I am using eval to convert the string to tuple as at line 26 attached.

Thanks again, brilliantly simple.

Astrikor

import gradients
print("Part A - direct input to Trend function")
k = (2, 4,6,8)
Gradient = gradients.Trend(k)
print()
print("Gradient = ",Gradient)
print()
print("######################")
print()
print()
print("Part B - indirect input to Trend function")
print()
print("Create example data list k")
x = k = 2
while x < 8:
        x = x + 2
        k = k,x     
print("k2 = ",k)

# remove all brackets from k:
k = str(k).replace('(', '').replace(')', '')
print("k3 = ",k)
k = "({})".format(k)
print("k4 formatted to match direct newdata :   ",k)
print()
k = eval(k)#(converts string to tuple)
print("k5 Converted to tuple = ", k)
#send data to Trend function:
Gradient = gradients.Trend(k)
print()
print("Gradient = ",Gradient)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem loading an image dataset into Tensorflow MohammedSohail 1 1,720 Jun-09-2020, 02:09 PM
Last Post: nuffink
  Problem in sending email using python nhoh007 1 2,495 Aug-25-2018, 07:20 PM
Last Post: typic
  Problem with sending text file as an attachment using this Gmail OAuth script downloaderfan 6 6,900 Feb-07-2018, 09:54 PM
Last Post: downloaderfan

Forum Jump:

User Panel Messages

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