python计算均方误差_在python中寻找线性回归的均方误差(使用scikit learn)

我试图用python做一个简单的线性回归,其中x变量是单词

项目描述的计数,y值是以天为单位的融资速度。在

我有点困惑,因为测试的均方根误差(RMSE)是13.77

训练数据为13.88。首先,RMSE不应该在0和1之间吗?

第二,测试数据的RMSE不应该高于训练数据的RMSE吗?

所以我想,我做错了什么,但不确定错误在哪里。在

另外,我需要知道回归的权重系数,但不幸的是

不知道如何打印,因为它隐藏在sklearn方法中。有人能帮忙吗?在

到目前为止,我得到的是:import numpy as np

import matplotlib.pyplot as plt

import sqlite3

from sklearn.model_selection import train_test_split

from sklearn import linear_model

con = sqlite3.connect('database.db')

cur = con.cursor()

# y-variable in regression is funding speed ("DAYS_NEEDED")

cur.execute("SELECT DAYS_NEEDED FROM success")

y = cur.fetchall() # list of tuples

y = np.array([i[0] for i in y]) # list of int # y.shape = (1324476,)

# x-variable in regression is the project description length ("WORD_COUNT")

cur.execute("SELECT WORD_COUNT FROM success")

x = cur.fetchall()

x = np.array([i[0] for i in x]) # list of int # x.shape = (1324476,)

# Get the train and test data split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

# Fit a model

lm = linear_model.LinearRegression()

x_train = x_train.reshape(-1, 1) # new shape: (1059580, 1)

y_train = y_train.reshape(-1, 1) # new shape: (1059580, 1)

model = lm.fit(x_train, y_train)

x_test = x_test.reshape(-1, 1) # new shape: (264896, 1)

predictions_test = lm.predict(x_test)

predictions_train = lm.predict(x_train)

print("y_test[5]: ", y_test[5]) # 14

print("predictions[5]: ", predictions_test[5]) # [ 12.6254537]

# Calculate the root mean square error (RMSE) for test and training data

N = len(y_test)

rmse_test = np.sqrt(np.sum((np.array(y_test).flatten() - np.array(predictions_test).flatten())**2)/N)

print("RMSE TEST: ", rmse_test) # 13.770731326

N = len(y_train)

rmse_train = np.sqrt(np.sum((np.array(y_train).flatten() - np.array(predictions_train).flatten())**2)/N)

print("RMSE train: ", rmse_train) # 13.8817814595

非常感谢任何帮助!谢谢!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值