Python 从一个“变量”中获取所有预定义变量的子程序;“主程序”;
我很难做到这一点: 由于我正在使用pyqt编程GUI,因此我希望构建我的工作: 我的GUI上有几个按钮,可以通过使用scikitlearn进行的计算调用“不同的子程序” 我有一个按钮“PRED”用于预测,另一个按钮用于一些绘图,称为“绘图” 单击这些按钮时,将调用一个python“计算程序”Python 从一个“变量”中获取所有预定义变量的子程序;“主程序”;,python,function,pyqt,scikit-learn,Python,Function,Pyqt,Scikit Learn,我很难做到这一点: 由于我正在使用pyqt编程GUI,因此我希望构建我的工作: 我的GUI上有几个按钮,可以通过使用scikitlearn进行的计算调用“不同的子程序” 我有一个按钮“PRED”用于预测,另一个按钮用于一些绘图,称为“绘图” 单击这些按钮时,将调用一个python“计算程序” class MyDia(QtGui.QDialog, Dlg): def __init__(self): QtGui.QDialog.__init__(self)
class MyDia(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.connect(self.buttonOPLOT,
QtCore.SIGNAL("clicked()"), self.onPLOT)
self.connect(self.buttonPRED,
QtCore.SIGNAL("clicked()"), self.onPRED)
def onPRED
if self.button_1.checkState():
a=1
if self.button_2.checkState():
a=2
query=np.zeros((1,18))
for i in range(0,18,1):
try:
query[0,i]= float(self.tableWidget.item(0,i).text())
### when user has made his choices the data goes do this
from sk_calc import main, pred
main() #after main, "pred" should be called with some definitions that
have been made in "main"
pred(a) #a is some parameter of a regression (i try to keep it easy)
目前,我在不同的文件中使用不同的“计算”程序“sk_plot和sk_pred”-目标是只更改一个。。。其中“main”在指定作业之前运行(PRED或PLOT…)
唯一计算程序的“外观”/结构应与此类似:
def main():
import numpy as np
import #all modules from scikitlearn
DATA=np.genfromtxt(direc+"\some.csv",delimiter=";",dtype=float ,skip_header=2, usecols=range(0,22)) #reading in a csv file with my data
features=DATA[:,4:22]#the "X" of my DATA
targets=DATA[:,1]#the "Y" of my DATA
svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a) #Regression using the DATA #a comes from user click
svr_rbf.fit(features, targets).predict(features)# method of scikit-learn
def pred():
Prediction=svr_rbf.predict(query)
#query is defined by the user in the gui typing in some values
print(Pred_ic)
def plot():
#... something different using pylab but ALSO DATA features and targets
您可以看到,我希望某些代码(main)在单击该按钮时不独立地运行
之后,应执行“计算程序”的一部分,该部分包含main()中定义的变量和数据
我是否为此使用类?如果是,我需要记住什么?这方面的步骤是什么…类是构造代码的好方法,这是正确的 类可以维护自己的状态,并具有预定义的行为,这些行为可以通过方法和属性进行操作 然而,我不会给出关于使用类的一般性建议,因为这与stackoverflow无关,它关注的是特定的编程问题。如果您想了解更多,只需在web上搜索有关python的书籍/教程就可以了——有几十本不错的 相反,我将尽我所能重新构造问题中的代码,以使用类。以下代码仅用于说明目的。它不是一个完整的、可运行的示例。希望这里有足够的提示让您了解如何继续: gui.py:
import numpy as np
import sk_calc
class MyDia(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.buttonOPLOT.clicked.connect(self.onPLOT)
self.buttonPRED.clicked.connect(self.onPRED)
def onPRED(self):
if self.button_1.isChecked():
a = 1
elif self.button_2.isChecked():
a = 2
else:
a = 0
query = np.zeros((1,18))
# ... etc
# when user has made his choices the data goes do this
# create an instance of the Calc class, passing in
# parameters from the gui
calc = sk_calc.Calc(a)
# call methods of the instance, passing in parameters
# from the gui, and receiving returned values
prediction = calc.pred(query)
# calc.plot() ... etc
import numpy as np
from sklearn.svm import SVR
# import other stuff from scikitlearn
DEFAULT_CSVPATH = 'path/to/some/file.csv'
class Calc(object):
def __init__(self, a, csvpath=None):
if csvpath is None:
csvpath = DEFAULT_CSVPATH
# reading in a csv file with my data
self.data = np.genfromtxt(
csvpath , delimiter=';', dtype=float,
skip_header=2, usecols=range(0,22))
self.features = data[:,4:22] # the "X" of my DATA
self.targets = data[:,1] # the "Y" of my DATA
# Regression using the DATA, a comes from user click
self.svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a)
# method of scikit-learn
self.svr_rbf.fit(features, targets).predict(features)
def pred(self, query):
# query is defined by the user in the gui typing in some values
prediction = self.svr_rbf.predict(query)
return prediction
def plot(self):
# ... use pylab with DATA features and targets
# self.data ...
# self.features ...
sk_calc.py:
import numpy as np
import sk_calc
class MyDia(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.buttonOPLOT.clicked.connect(self.onPLOT)
self.buttonPRED.clicked.connect(self.onPRED)
def onPRED(self):
if self.button_1.isChecked():
a = 1
elif self.button_2.isChecked():
a = 2
else:
a = 0
query = np.zeros((1,18))
# ... etc
# when user has made his choices the data goes do this
# create an instance of the Calc class, passing in
# parameters from the gui
calc = sk_calc.Calc(a)
# call methods of the instance, passing in parameters
# from the gui, and receiving returned values
prediction = calc.pred(query)
# calc.plot() ... etc
import numpy as np
from sklearn.svm import SVR
# import other stuff from scikitlearn
DEFAULT_CSVPATH = 'path/to/some/file.csv'
class Calc(object):
def __init__(self, a, csvpath=None):
if csvpath is None:
csvpath = DEFAULT_CSVPATH
# reading in a csv file with my data
self.data = np.genfromtxt(
csvpath , delimiter=';', dtype=float,
skip_header=2, usecols=range(0,22))
self.features = data[:,4:22] # the "X" of my DATA
self.targets = data[:,1] # the "Y" of my DATA
# Regression using the DATA, a comes from user click
self.svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a)
# method of scikit-learn
self.svr_rbf.fit(features, targets).predict(features)
def pred(self, query):
# query is defined by the user in the gui typing in some values
prediction = self.svr_rbf.predict(query)
return prediction
def plot(self):
# ... use pylab with DATA features and targets
# self.data ...
# self.features ...
类是构造代码的好方法,这是正确的 类可以维护自己的状态,并具有预定义的行为,这些行为可以通过方法和属性进行操作 然而,我不会给出关于使用类的一般性建议,因为这与stackoverflow无关,它关注的是特定的编程问题。如果您想了解更多,只需在web上搜索有关python的书籍/教程就可以了——有几十本不错的 相反,我将尽我所能重新构造问题中的代码,以使用类。以下代码仅用于说明目的。它不是一个完整的、可运行的示例。希望这里有足够的提示让您了解如何继续: gui.py:
import numpy as np
import sk_calc
class MyDia(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.buttonOPLOT.clicked.connect(self.onPLOT)
self.buttonPRED.clicked.connect(self.onPRED)
def onPRED(self):
if self.button_1.isChecked():
a = 1
elif self.button_2.isChecked():
a = 2
else:
a = 0
query = np.zeros((1,18))
# ... etc
# when user has made his choices the data goes do this
# create an instance of the Calc class, passing in
# parameters from the gui
calc = sk_calc.Calc(a)
# call methods of the instance, passing in parameters
# from the gui, and receiving returned values
prediction = calc.pred(query)
# calc.plot() ... etc
import numpy as np
from sklearn.svm import SVR
# import other stuff from scikitlearn
DEFAULT_CSVPATH = 'path/to/some/file.csv'
class Calc(object):
def __init__(self, a, csvpath=None):
if csvpath is None:
csvpath = DEFAULT_CSVPATH
# reading in a csv file with my data
self.data = np.genfromtxt(
csvpath , delimiter=';', dtype=float,
skip_header=2, usecols=range(0,22))
self.features = data[:,4:22] # the "X" of my DATA
self.targets = data[:,1] # the "Y" of my DATA
# Regression using the DATA, a comes from user click
self.svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a)
# method of scikit-learn
self.svr_rbf.fit(features, targets).predict(features)
def pred(self, query):
# query is defined by the user in the gui typing in some values
prediction = self.svr_rbf.predict(query)
return prediction
def plot(self):
# ... use pylab with DATA features and targets
# self.data ...
# self.features ...
sk_calc.py:
import numpy as np
import sk_calc
class MyDia(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.buttonOPLOT.clicked.connect(self.onPLOT)
self.buttonPRED.clicked.connect(self.onPRED)
def onPRED(self):
if self.button_1.isChecked():
a = 1
elif self.button_2.isChecked():
a = 2
else:
a = 0
query = np.zeros((1,18))
# ... etc
# when user has made his choices the data goes do this
# create an instance of the Calc class, passing in
# parameters from the gui
calc = sk_calc.Calc(a)
# call methods of the instance, passing in parameters
# from the gui, and receiving returned values
prediction = calc.pred(query)
# calc.plot() ... etc
import numpy as np
from sklearn.svm import SVR
# import other stuff from scikitlearn
DEFAULT_CSVPATH = 'path/to/some/file.csv'
class Calc(object):
def __init__(self, a, csvpath=None):
if csvpath is None:
csvpath = DEFAULT_CSVPATH
# reading in a csv file with my data
self.data = np.genfromtxt(
csvpath , delimiter=';', dtype=float,
skip_header=2, usecols=range(0,22))
self.features = data[:,4:22] # the "X" of my DATA
self.targets = data[:,1] # the "Y" of my DATA
# Regression using the DATA, a comes from user click
self.svr_rbf = SVR(kernel='rbf', C=2e4, gamma=a)
# method of scikit-learn
self.svr_rbf.fit(features, targets).predict(features)
def pred(self, query):
# query is defined by the user in the gui typing in some values
prediction = self.svr_rbf.predict(query)
return prediction
def plot(self):
# ... use pylab with DATA features and targets
# self.data ...
# self.features ...
你能概括你的问题吗?比如,你到底需要什么。从函数导入变量?将变量传递给正在运行的脚本?当我运行函数的“子部分”时,我希望我在函数的main()中定义的所有内容都可以在此子部分中使用-变量名称、值等…你能概括你的问题吗?比如,你到底需要什么。从函数导入变量?将变量传递给正在运行的脚本?当我运行函数的“子部分”时,我希望我在函数的main()中定义的所有内容都可以在该子部分中使用—变量名称、值等。感谢您的努力,我将阅读更多关于类的信息,并尝试您的方法!谢谢完成这项工作的工作量并不大——我基本上需要添加一个“self”。在使用“init”部分中定义的变量时,感谢您的努力,我将阅读更多关于类的内容,并尝试您的方法!谢谢完成这项工作的工作量并不大——我基本上需要添加一个“self”。在“init”部分中定义的所有变量都被使用