本文整理汇总了Python中sklearn.preprocessing.normalize函数的典型用法代码示例。如果您正苦于以下问题:Python normalize函数的具体用法?Python normalize怎么用?Python normalize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了normalize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, hps, example_list, dqn_batch_size, use_state_prime = False, max_art_oovs = 0):
"""
Args:
hps: seq2seq model parameters
example_list: list of experiences
dqn_batch_size: DDQN batch size
use_state_prime: whether to use the next decoder state to make the batch or the current one
max_art_oovs: number of OOV tokens in current batch
Properties:
_x: The input to DDQN model for training, this is basically the decoder output (dqn_batch_size, dqn_input_feature_len)
_y: The Q-estimation (dqn_batch_size, vocab_size)
_y_extended: The Q-estimation (dqn_batch_size, vocab_size + max_art_oovs)
"""
self._x = np.zeros((dqn_batch_size, hps.dqn_input_feature_len))
self._y = np.zeros((dqn_batch_size, hps.vocab_size))
self._y_extended = np.zeros((dqn_batch_size, hps.vocab_size + max_art_oovs))
for i,e in enumerate(example_list):
if use_state_prime:
self._x[i,:]=e.state_prime
else:
self._x[i,:]=e.state
self._y[i,:]=normalize([e.q_value[0:hps.vocab_size]], axis=1, norm='l1')
if max_art_oovs == 0:
self._y_extended[i,:] = normalize([e.q_value[0:hps.vocab_size]], axis=1, norm='l1')
else:
self._y_extended[i,:] = e.q_value
示例2: split_and_build_class
def split_and_build_class(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
print X_train.shape
print X_test.shape
# Normalize the input data.
imp = preprocessing.Imputer(missing_values='NaN', strategy='mean', axis=0)
fixed_X_train = X_train[:, 1:]
imp.fit(fixed_X_train)
fixed_X_train = imp.transform(fixed_X_train)
preprocessing.normalize(fixed_X_train, copy=False)
X_train[:, 1:] = fixed_X_train
fixed_X_test = X_test[:, 1:]
imp.fit(fixed_X_test)
fixed_X_test = imp.transform(fixed_X_test)
preprocessing.normalize(fixed_X_test, copy=False)
X_test[:, 1:] = fixed_X_test
train_data = read_dataset.microData()
train_data.get_data(X_train)
y_train = train_data.set_output(y_train)
test_data = read_dataset.microData()
test_data.get_data(X_test)
y_test = test_data.set_output(y_test)
return [X_train, X_test, y_train, y_test, train_data, test_data]
开发者ID:Shauro,项目名称:From-Fog-Nets-to-Neural-Nets,代码行数:27,代码来源:cross_validation_random_forest_regressor.py
示例3: func
def func(A, B): # comparematrices(A,B):
colA = A.shape[1]
colB = B.shape[1]
# method 1 - n is small dim, m is larger, matnew is new comparison matrix
if colA == colB and colA != 1:
Aprime = normalize(A, axis=1, norm='l2')
Bprime = normalize(B, axis=1, norm='l2')
if colA == 1:
dist = np.linalg.norm(Aprime - Bprime) # L2 norm (vectors)
else:
dist = np.linalg.norm(Aprime - Bprime, 2) # Frobenius norm (matrices)
else:
if colA < colB:
n = colA
m = colB
big = B
small = A
else:
n = colB
m = colA
big = A
small = B
matnew = np.identity(m)
matnew[0:n, 0:n] = small
bigprime = normalize(big, axis=1, norm='l2')
matnewprime = normalize(matnew, axis=1, norm='l2')
dist = np.linalg.norm(matnewprime - bigprime, 2)
print dist
示例4: normalize
def normalize(self):
"""
impute
"""
print('Normalization')
self.tr = normalize(self.tr)
self.te = normalize(self.te)
示例5: read_dataset
def read_dataset(train_size, scale=False, normalize=False):
logging.info('fetching the dataset')
#
d = sklearn.datasets.load_diabetes() # 糖尿病
#d = sklearn.datasets.load_boston() # ボストン住宅価格
#
data = d['data'].astype(np.float32)
target = d['target'].astype(np.float32).reshape(len(d['target']), 1)
#"Chainerのmnist.pyだと下記ののような書き方になっているが、ミニバッチの数が2以上だと動かない"らしい
#target = diabetes['target'].astype(np.float32)
# 本来訓練データで標準化・正規化して、そのパラメータをテストデータに適用すべき
if normalize and scale:
raise Exception('both normalize and scale can not be True')
if normalize:
data = preprocessing.normalize(data)
target = preprocessing.normalize(target)
if scale:
data = preprocessing.scale(data)
target = preprocessing.scale(target)
# 分割
x_train, x_test = np.split(data, [train_size])
y_train, y_test = np.split(target, [train_size])
assert len(x_train)==len(y_train)
assert len(x_test)==len(y_test)
return ((x_train, y_train), (x_test, y_test),
{"SHAPE_TRAIN_X":x_train.shape,
"SHAPE_TRAIN_Y":y_train.shape,
"SHAPE_TEST_X":x_test.shape,
"SHAPE_TEST_Y":y_test.shape,
})
示例6: remove_outliers
def remove_outliers(image,mask):
#taking the mask part to image to check the presence of bee
im = cv2.bitwise_and(image,image,mask=mask);
ldp_image,_,_ = ldp.ldp(im);
test_Y = ldp_image.reshape((ldp_image.shape[0] * ldp_image.shape[1], ldp_image.shape[2]));
test_rgb = im.reshape((im.shape[0] * im.shape[1], im.shape[2]));
test = np.concatenate((test_Y,test_rgb),axis=1);
mask_not = cv2.bitwise_not(mask);
ret1, mask_not = cv2.threshold (mask_not,np.mean(mask_not), 255, cv2.THRESH_BINARY);
im = cv2.bitwise_and(image,image,mask=mask_not);
ldp_image,_,_ = ldp.ldp(im);
data_ldp = ldp_image.reshape((ldp_image.shape[0] * ldp_image.shape[1], ldp_image.shape[2]));
data_rgb = im.reshape((im.shape[0] * im.shape[1], im.shape[2]));
data = np.concatenate((data_rgb,data_ldp),axis=1);
data = data[np.any(data!=0,axis=1)];
print data.shape;
data = data.astype('float64');
data = preprocessing.normalize(data,axis=0);
ss = StandardScaler();
data = ss.fit_transform(data);
clf = svm.OneClassSVM(nu=0.8, kernel="rbf", gamma=0.1)
clf.fit(data);
test = test.astype('float64');
test = preprocessing.normalize(test,axis=0);
print test.shape;
test = ss.fit_transform(test);
test = clf.predict(test);
test = test.reshape((image.shape[0] , image.shape[1]));
test[test==-1] = 0;
test[test==1] = 255;
test = test.astype('uint8');
im = cv2.bitwise_and(image,image,mask=test);
im = cv2.bitwise_and(im,im,mask=mask);
#print test[:,0],test[:,1];
return(im,test);
示例7: normaliser
def normaliser(x, option):
# normalize by the norm
if option == 'norm':
# print 'normalize by the norm or the row'
from sklearn.preprocessing import normalize
x_norma = normalize(x, norm='l2')
# normalize by the sum of the row, ( normalized matrix sum to 1 )
elif option == 'sum': # normalize sum to 1:
#print('normalize by the sum of the row')
from sklearn.preprocessing import normalize
x_norma = normalize(x, norm='l1')
# normalize each row by z-score : (x-mean)/std
elif option == 'zscore':
from scipy import stats
x_norma = stats.zscore(x, axis=1)
# set the nan to 0
x_norma[np.isnan(x_norma)] = 0
elif option == 'none':
# print ('no normalization')
x_norma = x
return x_norma
示例8: getData_NEC
def getData_NEC():
reader = JobDBCorpus()
data = reader.read_sequence_list(target='TODO')
np.seterr(all='ignore')
train, test = reader.train_test_data(test_size=0.2)
print("Reading chunks...")
chunks_train = ChunkSet(dataset=train)
chunks_test = ChunkSet(dataset=test)
print("Building features...")
idf = featNEC.IDFeatures(dataset=train, chunkset=chunks_train)
idf.build_features()
###############################################################################
print("Standarizing dataset...")
X_train, Y_train = getStandart(chunks_train, idf)
X_test, Y_test = getStandart(chunks_test, idf)
# sparse representation and normalize
X_train = sparse.csr_matrix(X_train)
X_train = normalize(X_train, copy=False)
X_test = sparse.csr_matrix(X_test)
X_test = normalize(X_test, copy=False)
return X_train, Y_train, X_test, Y_test, chunks_train
示例9: rwr
def rwr(transition,PT,r=0.7):
"""
:param transition: Get the spare Transition matrix
:param PT: Intialization Vector
:param r: restart probability
:return: Numpy Matrix of predicted scores
"""
#Stop criteria
stop = 1e-07
PO = PT
Tr = transition
while True:
PX = (1-r)* Tr.T * PT + (r * PO)
delta = spnorm(PX) - spnorm(PT)
if delta < stop :
break
PT = PX
#fMat = normalize(PT, norm='l1', axis=0)
OM = PT[0:5080]
OM = normalize(OM, norm='l1', axis=0)
PP = PT[5080:15078]
PP = normalize(PP, norm='l1', axis=0)
CP = PT[15078:16904]
CP = normalize(CP, norm='l1', axis=0)
PAT = PT[16904:19435]
PAT = normalize(PAT, norm='l1', axis=0)
P = np.concatenate((OM,PP,CP,PAT),axis=0)
return P
示例10: SVM_vary_train
def SVM_vary_train(train_images, train_labels, test_images, test_labels, kernel_type, tune1, tune2, tune3, train_amm):
# reshape the training/testing data into a classifiable form
train_data_mat = np.reshape(train_images, (train_images.shape[0]*train_images.shape[1],train_images.shape[3]))
test_data_mat = np.reshape(test_images, (test_images.shape[0]*test_images.shape[1],test_images.shape[3]))
train_data_mat = 1.0*np.array(np.mat(train_data_mat).transpose())
test_data_mat = 1.0*np.array(np.mat(test_data_mat).transpose())
# normalize the data
train_data_mat = preprocessing.normalize(train_data_mat, norm='l2')
test_data_mat = preprocessing.normalize(test_data_mat, norm='l2')
if kernel_type is "linear":
classif_1vr = svm.SVC(kernel=kernel_type, C=tune1)
elif kernel_type is "rbf":
classif_1vr = svm.SVC(kernel=kernel_type, gamma=tune1)
elif kernel_type is "sigmoid":
classif_1vr = svm.SVC(kernel=kernel_type, gamma=tune1, coef0=tune2)
elif kernel_type is "poly":
classif_1vr = svm.SVC(kernel=kernel_type, gamma=tune1, coef0=tune2, degree=tune3)
# fit the SVM to the training set
classif_1vr.fit(train_data_mat[0:train_amm,:], train_labels[0][0:train_amm])
targets = test_labels[0]
# make prediction on the test data set
predict = classif_1vr.predict(test_data_mat)
# calculate the accuracy
acc = calc_acc(targets, predict)
return "kernel=" + str(kernel_type) + ", tune1=" + str(tune1) + ", tune2=" + str(tune2) + ", tune3=" + str(tune3) + ", train_amm=" + str(train_amm) + ", acc: " + str(acc) + "\n"
示例11: classify
def classify(data_trn,lbl_trn,data_vld,lbl_vld,data_tst,lbl_tst):
data_trn = normalize(data_trn,copy=False)
data_vld = normalize(data_vld,copy=False)
data_tst = normalize(data_tst,copy=False)
# accuracy metric
metric_obj = mean_squared_error
'''
Train our model to predict labels for the dataset #1
'''
parameters = {'svr__gamma': 1.5, 'svr__probability': False, 'svr__epsilon': 0.4, 'svr__C': 1, 'svr__kernel': 'rbf'}
cls = Pipeline([
#('feature_selection',LinearSVC()),
('svr', SVR())
])
cls.set_params(**parameters)
cls.fit(data_trn, lbl_trn)
pred_vld = cls.predict(data_vld)
pred_tst = cls.predict(data_tst)
print ("Score for vld: %.6f" % (metric_obj(lbl_vld, pred_vld),))
print ("Score for tst: %.6f" % (metric_obj(lbl_tst, pred_tst),))
return pred_vld,pred_tst
示例12: PCA_SVM
def PCA_SVM(train_images, train_labels, test_images, test_labels, kernel_type, do_PCA, comps):
# reshape the training/testing data into a classifiable form
train_data_mat = np.reshape(train_images, (train_images.shape[0]*train_images.shape[1],train_images.shape[3]))
test_data_mat = np.reshape(test_images, (test_images.shape[0]*test_images.shape[1],test_images.shape[3]))
train_data_mat = np.array(np.mat(train_data_mat).transpose())
test_data_mat = np.array(np.mat(test_data_mat).transpose())
# normalize the data
train_data_mat = preprocessing.normalize(train_data_mat, norm='l2')
test_data_mat = preprocessing.normalize(test_data_mat, norm='l2')
# do PCA if necessary
if do_PCA:
# learn the covariance
pca = PCA(n_components=comps, whiten=True)
pca.fit(train_data_mat)
# use pca to reduce dimensionality of training data
train_data_mat = pca.transform(train_data_mat)
test_data_mat = pca.transform(test_data_mat)
# fit svm to pca-reduced
classif_1vr = svm.SVC(kernel=kernel_type)
classif_1vr.fit(train_data_mat, train_labels[0])
targets = test_labels[0]
# make prediction on the test data set
predict = classif_1vr.predict(test_data_mat)
# calculate the accuracy
acc = calc_acc(targets, predict)
return "PCA=" + str(do_PCA) + ", num_comps= " + str(comps) + ", kernel=" + str(kernel_type) + ", acc: " + str(acc) + "\n"
示例13: get_vector_sp
def get_vector_sp(model, x_data, x_test):
tmp_x = x_data[:, x_data.shape[1]-16:x_data.shape[1]-1]
tmp_x = tmp_x.reshape(tmp_x.shape[0], tmp_x.shape[1])
tmp_x = tmp_x/tmp_x[:,0].reshape(tmp_x.shape[0],1)
#tmp_x[:,1:] = tmp_x[:,1:] / tmp_x[:,0:tmp_x.shape[1]-1]
tmp_x = preprocessing.normalize(tmp_x, norm='l2')
preds = model.predict_proba(tmp_x.reshape(x_data.shape[0],15,1))
test_x = x_test[:, x_test.shape[1]-16:x_test.shape[1]-1]
test_x = test_x.reshape(test_x.shape[0], test_x.shape[1])
test_x = test_x/test_x[0][0]
test_x = preprocessing.normalize(test_x, norm='l2')
pred_test = model.predict_proba(test_x.reshape(test_x.shape[0],15,1))
x_result = numpy.hstack((x_data.reshape(x_data.shape[0], x_data.shape[1]), preds))
x_result_test = numpy.hstack((x_test.reshape(x_test.shape[0], x_test.shape[1]), pred_test))
test_bdate = x_test[0][1]
tmp_list = []
tmp_vec = x_result_test[0][x_result_test.shape[1]-20:]
i = 0
for m in x_result:
i = i + 1
dist = numpy.sqrt(numpy.sum(numpy.square(m[m.shape[0]-20:]- pred_test[0])))
tmp_list.append((m[1], dist))
sort_list = sorted(tmp_list, key = lambda x:x[1], reverse =False)
return sort_list, test_bdate
示例14: getData_NEC
def getData_NEC(test=0.2, val=0.2, mode='by_sent',target='TODO'):
print("Reading data...")
train,test,val = getData(test=test, val=val, mode=mode,target=target)
print("Reading chunks...")
chunks_train = ChunkSet(dataset=train)
chunks_test = ChunkSet(dataset=test)
print("Building features...")
idf = featNEC.IDFeatures(dataset = train, chunkset = chunks_train)
idf.build_features()
###############################################################################
print("Standarizing dataset...")
X_train,Y_train = getStandart(chunks_train, idf)
X_test,Y_test = getStandart(chunks_test, idf)
# sparse representation and normalize
X_train = sparse.csr_matrix(X_train)
X_train = normalize(X_train, copy = False)
X_test = sparse.csr_matrix(X_test)
X_test = normalize(X_test, copy = False)
return X_train,Y_train,X_test,Y_test, chunks_train
示例15: rw_overlap_kernel
def rw_overlap_kernel(C1, C2):
#l = 1.0/np.exp(1.0)
l = 0.5
k = 0
c = 0
# reshape rows into kernel matrices
M1 = np.reshape(C1, (90, 90))
M2 = np.reshape(C2, (90, 90))
# normalise so rows sum to 1
M1_norm = normalize(M1, axis=1, norm='l1')
M2_norm = normalize(M2, axis=1, norm='l1')
for i in range(1, 101) :
M1_exp = np.linalg.matrix_power(M1_norm, i)
M2_exp = np.linalg.matrix_power(M2_norm, i)
#overlap = np.sum(np.minimum(M1_exp, M2_exp))
overlap = np.sum(np.sqrt(np.multiply(M1_exp, M2_exp)))
#k = k + ((np.exp(-i) ) * overlap)
#c = c + ((np.exp(-i)) * 90)
k = k + ((l ** i) * overlap)
c = c + ((l ** i) * 90)
return k/c