def run(self): encoding_dim = 32 svm=SVM(Input(shape=(encoding_dim,))) svm.train(self.trainingData,self.trainLabel,self.testData) ans = svm.test(self.testData) print("Classification report for classifier %s:n%sn" % (svm.clf, metrics.classification_report(self.testLabel, ans))) self.printaccuracy(self.testLabel,ans)
def main(): svm = SVM(C=100000, fraction_split=0.7) fraction_train = float(sys.argv[2]) svm.load_dataset(fraction_train) svm.train() accuracy, f1 = svm.test() return accuracy, f1
def train_and_test(svm,datasets): testset = datasets['testset'] finaltrainset = datasets['finaltrainset'] svm.train(finaltrainset) outputs, costs = svm.test(testset) id_to_class = {} for label, id in testset.class_to_id.iteritems(): id_to_class[id] = label # Ground truth lbl = datasets['ground_truth'] auto_lbl = np.array([int(id_to_class[output[0]]) for output in outputs]) # Predicted labels len_bg = testset.metadata['len_bg'] lbl = np.append(lbl, [0]*len_bg) auto_lbl = np.append(auto_lbl, [0]*len_bg) (dice, jaccard, precision, recall) = compute_statistics.compute_eval_multilabel_metrics(auto_lbl, lbl) dice = dice[~np.isnan(dice)] return dice.mean()
def main(): svm = SVM(C=100000, fraction_split=0.7) svm.load_dataset() accuracies = [] f1s = [] num_splits = int(sys.argv[2]) kf = KFold(n_splits=num_splits) split_num = 1 for training_indices, testing_indices in kf.split(svm.dataset): print("Split {}/{}".format(split_num, num_splits)) svm.update_train_and_test_sets(training_indices, testing_indices) svm.train() accuracy, f1 = svm.test() accuracies.append(accuracy) f1s.append(f1) split_num += 1 return stats.mean(accuracies), stats.mean(f1s)
# 读取训练文件 print('load TrainData') X_train, y_train = loadData('./mnist/mnist_train.csv') # 读取测试文件 print('load TestData') X_test, y_test = loadData('./mnist/mnist_test.csv') print('Init SVM classifier') svm=SVM(X_train[0:6000],y_train[0:6000]) print('start to train') svm.train() print('start to test') svm.test(X_test[0:400], y_test[0:400]) # clf = svm.SVC() # clf.fit(X_train[0:6000],y_train[0:6000]) # print(clf.score(X_test[0:400], y_test[0:400])) # 获取结束时间 end = time.time() print('run time:', end - start)
def main(): # 入力データのファイルパス LOAD_INPUT_DATA_PATH = "/Users/panzer5/github/sample/python/scikit/svm/ex4_data/train.csv" # 学習済みモデルデータの出力先パス SAVE_TRAINED_DATA_PATH = '/Users/panzer5/github/sample/python/scikit/svm/ex4_data/train.learn' # テストデータのファイルパス LOAD_TEST_DATA_PATH = "/Users/panzer5/github/sample/python/scikit/svm/ex4_data/test.csv" # グラフ出力先パス SAVE_GRAPH_IMG_PATH = '/Users/panzer5/github/sample/python/scikit/svm/ex4_data/graph.png' # 説明変数の列名 NAME_X = ["x1", "x2"] # 目的変数の列名 NAME_Y = "x3" # SVMのパラメータ GAMMA = 0.1 C = 1 KERNEL = "rbf" # クラスのデータとプロット時に割り当てる色 CLASS_DATAS = [0, 1, 2] CLASS_COLORS = ["blue", "red", "green"] svm = SVM() # 学習済みモデルの作成 svm.train(load_input_data_path=LOAD_INPUT_DATA_PATH, save_trained_data_path=SAVE_TRAINED_DATA_PATH, name_x=NAME_X, name_y=NAME_Y, gamma=GAMMA, C=C, kernel=KERNEL) # 学習済みモデルの検証 svm.test(load_trained_data_path=SAVE_TRAINED_DATA_PATH, load_test_data_path=LOAD_TEST_DATA_PATH, name_x=NAME_X, name_y=NAME_Y) # 未知データを入力して予測 """ svm.test( load_trained_data_path = SAVE_TRAINED_DATA_PATH, load_input_data_path = LOAD_INPUT_DATA_PATH, name_x = NAME_X, name_y = NAME_Y) """ # グラフにプロットして決定境界を可視化 svm.plot2d( load_input_data_path=LOAD_INPUT_DATA_PATH, load_trained_data_path=SAVE_TRAINED_DATA_PATH, save_graph_img_path=SAVE_GRAPH_IMG_PATH, class_datas=CLASS_DATAS, class_colors=CLASS_COLORS, name_x=NAME_X, name_y=NAME_Y, x1_name="x1", x2_name="x2", fig_size_x=10, fig_size_y=10, lim_font_size=25, )
def main(): # 学習済みモデルデータの出力先パス SAVE_TRAINED_DATA_PATH = 'C:/github/sample/python/scikit/svm/ex6_data/train.learn' # テスト用の画像データ(ペイントソフトで2と書いて保存したもの) LOAD_TEST_IMG_PATH = 'C:/github/sample/python/scikit/svm/ex6_data/test_2.png' # SVMのパラメータ GAMMA = 0.1 C = 1 KERNEL = "linear" # 手書き数字画像の場合はrbfだと学習結果が悪い svm = SVM() # 学習用のデータを読み込み(Digitsデータセットを利用) digits_dataset = datasets.load_digits() # 説明変数(学習データ:手書き数字の画像データ8*8, 2次元配列)を抽出 X = digits_dataset.images print("X1:", X[1]) # 目的変数:数字(0~9) y = digits_dataset.target # xの二次元配列を1次元に変換(-1で変換元の要素数に合わせて自動で値が決定:変換前要素数=変換後要素数となる) X = X.reshape((-1, 64)) # 説明変数のデータを、学習用データと検証用データに分割(学習用90%、検証用10%、シャッフルする) train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2, shuffle=True) print("train_X size:", train_X.shape) print("train_y size:", train_y.shape) print("test_X size:", test_X.shape) print("test_y size:", test_y.shape) # 学習済みモデルの作成 svm.train(save_trained_data_path=SAVE_TRAINED_DATA_PATH, train_X=train_X, train_y=train_y, gamma=GAMMA, C=C, kernel=KERNEL) # 学習済みモデルの検証 svm.test(load_trained_data_path=SAVE_TRAINED_DATA_PATH, test_X=test_X, test_y=test_y) # 学習済みモデルを使って予測 # OpenCVで任意の手書き数字画像をロードし, # グレースケール変換, 、白黒反転 # 8*8にリサイズ, 1次元配列に変換, # 値を0~16に収めてモデルに入力 test_img = cv2.imread(LOAD_TEST_IMG_PATH) test_gray = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY) test_gray = cv2.bitwise_not(test_gray) test_gray = cv2.resize(test_gray, (8, 8)) test_gray = test_gray.reshape(-1, 64) test_gray = np.clip(test_gray, 0, 16) predict_y = svm.predict(load_trained_data_path=SAVE_TRAINED_DATA_PATH, test_X=test_gray) print("test_X:", test_gray) print("predict_y:", predict_y) """
self.X_test = [] self.Y_test = [] self.X_test, self.Y_test = get_x_y(self.test_data) #print(self.X_test) self.svm = svm.SVR() def train(self): self.svm = self.svm.fit(self.X_train, self.Y_train) def test(self): RMSE = 0 N = len(self.Y_test) for i in range(N): y = self.svm.predict([self.X_test[i]]) y_hat = self.Y_test[i] RMSE += ((float(y[0]) - y_hat) ** 2) RMSE = (RMSE / N) ** 0.5 print(RMSE) return RMSE svm = SVM('train.csv', 'test.csv') svm.train() svm.test()
def main(): # 学習済みモデルデータの出力先パス SAVE_TRAINED_DATA_PATH = '/Users/panzer5/github/sample/python/scikit/svm/ex5_data/train.learn' # グラフ出力先パス SAVE_GRAPH_IMG_PATH = '/Users/panzer5/github/sample/python/scikit/svm/ex5_data/graph_x2_x3.png' # SVMのパラメータ GAMMA = 0.1 C = 1 KERNEL = "rbf" # クラスのデータとプロット時に割り当てる色 CLASS_DATAS = [0, 1, 2] CLASS_COLORS = ["blue", "red", "green"] svm = SVM() # 学習用のデータを読み込み(Irisデータセットを利用) iris_dataset = load_iris() # 説明変数(学習データ)を抽出 X = iris_dataset.data # 目的変数:アヤメの品種('setosa'=0 'versicolor'=1 'virginica'=2) y = iris_dataset.target X1 = np.vstack((X[:, :1])) #sepal length(ガクの長さ)を取得 X2 = np.vstack((X[:, 1:2])) #sepal width(ガクの幅)を取得 X3 = np.vstack((X[:, 2:3])) #petal length(花弁の長さ)を取得 X4 = np.vstack((X[:, 3:4])) #petal width(花弁の幅)を取得 # 学習に使用する説明変数を選択 X = np.hstack((X1, X2, X3, X4)) #X = np.hstack((X1, X2)) #X = np.hstack((X2, X3)) #X = np.hstack((X3, X4)) # 説明変数のデータを、学習用データと検証用データに分割(学習用90%、検証用10%、シャッフルする) train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.1, shuffle=True) print("train_X size:", train_X.shape) print("train_y size:", train_y.shape) print("test_X size:", test_X.shape) print("test_y size:", test_y.shape) # 学習済みモデルの作成 svm.train(save_trained_data_path = SAVE_TRAINED_DATA_PATH, train_X=train_X, train_y=train_y, gamma = GAMMA, C = C, kernel = KERNEL) # 学習済みモデルの検証 svm.test( load_trained_data_path = SAVE_TRAINED_DATA_PATH, test_X=test_X, test_y=test_y) # 学習済みモデルを使って予測 predict_y = svm.predict( load_trained_data_path = SAVE_TRAINED_DATA_PATH, test_X=test_X) print("test_X:", test_X) print("predict_y:", predict_y) """ # グラフにプロットして決定境界を可視化(説明変数2つで学習したときのみ利用可能) svm.plot2d(load_trained_data_path = SAVE_TRAINED_DATA_PATH, save_graph_img_path = SAVE_GRAPH_IMG_PATH, train_X=train_X, train_y=train_y, class_datas = CLASS_DATAS, class_colors = CLASS_COLORS, x1_name = "x2", x2_name = "x3", fig_size_x = 10, fig_size_y = 10, lim_font_size = 25, ) """ """