线性模型
1.线性回归
普通的线性回归使用最小二乘法进行回归计算,得出参数
代码块例子
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
dataset = pd.read_csv('E://Salary_Data.csv')
X = dataset['YearsExperience'].values
Y = dataset.iloc[:,-1].values
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.3,random_state = 0)
#X_train = X_train.reshape(-1,1)
X_train = X_train.reshape(-1,1)
Y_train = Y_train.reshape(-1,1)
regresor = LinearRegression()
regresor.fit(X_train,Y_train)
predict = regresor.predict(X_train.reshape(-1,1))
2.岭回归
岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于最小二乘法。(L2正则化)
from sklearn.linear_model import Ridge
clf = Ridge(alpha=.5)
X = [[0,0],[0,0],[1,1]]
y = [0,.1,1]
clf.fit(X,y)
print(clf.coef_)
print(clf.intercept_)
• alpha:正则化因子,对应于损失函数中的α。
3.Lasso回归(L1正则化)
import numpy as np # 快速操作结构数组的工具
import matplotlib.pyplot as plt # 可视化绘制
from sklearn.linear_model import Lasso,LassoCV,LassoLarsCV # Lasso回归,LassoCV交叉验证实现alpha的选取,LassoLarsCV基于最小角回归交叉验证实现alpha的选取
# 样本数据集,第一列为x,第二列为y,在x和y之间建立回归模型
data=[
[0.067732,3.176513],[0.427810,3.816464],[0.995731,4.550095],[0.738336,4.256571],[0.981083,4.560815],
[0.526171,3.929515],[0.378887,3.526170],[0.033859,3.156393],[0.132791,3.110301],[0.138306,3.149813],
[0.247809,3.476346],[0.648270,4.119688],[0.731209,4.282233],[0.236833,3.486582],[0.969788,4.655492],
[0.607492,3.965162],[0.358622,3.514900],[0.147846,3.125947],[0.637820,4.094115],[0.230372,3.476039],
[0.070237,3.210610],[0.067154,3.190612],[0.925577,4.631504],[0.717733,4.295890],[0.015371,3.085028],
[0.335070,3.448080],[0.040486,3.167440],[0.212575,3.364266],[0.617218,3.993482],[0.541196,3.891471]
]
#生成X和y矩阵
dataMat = np.array(data)
X = dataMat[:,0:1] # 变量x
y = dataMat[:,1] #变量y
# ========Lasso回归========
model = Lasso(alpha=0.01) # 调节alpha可以实现对拟合的程度
# model = LassoCV() # LassoCV自动调节alpha可以实现选择最佳的alpha。
# model = LassoLarsCV() # LassoLarsCV自动调节alpha可以实现选择最佳的alpha
model.fit(X, y) # 线性回归建模
print('系数矩阵:\n',model.coef_)
print('线性回归模型:\n',model)
# print('最佳的alpha:',model.alpha_) # 只有在使用LassoCV、LassoLarsCV时才有效
# 使用模型预测
predicted = model.predict(X)
原文链接:https://blog.csdn.net/luanpeng825485697/article/details/79829926
4.逻辑回归(分类模型)
(之前没有用过,后面测试过之后会加上)
非线性模型
(一)树模型
5.决策树
#建树参数
clf = tree.DecisionTreeClassifier(criterion="entropy"#基尼系数还是信息熵
,random_state=30
,splitter="random"
)
clf = clf.fit(Xtrain, Ytrain)
score = clf.score(Xtest, Ytest)
``
```python
#剪枝参数
clf = tree.DecisionTreeClassifier(criterion="entropy"
,random_state=30
,splitter="random"
,max_depth=3#限制树的最大深度,超过设定深度的树枝全部剪掉
,min_samples_leaf=10
#一个节点在分枝后的每个子节点都必须包含至少min_samples_leaf个训练样本,否则分枝就不会发生,或者,分枝会朝着满足每个子节点都包含min_samples_leaf个样本的方向去发生
,min_samples_split=10
#一个节点必须要包含至少min_samples_split个训练样本,这个节点才允许被分枝,否则分枝就不会发生。
)
clf = clf.fit(Xtrain, Ytrain)
6.随机森林
bagging集成算法的典型代表
rfc = RandomForestClassifier(n_estimators=68#建立的树的个数
,random_state=90
,criterion="gini"
,min_samples_split=8
,min_samples_leaf=1
,max_depth=12
,max_features=2
,max_leaf_nodes=36
)
其他的参数类似于决策树的参数
7.lgbm算法
LGBMRegressor(objective = 'regression_l1',#目标函数
num_leaves=int(num_leaves),#树的最大叶子节点数
max_depth=int(max_depth),#树的深度
subsample = subsample,#训练样本采样率
min_child_samples = int(min_child_samples)
)
其他的参数(有的也没有用过)
1.boosting_type=‘gbdt’# 提升树的类型 gbdt,dart,goss,rf
2.num_leavel=32#树的最大叶子数,对比xgboost一般为2^(max_depth)
3.max_depth=-1#最大树的深度
4.learning_rate#学习率
5.n_estimators=10: 拟合的树的棵树,相当于训练轮数
6.subsample=1.0: 训练样本采样率 行
7.colsample_bytree=1.0: 训练特征采样率 列
8.subsample_freq=1: 子样本频率
9.reg_alpha=0.0: L1正则化系数
10.reg_lambda=0.0: L2正则化系数
11.random_state=None: 随机种子数
12.n_jobs=-1: 并行运行多线程核心数
13.silent=True: 训练过程是否打印日志信息
14.min_split_gain=0.0: 最小分割增益
15.min_child_weight=0.001: 分支结点的最小权重
————————————————
版权声明:本文为CSDN博主「sorrythanku」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/starmoth/article/details/84586709
XGboost(后期会更新)
SVM
(1)线性svm的分类
from sklearn.svm import SVC
clf = SVC(kernel = "linear").fit(X,y)
(2)非线性svm分类(关键在于核函数)
对于线性核函数,"kernel"是
唯一能够影响它的参数,但是对于其他三种非线性核函数,他们还受到参数gamma,degree以及coef0的影响。参数gamma就是表达式中的 ,degree就是多项式核函数的次数 ,参数coef0就是常数项 。其中,高斯径向基核
函数受到gamma的影响,而多项式核函数受到全部三个参数的影响。
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
from time import time
import datetime
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3,random_state=420)
Kernel = ["linear","poly","rbf","sigmoid"]
for kernel in Kernel:
time0 = time()
clf= SVC(kernel = kernel
, gamma="auto"
, degree = 1
, cache_size=5000
).fit(Xtrain,Ytrain)
声明:部分图片及代码转载于菜菜的sklearn课件