1.5机器学习的主要挑战

1.5.1 训练数据量不足

数据的不合理有效性(理解数据集和算法的合理匹配)

1.5.2 训练数据不具代表性

采样偏差

1.5.3 低质量数据

如果你的训练数据充满错误、异常值和噪声(例如,低质量的测量产⽣的数据),系统将更难检测到底层模式,也就更不太可能表现良好。花时间清洗训练数据通常是⾮常值得的

1.5.4 无关特征

只有当训练数据包含⾜够多的相关特征并且没有太多⽆关特征时,系统才能够进⾏学习。

1.5.5 过拟合训练数据

假设你正在国外旅游,被出租⻋司机敲诈。你可能会说那个国家的所有出租⻋司机都是⼩偷。

1.5.6 欠拟合训练数据

当模型太简单⽽⽆法学习数据的底层结构时,就会发⽣⽋拟合。

练习:使用scikit-learn训练并运行一个线性模型

# 线性回归模型
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
# 取数据
lifesat = pd.read_csv('data/lifesat.csv')
print(lifesat.head())
x = lifesat[['GDP per capita (USD)']].values
y = lifesat[['Life satisfaction']].values

# 画图
lifesat.plot(kind='scatter',grid=True
             ,x='GDP per capita (USD)',y='Life satisfaction')
plt.axis([23_500,62_500,4,9])
plt.show()
#选模型
model = LinearRegression()
# 训练模型
model.fit(x,y)
# 做预测
x_new = [[37_655.2]]
prediction = model.predict(x_new)
print(prediction)

在这里插入图片描述
from sklearn.linear_model import LinearRegression
model = LinearRegression()
改为
from sklearn.neighbors import KNeighborsRegressor
model = KNeighborsRegressor(n_neighbors=3)
即为K近邻回归模型

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐