Пример #1
0
reload(sys)
sys.setdefaultencoding('utf8')
import os
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR + "/../Model")
sys.path.append(ROOT_DIR + "/../../0_FeatureMaker")
import numpy as np
from feature_maker import FeatureMaker
from nn import NeuralNetwork
from regularizor import L2Regularization
from learning_rate import MomentumLearningRate

src = ROOT_DIR + '/../../Data/iris/iris_data.csv'
feature_types = [1, 1, 1, 1, 0]
feature_maker = FeatureMaker(src=src,
                             delimiter=',',
                             types=feature_types,
                             norm=True)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1)
# print("X : {}".format(X))
# print("Y : {}".format(Y))

train_X, test_X, train_Y, test_Y = feature_maker.train_test_split(
    X, Y, test_size=0.2, random_state=1234)
# train_X, test_X, train_Y, test_Y = train_X[:5,:], test_X[:5,:], train_Y[:5,:], test_Y[:5,:]
print("Train X : {}\n...".format(train_X[:5, :]))
print("Train Y : {}\n...".format(train_Y[:5, :]))
print("Test X : {}".format(test_X))
print("Test Y : {}".format(test_Y))

N_input = np.reshape(X[0], -1).shape[0]
N_output = np.reshape(Y[0], -1).shape[0]
Пример #2
0
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
# sys.path.append(ROOT_DIR+"/../Model")
sys.path.append(ROOT_DIR+"/../../0_FeatureMaker")

import numpy as np
from feature_maker import FeatureMaker
import json
from sklearn.svm import LinearSVC
from sklearn.metrics import hinge_loss


src = ROOT_DIR+'/../../Data/iris/iris_data.csv'
feature_types = [1,1,1,1,0]
# src = ROOT_DIR+'/../../Data/iris/iris_data.csv'
# feature_types = [1,1,1,1,0]
feature_maker = FeatureMaker(src=src, delimiter=',', types=feature_types, norm=True)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1, one_hot=True)
### 将数据集做成二分类
X = X[Y[:,0]!=1,:]
Y = Y[Y[:,0]!=1,:]
# print(Y)
Y = np.array([[1] if y[0]>1 else [-1] for y in Y])
# Y = Y.flatten()
print("X.shape:{}\nY.shape:{}".format(X.shape, Y.shape))

Xtrain, Xtest, Ytrain, Ytest = feature_maker.train_test_split(X, Y, test_size=0.3)
Xmarked, Xunmarked, Ymarked, Yunmarked = feature_maker.train_test_split(Xtrain, Ytrain, test_size=6.0/7)
Ytest = Ytest.flatten()
Ymarked = Ymarked.flatten()
Yunmarked = Yunmarked.flatten()
print("Xmarked.shape:{} Ymarked.shape:{}\nXunmarked.shape:{} Yunmarked.shape:{}\nXtest.shape:{} Ytest.shape:{}".format(Xmarked.shape, Ymarked, Xunmarked.shape, Yunmarked, Xtest.shape, Ytest))
#! coding:utf-8
## Assuming you had libsvm for python installed.

import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR + "/../Model")
sys.path.append(ROOT_DIR + "/../../0_FeatureMaker")
import numpy as np
from feature_maker import FeatureMaker
from libsvm.svmutil import *

src = ROOT_DIR + '/../../Data/iris/iris_data.csv'
feature_types = [1, 1, 1, 1, 0]
feature_maker = FeatureMaker(src=src,
                             delimiter=',',
                             types=feature_types,
                             norm=True)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1)
Y = [len(y) - y.tolist().index(1) for y in Y]
print("X:{}\nY:{}".format(X, Y))

problem = svm_problem(Y, X)
params = svm_parameter('-s 0 -t 2 -c 8 -v 10')
model = svm_train(problem, params)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR + "/../Model")
sys.path.append(ROOT_DIR + "/../../0_FeatureMaker")
import numpy as np
from feature_maker import FeatureMaker
import json
import bisect

src = ROOT_DIR + '/../../Data/watermelon/watermelon_3.0.csv'
feature_types = [0, 0, 0, 0, 0, 0, 1, 1, 0]
feature_maker = FeatureMaker(src=src,
                             delimiter=',',
                             types=feature_types,
                             norm=False)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1, one_hot=True)
Y = Y.flatten()
print("X:{}\nY:{}".format(X, Y))

### train
print("Trianing begin...")
num_sample = X.shape[0]
num_continuous_feature = np.sum(feature_types[:-1])
discrete_X = X[:, :-num_continuous_feature]
continuous_X = X[:, num_continuous_feature:]

### label种类数
num_class = len(feature_maker.feature2index[-1])
Пример #5
0
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR + "/../Model")
sys.path.append(ROOT_DIR + "/../../0_FeatureMaker")
import numpy as np
from feature_maker import FeatureMaker
import json

src = ROOT_DIR + '/../../Data/watermelon/watermelon_3.0.csv'
feature_types = [0, 0, 0, 0, 0, 0, 1, 1, 0]
feature_maker = FeatureMaker(src=src,
                             delimiter=',',
                             types=feature_types,
                             norm=False)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1, one_hot=False)
Y = Y.flatten()
print("X:{}\nY:{}".format(X, Y))

class_prior = {}
class_cond = {}
x_feature_types = feature_types[:-1]

### train
for x, y in zip(X, Y):
    # print("x:{} y:{}".format(x, y))
    if class_prior.get("total") is None:
        class_prior["total"] = 0
    class_prior["total"] += 1