我正在尝试运行 Elastic Net 回归,但收到以下错误:NameError: name 'sklearn' is not defined... 非常感谢任何帮助!

Python 3 : NameError: name  'sklearn'  is not defined-LMLPHP

    # ElasticNet Regression

    from sklearn import linear_model
    import statsmodels.api as sm

    ElasticNet = sklearn.linear_model.ElasticNet() # create a lasso instance
    ElasticNet.fit(X_train, y_train) # fit data

    # print(lasso.coef_)
    # print (lasso.intercept_) # print out the coefficients

    print ("R^2 for training set:"),
    print (ElasticNet.score(X_train, y_train))

    print ('-'*50)

    print ("R^2 for test set:"),
    print (ElasticNet.score(X_test, y_test))

最佳答案

由于您已导入 linear_model
改变
ElasticNet = sklearn.linear_model.ElasticNet()

ElasticNet = linear_model.ElasticNet()

关于Python 3 : NameError: name 'sklearn' is not defined,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43608932/

10-12 17:40