首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-名称未定义

Python-名称未定义
EN

Stack Overflow用户
提问于 2015-12-05 23:56:04
回答 2查看 385关注 0票数 0

我很难让我的代码运行。我一直收到的错误是,我的x变量(如'hsGPA‘)没有定义。下面是我的密码。我已经尝试了张贴在其他线程上的解决方案,但没有任何帮助,所以请不要标记为副本。谢谢!

代码语言:javascript
复制
def readData(fileName):


    hsGPA = []   #High School GPA
    mathSAT = []  #Math SAT scores
    crSAT = []  #Verbal SAT scores
    collegeGPA = []  #College GPA
    FullList=[] 
    inputFile = open(fileName, 'r', encoding = 'utf-8')

    for line in inputFile:
        FullList=line.split(',')
        hsGPA.append(float(FullList[0]))
        mathSAT.append(int(FullList[1]))
        crSAT.append(int(FullList[2]))
        collegeGPA.append(float(FullList[3]))
    return hsGPA, mathSAT, crSAT, collegeGPA 



def plotData(hsGPA, mathSAT, crSAT, collegeGPA):

    GPA1 = []   #High School GPA
    Score1 = []  #Math SAT scores
    Score2= []  #Verbal SAT scores
    GPA2 = []  #College GPA

    hsGPA, mathGPA, crSAT, collegeGPA = readData('SAT.txt')
    pyplot.figure(1)

    pyplot.subplot(4,1,1)
    for line in range(len(hsGPA)):
        GPA1.append(line)
    pyplot.plot(GPA1,hsGPA)

    pyplot.subplot(4,1,2)
    for line in range(len(mathSAT)):
        Score1.append(line)
    pyplot.plot(Score1,mathSAT)

    pyplot.subplot(4,1,3)
    for line in range(len(crSAT)):
        Score2.append(line)       
    pyplot.plot(Score2,crSAT)

    pyplot.subplot(4,1,4)
    for line in range(len(collegeGPA)):
        GPA2.append(line)
    pyplot.plot(GPA2,collegeGPA)

    pyplot.show()

def LinearRegression(xList, yList):
    '''
    This function finds the constants in the y = mx+b, or linear regression
    forumula

    xList - a list of the x values
    yList - a list of the y values
    m - the slope f the line
    b - where the line intercepts the y axis
    '''

    n = len(xList)
    sumX = 0
    sumXX = 0
    sumXY = 0
    sumY = 0

    for index in range(n):
        sumX += xList[index]
        sumXY += xList[index] * yList[index]
        sumXX += xList[index]**2
        sumY += yList[index]
        #the components needed to find m and b

    m = (n*(sumXY - (sumX*sumY)))/(n*(sumXX - (sumX**2)))
    b = (sumY - (m*sumX))/n
    #actually implements formula

    return m, b


def plotRegression(x,y, xLabel, yLabel):
    ScoreT = []

    pyplot.scatter(x,y)
    m,b = linearRegression(xList,yList)
    minX = min(x)
    maxX = max(x)
    pyplot.plot([minX, maxX], [m * minX + b, m * maxX + b], color ='red')
    pyplot.xlabel(xLabel)
    pyplot.ylabel(yLabel)
    pyplot.show()

    for index in range(len(mathSAT)):
        sumscore = mathSAT[index] + crSAT[index]
        ScoreT.append(sumscore)
    return ScoreT


def rSquared(x,y,m,b):

    n = len(x)
    R=0
    sumS=0
    sumT=0
    sumY=0

    for index in range(n):
        a=(y[index]-((m*x[index])+b))**2
        sumS = sumS+a


    for index in range(len(y)):
        sumY = sumY= y[index]
        MeanY= sumY/(len(y))
        e=(y[index]-MeanY)**2
        sumT = sumT+e




    m,b= LinearRegression(xList, yList)

    RG=1-(sumS/sumT)





def main():
    print(readData('SAT.txt'))
    plotData(*readData('SAT.txt'))
    plotRegression(hsGPA,collegeGPA, 'highGPA', 'collegeGPA')
    plotRegression(mathSAT,collegeGPA, 'highGPA' , 'collegeGPA')
    plotRegression(crSAT,collegeGPA, 'highGPA' , 'collegeGPA')
    plotRegression(ScoreT,collegeGPA, 'highGPA' , 'collegeGPA')



main()

它为每个x变量在plotRegression之后给出了main中的错误。请帮帮忙!谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-05 23:59:48

试试这个:

代码语言:javascript
复制
def plotRegression(x,y, xLabel, yLabel):
    # I deleted ScoreT = [] here
    pyplot.scatter(x,y)
    m,b = linearRegression(x,y)
    minX = min(x)
    maxX = max(x)
    pyplot.plot([minX, maxX], [m * minX + b, m * maxX + b], color ='red')
    pyplot.xlabel(xLabel)
    pyplot.ylabel(yLabel)
    pyplot.show()
    # I deleted the loop and return statement here

# ....

def main():
    data = readData('SAT.txt')
    print(data)
    plotData(*data)
    hsGPA, mathSAT, crSAT, collegeGPA = data
    # added ScoreT calculation here
    ScoreT = [sum(x) for x in zip(mathSAT, crSAT)]
    plotRegression(hsGPA,collegeGPA, 'highGPA', 'collegeGPA')
    plotRegression(mathSAT,collegeGPA, 'highGPA' , 'collegeGPA')
    plotRegression(crSAT,collegeGPA, 'highGPA' , 'collegeGPA')
    plotRegression(ScoreT,collegeGPA, 'highGPA' , 'collegeGPA')
票数 1
EN

Stack Overflow用户

发布于 2015-12-05 23:59:22

在您的main()中,从未定义过hsGPA。它在其他函数中定义,在全局上下文中不共享。所以main无法访问它。

你需要从readData()回来

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34112359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档