educoder 数据挖掘算法原理与实践:线性回归(房价预测)

第1关:线性回归算法思想

1 BC

第2关:动手实现线性回归

#encoding=utf8
import numpy as np

#mse
def mse_score(y_predict,y_test):
    #********* Begin *********#
    m = len(y_predict)
    a = y_test - y_predict
    mse = np.dot(a,a)/m
    #********* End *********#
    return mse

def lr(train_feature,train_label,test_feature):
    '''
    input:
        train_feature(ndarray):训练样本特征
        train_label(ndarray):训练样本标签
        test_feature(ndarray):测试样本特征
    output:
        predict(ndarray):测试样本预测标签
    '''
    #********* Begin *********#
    #将x0=1加入训练数据
    new_train = np.concatenate((np.ones(len(train_feature)).reshape(-1,1),train_feature),axis=1)
    #使用正规方程解求得参数
    train_T = new_train.T
    linal = np.linalg.inv(np.dot(train_T,new_train))
    result = np.dot(linal,train_T)
    theta = np.dot(result,train_label)
    #将x0=1加入测试数据
    new_test = np.concatenate((np.ones(len(test_feature)).reshape(-1,1),test_feature),axis=1)
    #求得测试集预测标签    
    predict = np.dot(new_test, theta)
    #********* End *********#
    return predict

第3关:衡量线性回归的性能指标

#encoding=utf8
import numpy as np

#mse
def mse_score(y_predict,y_test):
    mse = np.mean((y_predict-y_test)**2)
    return mse


#r2
def r2_score(y_predict,y_test):
    '''
    input:y_predict(ndarray):预测值
          y_test(ndarray):真实值
    output:r2(float):r2值
    '''
    #********* Begin *********#
    r2 = 1-mse_score(y_predict,y_test)/np.var(y_test)
    #********* End *********#
    return r2

你可能感兴趣的:(educoder,数据挖掘,算法,线性回归)