天天看點

python部分機器學習處理代碼1

1. 将所有輸入資料規範化為0和1

$$

{Z_i} = \frac{{{x_i} - \min (x)}}{{\max (x) - min(x)}}

$$

def _normalize_column_0_1(X, train=True, specified_column = None, X_min = None, X_max=None):
    if train:
        if specified_column == None:
            specified_column = np.arange(X.shape[1])
        length = len(specified_column)
        X_max = np.reshape(np.max(X[:, specified_column], 0),(1,length))
        X_min = np.reshape(np.min(X[:, specified_column], 0), (1, length))
        print("X_max is" + str(X_max))
        print("X_min is" + str(X_min))
        print("specified_column is" + str(specified_column))
    X = np.divide(np.subtract(X[:, specified_column], X_min), np.subtract(X_max ,X_min))
    return X, X_max, X_min
           

測試代碼

col = np.array([[15,2,3,4,15,7,10,12,25,26,27,28],
                [6,1,13,42,5,73,10,3,15,6,7,8],
                [80,2,33,41,15,27,20,13,25,51,47,81],
                [6,2,33,41,15,27,20,13,25,51,47,81]])
X, X_max, X_min = _normalize_column_0_1(X=col)
print(X)
           

2.将指定列規範化為正态分布

def _normalize_column_normal(X, train=True, specified_column = None, X_mean=None, X_std=None):
    # The output of the function will make the specified column number to 
    # become a Normal distribution
    # When processing testing data, we need to normalize by the value 
    # we used for processing training, so we must save the mean value and 
    # the variance of the training data
    if train:
        if specified_column == None:
            specified_column = np.arange(X.shape[1])
        length = len(specified_column)
        X_mean = np.reshape(np.mean(X[:, specified_column],0), (1, length))             # 求平均
        X_std  = np.reshape(np.std(X[:, specified_column], 0), (1, length))             # 方差
    
    X[:,specified_column] = np.divide(np.subtract(X[:,specified_column],X_mean), X_std)
     
    return X, X_mean, X_std
           

測試代碼

X_train = np.array([[15,2,3,4,15,7,10,12,25,26,27,28],
                [6,1,13,42,5,73,10,3,15,6,7,8],
                [80,2,33,41,15,27,20,13,25,51,47,81],
                [6,2,33,41,15,27,20,13,25,51,47,81]])
col = [0,1,3]
X_train, X_mean, X_std = _normalize_column_normal(X_train, specified_column=col)
print(X_train)
           

3.将資料随機清晰,更換順序

def _shuffle(X, Y):
    randomize = np.arange(len(X))
    np.random.shuffle(randomize)
    return (X[randomize], Y[randomize])
           

測試代碼

(X, Y) = _shuffle(x,y)
print(X)
print(Y)
           

4.将資料切分,安照比例進行選擇

def train_dev_split(X, y, dev_size=0.25):
    train_len = int(round(len(X)*(1-dev_size)))
    return X[0:train_len], y[0:train_len], X[train_len:None], y[train_len:None]
           

測試代碼

X_train, Y_train, X_test, Y_test = train_dev_split(x, y)