是否可以使用其中一种方法获取结构的名称?

时间:2019-02-01 10:34:35

标签: reflection types rust

例如:

struct ABC;

impl ABC {
    fn some_method(&self) -> &str {
        // return the name of its struct -> "ABC"
    }
}

我正在编写Python扩展,我需要一种方法来为其repr方法返回当前结构的名称。在Python中,我可以使用self.__class__.__name__来获得它。 Rust中有类似的东西吗?

1 个答案:

答案 0 :(得分:5)

每晚和# Load libraries import pandas from pandas.plotting import scatter_matrix import matplotlib.pyplot as plt from sklearn import model_selection # for command model_selection.cross_val_score from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.naive_bayes import GaussianNB from sklearn.svm import SVC # Load dataset url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv" names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class'] dataset = pandas.read_csv(url, names=names) # Split-out validation dataset array = dataset.values X = array[:,0:4] Y = array[:,4] validation_size = 0.20 seed = 7 X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed) # Test options and evaluation metric seed = 7 scoring = 'accuracy' #Below, we build and evaluate 6 different models # Spot Check Algorithms models = [] models.append(('LR', LogisticRegression())) models.append(('LDA', LinearDiscriminantAnalysis())) models.append(('KNN', KNeighborsClassifier())) models.append(('CART', DecisionTreeClassifier())) models.append(('NB', GaussianNB())) models.append(('SVM', SVC())) # evaluate each model in turn, we calculate the cv-scores, ther mean and std for each model # results = [] names = [] for name, model in models: #below, we do k-fold cross-validation kfold = model_selection.KFold(n_splits=10, random_state=seed) cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring) results.append(cv_results) names.append(name) msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std()) print(msg) 功能都可以实现:

ValueError: Target is multiclass but average='binary'. Please choose another average setting.