guopengfa
发布于 2021-01-11 / 698 阅读 / 0 评论 / 0 点赞

机器学习-广义线性模型训练,入门

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

if __name__ == '__main__':

    '''
    数据为:
    origin_num,real_num
    1,1
    2,2
    3,3
    4,4
    5,5
    6,6
    7,7
    8,8
    9,9
    10,10
    11,11
    12,12
    13,13
    14,14
    15,15
    '''

    file = pd.read_csv(r'D:\Python\myself_sync\src\test\num_arry.csv')
    X = file.to_numpy() # X为一个两列多行的数据,自变量
    y = file['real_num'].to_numpy() # y为结果数据,此处为一列多行结果
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
    lr_model = LinearRegression()
    lr_model.fit(X_train, y_train)

    predict_data = lr_model.predict(X_test)
    accuracy = np.mean(predict_data == y_test)

    print(accuracy)

    # ----------保存模型到文件---------
    import pickle
    with open('line_model_1.skm','wb') as f:
        pickle.dump(lr_model,f)
        f.close()

    # 加载模型并且使用模型
    import pickle
    with open('line_model_1.skm','rb') as f:
        crf2 = pickle.load(f)
        f.close()
    predict_data = crf2.predict(X_test)



评论