【发布时间】:2017-11-05 01:01:53
【问题描述】:
我有单独的训练和测试数据集,其中包含有关大脑和体重的信息。我想做的是在从训练数据集中学习后,通过测试数据集中给定的体重来预测测试数据集中的脑重。我已经完成了linear regression,但数据没有给出可接受的结果,因为数据分布不顺畅。
我们如何使用scikit-learn 训练“训练数据集”以预测测试数据集的单列?下面的数组仅用于演示。
Training['Brain'] = [3.385, .480, 1.350, 465.00,36.330, 27.660, 14.830, 1.040, 4.190, 0.425, 0.101, 0.920, 1.000, 0.005, 0.060, 3.500 ]
Training['Body'] = [44.500, 15.5, 8.1, 423, 119.5, 115, 98.2, 5.5,58, 6.40, 4, 5.7,6.6, 140,1, 10.8]
Test['Brain'] = [192.000,3.000,160.000,0.900,1.620,0.104,4.235]
Test['Body'] = [180.000,25.000,169.000,2.600,11.400,2.500,50.400]
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
training = pd.read_csv('C:\\training.csv', index_col='Index')
test = pd.read_csv('C:\\test.csv', index_col='Index')
train_x = training['Brain']
train_y = training['Body']
slope, intercept, r_value, p_value, std_err = stats.linregress(train_x, train_y)
fig, ax = plt.subplots(figsize=(20,10))
plt.axis([-10, 600, -10, 700])
plt.plot(train_x, train_y, 'ro', color='blue')
plt.ylabel('Body')
plt.xlabel('Brain')
plt.plot(train_x, train_x*slope+intercept, 'black')
plt.plot()
plt.show()
newX = test['Body']
newY = newX * slope+intercept
print(newX)
print(newY)
print(std_err)
【问题讨论】:
-
将您的代码添加到问题中,看看您尝试了什么,并给我们一个帮助您的起点。
-
问题已编辑。 @Nic3500
标签: python scikit-learn prediction