如何清除“文本字段”

时间:2019-01-21 16:08:55

标签: tornadofx

我正在尝试将文本写入文本字段,清除文本并写入新文本。我无法摆脱旧的文字。新的写在旧的上,所以我都看到了。我正在使用choosefile,并尝试在文本字段中显示所选文件,以便我可以确认选择。

from numpy import genfromtxt
import numpy as np
import matplotlib
matplotlib.use('TKAgg')
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.decomposition import PCA
import pylab as pl
from itertools import cycle
from sklearn import cross_validation
from sklearn.svm import SVC 

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

#Loading and pruning the data
dataset = genfromtxt('cleveland_data.csv',dtype = float, delimiter=',')
#print dataset
X = dataset[:,0:12] #Feature Set
y = dataset[:,13]   #Label Set

#Replacing 1-4 by 1 label
for index, item in enumerate(y):
    if not (item == 0.0):
        y[index] = 1
print(y)
target_names = ['0', '1']

#Method to plot the graph for reduced Dimesions
def plot_2D(data, target, target_names):
     colors = cycle('rgbcmykw')
     target_ids = range(len(target_names))
     plt.figure()
     for i, c, label in zip(target_ids, colors, target_names):
         plt.scatter(data[target == i, 0], data[target == i, 1],
                    c=c, label=label)
     plt.legend()
     plt.savefig('Problem 2 Graph')

# Classifying the data using a Linear SVM and predicting the probability of disease belonging to a particular class
modelSVM = LinearSVC(C=0.001)
pca = PCA(n_components=5, whiten=True).fit(X)
X_new = pca.transform(X)

# calling plot_2D
plot_2D(X_new, y, target_names)


#Applying cross validation on the training and test set for validating our Linear SVM Model
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X_new, y, test_size=0.2, train_size=0.8, random_state=0)
modelSVM = modelSVM.fit(X_train, y_train)
print("Linear SVC values with split")
print(modelSVM.score(X_test, y_test))

modelSVMRaw = LinearSVC(C=0.001)
modelSVMRaw = modelSVMRaw.fit(X_new, y)
cnt = 0
for i in modelSVMRaw.predict(X_new):
    if i == y[i]:
       cnt = cnt+1
print("Linear SVC score without split")
print(int(cnt)//303)

#Applying the Principal Component Analysis on the data features
modelSVM2 = SVC(C=0.001,kernel='rbf')

#Applying cross validation on the training and test set for validating our Linear SVM Model
X_train1, X_test1, y_train1, y_test1 = cross_validation.train_test_split(X_new, y, test_size=0.2, train_size=0.8, random_state=0)
modelSVM2 = modelSVM2.fit(X_train1, y_train1)
print("RBF score with split")
print(modelSVM2.score(X_test1, y_test1))

modelSVM2Raw = SVC(C=0.001,kernel='rbf')
modelSVM2Raw = modelSVM2Raw.fit(X_new, y)
cnt1 = 0
for i in modelSVM2Raw.predict(X_new):
        if i == y[i]:
           cnt1 = cnt1+1
print("RBF score without split")
print(float(cnt1)//303)

#Using Stratified K Fold
skf = cross_validation.StratifiedKFold(y, n_folds=5)
for train_index, test_index in skf:
   # print("TRAIN:", train_index, "TEST:", test_index)
    X_train3, X_test3 = X[train_index], X[test_index]
    y_train3, y_test3 = y[train_index], y[test_index]
modelSVM3 = SVC(C=0.001,kernel='rbf')
modelSVM3 = modelSVM3.fit(X_train3, y_train3)
print("Stratified K fold score")
print(modelSVM3.score(X_test3, y_test3))

modelSVM3Raw = SVC(C=0.001,kernel='rbf')
modelSVM3Raw = modelSVM3Raw.fit(X_new, y)
cnt2 = 0
for i in modelSVM3Raw.predict(X_new):
        if i == y[i]:
           cnt2 = cnt2+1
print("On PCA valued X_new")
print(float(cnt2)//303)

        #Text interpretation
fig = plt.figure(figsize=(5, 1.5))
t = fig.text(0.5, 0.5, 'Problem 1 \nTesting Linear SVC values using Split \n0.5491803278688525 \nTesting with RBF using split \n0.4918032786885246 \nTesting using stratified with K folds \n0.5423728813559322',
ha='center', va='center', size=15)
text.set_path_effects([path_effects.path_2d()])
plt.show()

我希望在文本字段中看到“ 999”,但我在同一位置看到了678和999。

1 个答案:

答案 0 :(得分:0)

您实际上是在文本字段上调用text()构建器,因此您基本上是在文本字段内创建两个文本元素。我认为您应该先退后一步,然后再阅读指南:)这是代码的清理版本:

class TestView : View("My View") {
    val filename = SimpleStringProperty()

    override val root = borderpane {
        center {
            form {
                fieldset("Main") {
                    field("File") {
                        textfield(filename)
                        button("Browse...").action {
                            val filters = arrayOf(FileChooser.ExtensionFilter("All Files", "*.*"))
                            chooseFile("Choose file...", filters).firstOrNull()?.let {
                                filename.value = it.path
                            }
                        }
                    }
                }
            }
        }
    }
}

Result