关于#python#的问题:print不出

img


coef_这里一直报错,尝试运行 运行不了,报错的地方不知道怎么进行修改 有哪位可以帮帮我

报错信息贴一下,而且print里面你应该吧那么“,”换成+吧

这段代码存在多处问题:

  1. 在LassoCy导入语句中,LassoCy应该为LassoCV,需要更正。
  2. inputfile变量定义中的单引号和句点位置错误,应该修改为:inputfile = 'F:\data.csv'。
  3. X_train和y变量的赋值语句缺少空格,应该修改为:X_train = data.iloc[:, 0:13] 和 y = data.iloc[:, -1]。
  4. 在LassoCV初始化时,alphas和alpha应该同名,需要修改为:alphas=alpha。
  5. coef变量赋值语句中,model lasso应该为model_lasso,需要修改为:model_lasso.coef_。
  6. 最后print语句中,应该为np.round(coef, 5),需要修正。
  7. 在注释中,输入的数据文件data.csv前缺少了双引号。
    改正后的代码如下:
import pandas as pd
import numpy as np
from sklearn.linear_model import LassoCV
 inputfile = "F:\data.csv"
data = pd.read_csv(inputfile, index_col=0)
X_train = data.iloc[:, 0:13]
y = data.iloc[:, -1]
 alpha = np.arange(0, 1001, 10)
model_lasso = LassoCV(alphas=alpha, cv=3).fit(X_train, y)
coef = pd.Series(model_lasso.coef_, index=X_train.columns)
print('相关系数为:\n', np.round(coef, 5))

建议在写代码时,注意细节,仔细检查语法和语义,尽量避免拼写错误和语法错误的出现。

\是转义符,要改成\\或者/,或者r'F:\data.csv',否则路径不正确