def main(): import pandas as pd ''' 実行時に引数を取れるように設定 ''' ps = support.get_base_args() ps.add_argument('--metric', '-m', default='', help='Metric function') ps.add_argument('--leaf', '-l', default='', help='Leaf class') args = ps.parse_args() df = pd.read_csv(args.input, sep=args.separator, header=args.header, index_col=args.indexcol) x = df[df.columns[:-1]].values ##argsの引数によってどの関数を使うか定義 ##metric if args.metric == 'div': mt = entropy.deviation_org elif args.metric == 'infgain': mt = entropy.infgain elif args.metric == 'gini': mt = entropy.gini else: mt = None ##leaf if args.leaf == 'zeror': lf = ZeroRule elif args.leaf == 'linear': lf = Linear else: lf = None ##回帰かクラス分類か if not args.regression: ##クラス分類 y, clz = support.clz_to_prob(df[df.columns[-1]]) if mt is None: mt = entropy.gini if lf is None: lf = ZeroRule plf = DecisionStump(metric=mt, leaf=lf) support.report_classifer(plf, x, y, clz, args.crossvalidate) else: y = df[df.columns[-1]].values.reshape((-1, 1)) if mt is None: mt = entropy.deviation_org if leaf is None: lf = Linear plf = DecisionStump(metric=mt, leaf=lf) support.report_regressor(plf, x, y, args.crossvalidate)
def main(): import pandas as pd ps = support.get_base_args() ps.add_argument('--metric', '-m' , default='' , help='Metric function') ps.add_argument('--leaf' , '-l', default='',help='Leaf class') ps.add_argument('--depth' , '-d' , type=int , default=5,help='Max Tree Depth') args = ps.parse_args() df = pd.read_csv(args.input, sep=args.separator , header=args.header, index_col = args.indexcol) x = df[df.columns[:-1]].values ##分岐関数の指定 if args.metric == 'div': mt = entropy.deviation_org elif args.metric == 'infgain': mt = entropy.infgain elif args.metric == 'gini': mt = entropy.gini else: mt = None ##葉の関数の指定 if args.leaf == 'zeror': lf = ZeroRule elif args.leaf == 'linear': lf = Linear else: lf =None ##回帰かクラス分類か if not args.regression: y,clz = support.clz_to_prob(df[df.columns[:-1]]) if mt is None: mt = entropy.gini if lf is None: lf = ZeroRule plf = DecisionTree(metric=mt , leaf=lf, max_depth=args.depth) support.report_classifer(plf,x,y,clz,args.crossvalidate) else: y = df[df.columns[-1]].values.reshape((-1,1)) if mt is None: mt = entropy.deviation_org if lf is None: lf = Linear plf = DecisionTree(metric=mt ,leaf=lf , max_depth=args.depth) support.report_regressor(plf,x,y,args.crossvalidate)
help='test size for pruning') ps.add_argument('--critical', '-l', type=float, default=0.8, help='value for critical') args = ps.parse_args() df = pd.read_csv(args.input, sep=args.separator, header=args.header, index_col=args.indexcol) x = df[df.columns[:-1]].values if not args.regression: y, clz = support.clz_to_prob(df[df.columns[-1]]) mt = entropy.gini lf = ZeroRule plf = PrunedTree(prunfnc=args.pruning, pruntest=args.test, splitratio=args.ratio, critical=args.critical, metric=mt, leaf=lf, max_depth=args.depth) plf.fit(x, y) support.report_classifier(plf, x, y, clz, args.crossvalidate) else: y = df[df.columns[-1]].values.reshape((-1, 1)) mt = entropy.deviation lf = linear
def main(): ###ベンチマークのアルゴリズムとアルゴリズムを実装したモデルの一覧 models = [ ('SVM', SVC(random_state=1), SVR()), ('GaussianProcess', GaussianProcessClassifier(random_state=1), GaussianProcessRegressor(normalize_y=True, alpha=1, random_state=1)), ('KNeighbors', KNeighborsClassifier(), KNeighborsRegressor()), ('MLP', MLPClassifier(random_state=1), MLPRegressor(hidden_layer_sizes=(5), solver='lbfgs', random_state=1)), ] ###データセットの用意 ###検証用のデータセットのファイルを定義する. ###検証用のデータセットはファイルによって区切り文字、ヘッダーの行数、インデックスとなる列の違いがあるためにそれぞれに対応させる classifier_files = ['iris.data', 'sonar.all-data', 'glass.data'] classifier_params = [(',', None, None), (',', None, None), (',', None, 0)] regressor_files = [ 'airfoil_self_noise.dat', 'winequality-red.csv', 'winequality-white.csv' ] regressor_params = [(r'\t', None, None), (';', 0, None), (';', 0, None)] ##評価スコアをcsvに書き出しする表の作成 result = pd.DataFrame( columns=['target', 'function'] + [m[0] for m in models], index=range(len(classifier_files + regressor_files) * 2)) ##クラス分類アルゴリズムを評価する ncol = 0 for i, (c, p) in enumerate(zip(classifier_files, classifier_params)): df = pd.read_csv(c, sep=p[0], header=p[1], index_col=p[2]) x = df[df.columns[:-1]].values ##ラベルをラベル番号とそのラベルに属する可能性の配列で表現する y, clz = support.clz_to_prob(df[df.columns[-1]]) ##結果の表にファイル名からデータセットの種類と評価関数の行を作る result.loc[ncol, 'target'] = re.split(r'[._]', c)[0] result.loc[ncol + 1, 'target'] = '' result.loc[ncol, 'function'] = 'F1score' result.loc[ncol + 1, 'function'] = 'Accuracy' ##全てのアルゴリズムをループで実行して、交差検証のスコアを返す ##cross_validationは評価関数の名前に「train_」or「test_」をつけた名前が返されるディクショナリのキーになっている for l, c_m, r_m in models: kf = KFold(n_splits=5, random_state=1, shuffle=True) s = cross_validate(c_m, x, y.argmax(axis=1), cv=kf, scoring=('f1_weighted', 'accuracy')) result.loc[ncol, l] = np.mean(s['test_f1_weighted']) result.loc[ncol + 1, l] = np.mean(s['test_accuracy']) ncol += 2 ##回帰アルゴリズムを評価する for i, (c, p) in enumerate(zip(regressor_files, regressor_params)): ##ファイルを読み込む df = pd.read_csv(c, sep=p[0], header=p[1], index_col=p[2]) x = df[df.columns[:-1]].values y = df[df.columns[-1]].values.reshape((-1, )) ##結果の表にファイル名からデータセットの種類と評価関数用の行を作る result.loc[ncol, 'target'] = re.split(r'[._]', c)[0] result.loc[ncol + 1, 'target'] = '' result.loc[ncol, 'function'] = 'R2 score' result.loc[ncol + 1, 'function'] = 'Meansquared' ##全てのアルゴリムを回す for l, c_m, r_m in models: kf = KFold(n_splits=5, random_state=1, shuffle=True) s = cross_validate(r_m, x, y, cv=kf, scoring=('r2', 'neg_mean_squared_error')) result.loc[ncol, l] = np.mean(s['test_r2']) result.loc[ncol + 1, l] = -np.mean(s['test_neg_mean_squared_error']) ncol += 2 print(result)