This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dropout(x, level, noise_shape=None, seed=None): | |
"""Sets entries in `x` to zero at random, | |
while scaling the entire tensor. | |
# Arguments | |
x: tensor | |
level: fraction of the entries in the tensor | |
that will be set to 0. | |
noise_shape: shape for randomly generated keep/drop flags, | |
must be broadcastable to the shape of `x` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initialize the parameters of logistic regression | |
def initialize_with_zeros(dim): | |
""" | |
This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0. | |
Argument: | |
dim -- size of the w vector we want (or number of parameters in this case) | |
Returns: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
返回Logistic Regression中的预测错误的样本: | |
float(sum( labels != (preds > 0.0) ) ) / len(labels) | |
核心逻辑:对于Sigmoid函数,y = 1 / 1 + exp(-x),当x > 0 时,y = 1, 当 x < 0 时, y = 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def custom_loss(y_pre,D_label): #别人的自定义损失函数 | |
label=D_label.get_label() | |
penalty=2.0 | |
grad=-label/y_pre+penalty*(1-label)/(1-y_pre) #梯度 | |
hess=label/(y_pre**2)+penalty*(1-label)/(1-y_pre)**2 #2阶导 | |
return grad,hess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import train_test_split,GridSearchCV | |
from sklearn.preprocessing import OneHotEncoder | |
from sklearn.metrics import mean_squared_error | |
from sklearn.metrics import r2_score | |
import pandas as pd | |
import scipy as sp | |
import xgboost as xgb | |
import matplotlib.pyplot as plt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f1_error(preds,dtrain): | |
label=dtrain.get_label() | |
preds = 1.0/(1.0+np.exp(-preds)) | |
pred = [int(i >= 0.5) for i in preds] | |
tp = sum([int(i == 1 and j == 1) for i,j in zip(pred,label)]) | |
precision=float(tp)/sum(pred) | |
recall=float(tp)/sum(label) | |
return 'f1-score',2 * ( precision*recall/(precision+recall) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import numpy as np | |
import xgboost as xgb | |
### | |
# advanced: customized loss function | |
# | |
print ('start running example to used customized objective function') | |
dtrain = xgb.DMatrix('agaricus.txt.train') | |
dtest = xgb.DMatrix('agaricus.txt.test') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"mpi.h" | |
int main(int argc,char *argv[]) | |
{ | |
char message[20]=""; | |
int myrank; | |
MPI_Status status; | |
MPI_Init(&argc,&argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&myrank); | |
if(myrank==0) | |
{/*先将字符串拷贝到发送缓冲区message中,然后调用MPI_Send语句将它发出,用 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/tuple/tuple.hpp> | |
#include <boost/tuple/tuple_comparison.hpp> | |
#include <boost/tuple/tuple_io.hpp> | |
using namespace boost; | |
int main(){ | |
tuple<int, char, float> t(2, 'a', 0.9); | |
std::cout << t << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################# | |
#desc: find the number of ways to divide series into diff sets | |
#author: zhpmatrix | |
#date: 2017-05-11 | |
################################################################# | |
from itertools import combinations | |
def c(n,k): | |
return len([val for val in combinations(range(n),k)]) |