Pythons机器学习--sklearn导包合集
2021/9/4 17:06:03
本文主要是介绍Pythons机器学习--sklearn导包合集,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# 分类算法
1.KNN
from sklearn.neighbors import KNeighborsClassifier
2.朴素贝叶斯
from sklearn.naive_bayes import BernoulliNB # 特征值是二分类
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB3.逻辑回归【二分类】
from sklearn.linear_model import LogisticRegression
4.决策树
from sklearn.tree import DecisionTreeClassifier
5.随机森林
from sklearn.ensemble import RandomForestClassifier
6.XGboost【极端的梯度提升树】
from xgboost import XGBClassifier
# 回归算法
1.KNN
from sklearn.neighbors import KNeighborsRegressor
2. 线性回归
from sklearn.linear_model import LinearRegression # 正规方程解法
from sklearn.linear_model import SGDRegressor # 随机梯度下降
from sklearn.linear_model import Ridge # 岭回归【L2,趋近于0】
from sklearn.linear_model import Lasso # 套索回归【L1, 等于0】3.决策树
from sklearn.tree import DecisionTreeRegressor
4.随机森林
from sklearn.ensemble import RandomForestRegressor
5.XGBoost【极端的梯度提升树】
from xgboost import XGBRegressor
# 聚类算法[聚类不仅仅只有一个]1. k-means
from sklearn.cluster import KMeans
# 交叉验证from sklearn.model_selection import cross_val_score # 交叉验证得分
from sklearn.model_selection import GridSearchCV # 网络搜索
from sklearn.model_selection import KFold # K折交叉验证
from sklearn.model_selection import StratifiedKFold# 等比例K折交叉验证(仅分类有等比例)
from sklearn.model_selection import ShuffleSplit # 随机分
from sklearn.model_selection import StratifiedShuffleSplit# 等比例随机分
from sklearn.model_selection import RepeatedKFold # P次K折交叉验证
from sklearn.model_selection import LeaveOneOut # 留一法
from sklearn.model_selection import LeavePOut# 留P法
from sklearn.model_selection import GroupKFold # 分组的K折交叉验证
from sklearn.model_selection import LeaveOneGroupOut# 留一组法
from sklearn.model_selection import LeavePGroupsOutt# 留P组法
from sklearn.model_selection import GroupShuffleSplit# 随机分组
# 特征和标签处理1. 数据拆分
# 分类算法有stratify参数,回归算法没有
X_train, X_test, y_train, y_test = train_test_split(
X, # 整个数据集的特征
y, # 整个数据集的标签
test_size=0.2, # 分多少数据给测试集;20%的数据集作为测试集
# train_size=0.8, # 新版本建议使用test_size,
random_state=1, # 随机拆分
stratify=y, # 分层, 拆分后和拆分前的类别比例一致
)2. 数据标准化
from sklearn.preprocessing import MaxAbsScaler # 小数标准化
from sklearn.preprocessing import MinMaxScaler # 离差标准化
from sklearn.preprocessing import StandardScaler #标准差标准化3. 特征多项式处理【x--> 0 x x**2】
from sklearn.preprocessing import PolynomialFeatures
4. 将类别类型特征 转换为数值特征 【比如转换 female male ===> 0 1】
X["部门"] = X["部门"].astype('category').cat.codes
name=df["部门"].astype('category').cat.categories5. 将文本转换为特征
from sklearn.feature_extraction.text import CountVectorizer # 词频向量化
from sklearn.feature_extraction.text import TfidfVectorizer # TF-IDF向量化6. 主成分分析 降维
from sklearn.decomposition import PCA
# 评估
1. 分类算法from sklearn.metrics import accuracy_score # 准确率
from sklearn.metrics import recall_score # 查全率【召回率】
from sklearn.metrics import precision_score # 查准率[精确率]
from sklearn.metrics import f1_score # F1-score
from sklearn.metrics import plot_precision_recall_curve # PR曲线
from sklearn.metrics import plot_roc_curve # ROC和AUC指标
from sklearn.metrics import classification_report # 分类报告
from sklearn.metrics import confusion_matrix # 混淆矩阵2.聚类算法
from sklearn.metrics import silhouette_score # 轮廓系数
3. 回归算法
from sklearn.metrics import r2_score # [R2]
from sklearn.metrics import mean_squared_error # [mse]
from sklearn.metrics import mean_absolute_error # [mae]
# RMSE,需要根据mse计算
这篇关于Pythons机器学习--sklearn导包合集的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门