您能否将字典传递给函数 python 并在不同的地方使用密钥对

问题描述

我目前正在做机器学习的回归模型。我正在测试 3 个单独的 y 变量。我写了一个函数

  1. 将使用训练数据拟合模型
  2. 获得 R^2 进行训练并使用测试数据获得 R^2 和 MSE 进行预测
  3. “网格搜索”一组 alpha 值和标准化选项*
  4. 为训练获取 R^2 并使用测试数据进行预测,为每个网格搜索组合获取 R^2 和 MSE
  5. 将所有结果放在表格中
  • 这不是使用网格搜索 fx 的真正网格搜索,它只是使用定义的值反复拟合和测试模型

    见下文:

      ## GRID FOR y
      x=Lasso()
    
      def regdf(x,i,j):
          yval = [y1_train,y2_train,y3_train]
          for y in yval:
              model = x(alpha=i)
              model.fit(Xtrain,y)
              rsqtrain= format(model.score(Xtrain,y),'.3f')
    
          # Predict the test set
              model_pred= model.predict(Xt20)
    
          # Calculating mean squared error and R-sq for the predictions
              msetest = np.sqrt(mean_squared_error(y1_test model_pred))  # here I need to y_test to change
              rsqtest = format(model.score(Xtest,y1_test),'.3f')# here I need to y_test to change
              return rsqtrain,msetest,rsqtest
    
    
      y_ = "y1"
      y_test = "y1T"
      alphas = [1,0.1,0.01,0.001,0.0001,0]
      rsqtrainlist = []
      msetestlist = []
      rsqtestlist = []
      modellist = []
      alphalist = []
      normlist = []
      norm = [True,False]
      yval = [y1,y2,y3]
      ylist= []
      for x in [Ridge]:
          for y in yval:
              for i in alphas:        
                  for j in norm: 
                      a,b,c = regdf(x,j)
                      rsqtrainlist.append(a)
                      msetestlist.append(b)
                      rsqtestlist.append(c)
                      modellist.append(x)
                      alphalist.append(i)
                      normlist.append(j)
    
    
      testdf = pd.DataFrame()
      testdf["yval"] = ylist
      testdf['rsqtrain'] = rsqtrainlist
      testdf["y_test"] = y_test
      testdf['msetest'] = msetestlist
      testdf['rsqtest'] = rsqtestlist
      testdf['model'] = modellist
      testdf['alpha'] = alphalist
      testdf["normalize"]=normlist
    

    gridLassoytest = testdf.copy() gridLassoytest

因为我有 3 个不同的 y 变量,所以我试图将 for 循环添加函数中以针对每个 y 变量运行。我添加了循环以遍历 y_trian 值。我的问题是,当它遍历 y 变量时,我需要将 y1_train 和 y1_test 放在另一个变量中,现在它的迭代抛出了三个 y_train 值但不更改 y_test 值。我以为我可以使用字典并像这样配对 y_train/y_test 变量:

yvals = { y1_train:y1_test,y2_train:y2_test,y3_train:y3_test}。

问题是我不知道如何使函数调用密钥对或知道它需要更改 Y_test 变量以匹配 y_train。有没有人有任何想法?我正在处理一个通用数据集,所以我在我的谷歌驱动器中附加了一个指向该文件链接。我已将它们作为测试和训练集以及我如何定义变量。

谢谢

测试 = https://drive.google.com/file/d/1FNeJ5YgT-_VP7DzR6sXJAREzTnhkxAB7/view?usp=sharing 火车 = https://drive.google.com/file/d/1hwrnwbqjdmoRyo1hO1S5Gp_f2vudfsFU/view?usp=sharing

# import csv's
RegTrain80.drop(["Unnamed: 0"],axis=1,inplace=True)
RegTest20.drop(["Unnamed: 0"],inplace=True)

#train set
Xtrain = RegTrain80.iloc[ :,4:66]
    # y1 variable
y1_train = RegTrain80.iloc[ :,1]
    # y2 variable
y2_train  = RegTrain80.iloc[ :,2]
    # y3 variable
y3_train = RegTrain80.iloc[ :,3]

# test set
Xtest = RegTest20.iloc[ :,4:66]
    # y1 variable
y1_test  = RegTest20.iloc[ :,1]
    # y2 variable
y2_test  = RegTest20.iloc[ :,2]
    # y3 variable
y3_test = RegTest20.iloc[ :,3]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)