预测鲍鱼的年龄

star2017 1年前 ⋅ 789 阅读

Abalone Data Set(鲍鱼数据集)

练习目的:预测鲍鱼的年龄

原始数据集包括六个属性:Gender,Length,Diameter,Height,Weight,Rings:

  1. Gender :M (male), F (female), I (infant)
  2. Length:最长外壳尺寸(毫米)
  3. Diameter:垂直于长度的直径(毫米)
  4. Height:带壳肉的高度(毫米)
  5. Weight:整只鲍鱼的重量(克)
  6. Rings:确定鲍鱼的实际年龄,有点像估计一棵树的年龄。 随着鲍鱼的生长,在鲍鱼壳中形成环,通常以每年一环的速度形成。

下载地址1

下载地址2

参考代码:来自 https://blog.csdn.net/weixin_38748717/article/details/78787853

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#机器学习的普通线性模型、岭回归模型、lasso模型
from sklearn.linear_model import LinearRegression,Ridge,Lasso

#导入机器学习相关的数据集
import sklearn.datasets as datasets

2.获取训练数据

abalone=pd.read_csv('../day09/data/abalone.txt',sep='\t',header=None)

数据为自己本地数据,格式如下:
预测鲍鱼的年龄

#前7列为特征
train = abalone.loc[:,0:7]
#第8列为标签
target = abalone[8]
#训练数据
X_train = train[:4000]
Y_train = target[:4000]
#测试数据
x_test = train[4000:]
y_true = target[4000:]

3.确定学习模型

  • 普通线性回归
line=LinearRegression()
  • 岭回归模型
ridge = Ridge(alpha=0.5)
  • lasso回归模型
lasso = Lasso(alpha=0.003)

4.使用训练数据对学习模型进行训练

  • 训练普通线性回归模型
line.fit(X_train,Y_train)
  • 训练岭回归模型
ridge.fit(X_train,Y_train)
  • 训练lasso回归模型
lasso.fit(X_train,Y_train)

5.使用测试数据,获取预测结果

  • 使用普通线性回归模型预测结果
y_pre_line = line.predict(x_test)
  • 使用岭回归模型预测结果
y_pre_ridge = line.predict(x_test)
  • 使用lasso回归模型预测结果
y_pre_lasso = line.predict(x_test)

6.绘图

  • 普通线性回归
plt.plot(np.arange(y_pre_line.size),y_pre)
plt.plot(np.arange(y_true.size),y_true)

预测鲍鱼的年龄

  • 岭回归
plt.plot(np.arange(y_pre_ridge.size),y_pre)
plt.plot(np.arange(y_true.size),y_true)

预测鲍鱼的年龄

  • lasso回归
plt.plot(np.arange(y_pre_lasso.size),y_pre)
plt.plot(np.arange(y_true.size),y_true)

预测鲍鱼的年龄

本文来自CSDN,观点不代表一起大数据-技术文章心得立场,如若转载,请注明出处:https://blog.csdn.net/weixin_38748717/article/details/78787853

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: