资源简介
普通最小二乘法回归_源码数据
包含 数据集 Cal_housing.csv ,源码 ExistData.py ,SimulateData.py
代码片段和文件信息
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
data = pd.read_csv(“cal_housing.csv“)
name = data.columns
X = data[name[:8]] # 第1-8列
y = data[name[8:9]] # 第9列
# print(“X name :“ name[:8])
# print(“y name :“ name[8:9])
# print(data.shape X.shape y.shape) # 返回行列数
seed = 8888 # 随机种子
proportion = 0.1 # 测试集百分比
# test_size:样本占比,如果是整数的话就是样本的数量
# random_state:是随机数的种子
X_train X_test y_train y_test = train_test_split(X y test_size=proportion random_state=seed)
print(X_train.shape X_test.shape y_train.shape y_test.shape)
reg = LinearRegression() # 线性回归(Linear Regression)
res = reg.fit(X_train y_train) # 对训练集X_train y_train进行训练
y_hat = res.predict(X_test) # 使用训练得到的估计器对输入为X_test的集合进行预测得到y_hat
e = y_test-y_hat # 计算残差
SSE_cv = np.mean(e**2) # 残差平方和
SSE_test = np.mean((y_test-np.mean(y_test))**2) # 拍脑袋平方和
NMSE_cv = SSE_cv/SSE_test # 标准化均方误差 NMSE_cv
R2_cv = 1 - NMSE_cv # 可决系数R2_cv
print R2_cv
print NMSE_cv
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 988391 2018-05-10 17:18 普通最小二乘法回归源码\cal_housing.csv
文件 1336 2018-05-25 12:01 普通最小二乘法回归源码\ExistData.py
文件 614 2018-05-25 12:07 普通最小二乘法回归源码\SimulateData.py
目录 0 2018-05-25 12:08 普通最小二乘法回归源码
----------- --------- ---------- ----- ----
990341 4
- 上一篇:51单片机封装库
- 下一篇:基于DSP 的通用语音信号处理系统的设计
评论
共有 条评论