# 算法
# 分类算法
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 MultinomialNB
3.逻辑回归【二分类】
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# 随机分组
KNN、K-means、线性回归【SGD、岭回归、套索回归】、逻辑回归、
朴素贝叶斯、线性回归【正规方程】、决策树、随机森林、xgboost
规范的流程:先拆分、后标准化
原因:在训练集上寻找对应的标准化参数【标准差标准化寻找 均值、标准差; 离差标准化 寻找 最小值 最大值;小数定标标准化 寻找 绝对值最大的数】;之后在训练集和测试集进行转换
# 特征和标签处理
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.categories
5. 将文本转换为特征
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 # 混淆矩阵
from sklearn.metrics import silhouette_score # 轮廓系数
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计算