[英]Issue with subscript (script instantiated and called from another script) only running the called function, but not the entire script
Goal: From the superscript, per each iteration of the for loop, send a list to a function in the subscript, update that list, exit the function in the subscript, and perform several actions, then onto the next iteration.目标:从上标开始,for 循环的每次迭代,将列表发送到下标中的 function,更新该列表,退出下标中的 function,并执行几个操作,然后进入下一次迭代。
Issue: Only the function I call from the superscript is run and not the entire subscript.问题:只有我从上标调用的 function 运行,而不是整个下标。
CrossValidationSmall.py (superscript) CrossValidationSmall.py(上标)
import subfileTest
data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K']
n = 2
for start in range(0, len(data), n):
stop = start + n
test = data[start: stop]
train = data[:start] + data[stop:]
subfileTest.set_train_data(train)
subfileTest.py (subscript) subfileTest.py(下标)
train_data = []
def set_train_data(train):
global train_data
train_data = train
print(train_data) #should output the lists each time subfileTest is called.
Expected Output预计 Output
MacBook-Pro:SmallerEnviromentTest Me$ python CrossValidationSmall.py
['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'G', 'H', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'I', 'J', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
Current Output当前 Output
MacBook-Pro:SmallerEnviromentTest Me$ python CrossValidationSmall.py
[]
From what I understood, you need your subscript to be called in entirety on every iteration.据我了解,您需要在每次迭代时完整调用您的下标。
Here's the thing:事情是这样的:
Since you need you subscript to be run fresh on each iteration, a Class can be used.由于您需要在每次迭代时重新运行下标,因此可以使用 Class。 an object of the class to: class 的 object 至:
Superscript :上标:
from subscript import ModelCrossValidator
trial = 0
data = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
n = 2
for start in range(0, len(data), n):
trial += 1
stop = start + n
test = data[start: stop]
# train_subset to be used for cross validation
train_subset = data[:start] + data[stop:]
cross_validator = ModelCrossValidator(train_subset)
cross_val_score = cross_validator.calc_score()
print("CrossValSet: {}, Data: {}, Score: {:.2}".format(trial, cross_validator.data, cross_val_score))
Subscript :下标:
import random
class ModelCrossValidator:
def __init__(self, train_subset):
self.data = train_subset # your folded cross-val data
self.model = None # can pass in a model too here
self.accuracy = 0 # other fields that you might need
def calc_score(self):
# you can have any number of functions like this.
# all will deal with only the data from a single object.
self.accuracy = 0.8 + (random.random()) % 0.2
return self.accuracy
Output: Output:
Macbook-Pro: chimichanga$ python superscript.py
CrossValSet: 1, Data: ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'], Score: 0.89
CrossValSet: 2, Data: ['A', 'B', 'E', 'F', 'G', 'H', 'I', 'J', 'K'], Score: 0.94
CrossValSet: 3, Data: ['A', 'B', 'C', 'D', 'G', 'H', 'I', 'J', 'K'], Score: 0.9
CrossValSet: 4, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'J', 'K'], Score: 0.84
CrossValSet: 5, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K'], Score: 0.88
CrossValSet: 6, Data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], Score: 0.94
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.