Bill
Bill

Reputation: 273

Trying to import a function into a separate script file

I have this function saved in a script file. Saved as SampCov.py

x= [1., 5.5, 7.8, 4.2, -2.7, -5.4, 8.9]  
y =[0.1, 1.5, 0.8, -4.2, 2.7, -9.4, -1.9] 
def mean(x): 
    return sum(x) / len(x) 

def cov(x, y): 
    x_mean = mean(x) 
    y_mean = mean(y) 
    data = [(x[i] - x_mean) * (y[i] - y_mean) 
            for i in range(len(x))] 
    return sum(data) / (len(data) - 1) 

I am trying to import SampCov.py into a separate python script file. I got the x and y vectors to appear when I imported them. However cov(x,y) and mean(x) give me syntax errors. This is what I have so far. to import Sampcov.py

import Sampcov.py as samp
samp.x
samp.y
samp.mean(x)
samp.cov(x,y)

So I am trying to import my entire function into a new script file and I am not retrieving the entire function. Am I doing something wrong with sam.mean(x) and samp.cov(x,y)?

Upvotes: 0

Views: 68

Answers (1)

Himanshu
Himanshu

Reputation: 2454

You want samp.x, samp.y instead of x, y

import Sampcov as samp
samp.x
samp.y
samp.mean(samp.x)
samp.cov(samp.x,samp.y)

Upvotes: 1

Related Questions