python_Sklearn基础
Scikit-learn(sklearn)是机器学习中常用的第三方模块,对常用的机器学习方法进行了封装,包括回归(Regression)、降维(Dimensionality Reduction)、分类(Classfication)、聚类(Clustering)等方法。当我们面临机器学习问题时,便可根据下图来选择相应的方法。Sklearn具有以下特点:
- 简单高效的数据挖掘和数据分析工具
- 让每个人能够在复杂环境中重复使用
- 建立NumPy、Scipy、MatPlotLib之上
Sklearn安装
pip install -U scikit-learn
获取数据
- sklearn数据集
- 创建数据集
sklearn数据集
from sklearn import datasets
iris = datasets.load_iris() # 导入数据集
X = iris.data # 获得其特征向量
y = iris.target # 获得样本label
创建数据集
from sklearn.datasets.samples_generator import make_classification
X, y = make_classification(n_samples=6, n_features=5, n_informative=2, n_redundant=2, n_classes=2,
n_clusters_per_class=2,scale=1.0, random_state=20)
# n_samples: 指定样本数
# n_features:指定特征数
# n_classes: 指定几分类
# random_state:随机种子,使得随机状可重
print(X)
print(y)
[[-0.6600737 -0.0558978 0.82286793 1.1003977 -0.93493796]
[ 0.4113583 0.06249216 -0.90760075 -1.41296696 2.059838 ]
[ 1.52452016 -0.01867812 0.20900899 1.34422289 -1.61299022]
[-1.25725859 0.02347952 -0.28764782 -1.32091378 -0.88549315]
[-3.28323172 0.03899168 -0.43251277 -2.86249859 -1.10457948]
[ 1.68841011 0.06754955 -1.02805579 -0.83132182 0.93286635]]
[0 1 1 0 0 1]
for x_,y_ in zip(X,y):
print(y_,end=': ')
print(x_)
0: [-0.6600737 -0.0558978 0.82286793 1.1003977 -0.93493796]
1: [ 0.4113583 0.06249216 -0.90760075 -1.41296696 2.059838 ]
1: [ 1.52452016 -0.01867812 0.20900899 1.34422289 -1.61299022]
0: [-1.25725859 0.02347952 -0.28764782 -1.32091378 -0.88549315]
0: [-3.28323172 0.03899168 -0.43251277 -2.86249859 -1.10457948]
1: [ 1.68841011 0.06754955 -1.02805579 -0.83132182 0.93286635]
数据预处理
- 数据归一化
- 正则化(normalize)
- one-hot编码
from sklearn import preprocessing
数据归一化
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
# 1. 基于mean和std的标准化
scaler = preprocessing.StandardScaler().fit(train_data)
scaler.transform(train_data)
scaler.transform(test_data)
# 2. 将每个特征值归一化到一个固定范围
scaler = preprocessing.MinMaxScaler(feature_range=(0, 1)).fit(train_data)
scaler.transform(train_data)
scaler.transform(test_data)
#feature_range: 定义归一化范围,注用()括起来
正则化(normalize)
X = [[ 1., -1., 2.],
[ 2., 0., 0.],
[ 0., 1., -1.]]
X_normalized = preprocessing.normalize(X, norm='l2')
X_normalized
array([[ 0.40824829, -0.40824829, 0.81649658],
[ 1. , 0. , 0. ],
[ 0. , 0.70710678, -0.70710678]])
one-hot编码
data = [[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]]
encoder = preprocessing.OneHotEncoder().fit(data)
encoder.transform(data).toarray()
/Users/alpaca/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/_encoders.py:415: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.
If you want the future behaviour and silence this warning, you can specify "categories='auto'".
In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.
warnings.warn(msg, FutureWarning)
array([[1., 0., 1., 0., 0., 0., 0., 0., 1.],
[0., 1., 0., 1., 0., 1., 0., 0., 0.],
[1., 0., 0., 0., 1., 0., 1., 0., 0.],
[0., 1., 1., 0., 0., 0., 0., 1., 0.]])
数据集拆分
# 作用:将数据集划分为 训练集和测试集
from sklearn.mode_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
定义模型
- 线性回归
- 逻辑回归LR
- 朴素贝叶斯算法NB
- 决策树DT
- 支持向量机SVM
- k近邻算法KNN
- 随机森林RF
- 多层感知机(神经网络)
# 拟合模型
model.fit(X_train, y_train)
# 模型预测
model.predict(X_test)
# 获得这个模型的参数
model.get_params()
# 为模型进行打分
model.score(data_X, data_y) # 线性回归:R square; 分类问题: acc
线性回归
from sklearn.linear_model import LinearRegression
# 定义线性回归模型
model = LinearRegression(fit_intercept=True, normalize=False,
copy_X=True, n_jobs=1)
"""参数
fit_intercept:是否计算截距。False-模型没有截距
normalize: 当fit_intercept设置为False时,该参数将被忽略。 如果为真,则回归前的回归系数X将通过减去平均值并除以l2-范数而归一化。
n_jobs:指定线程数
逻辑回归LR
from sklearn.linear_model import LogisticRegression
# 定义逻辑回归模型
model = LogisticRegression(penalty=’l2’, dual=False, tol=0.0001, C=1.0,
fit_intercept=True, intercept_scaling=1, class_weight=None,
random_state=None, solver=’liblinear’, max_iter=100, multi_class=’ovr’,
verbose=0, warm_start=False, n_jobs=1)
"""参数
penalty:使用指定正则化项(默认:l2)
dual: n_samples > n_features取False(默认)
C:正则化强度的反,值越小正则化强度越大
n_jobs: 指定线程数
random_state:随机数生成器
fit_intercept: 是否需要常量
朴素贝叶斯算法NB
from sklearn import naive_bayes
model = naive_bayes.GaussianNB() # 高斯贝叶斯
model = naive_bayes.MultinomialNB(alpha=1.0, fit_prior=True, class_prior=None)
model = naive_bayes.BernoulliNB(alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None)
"""文本分类问题常用MultinomialNB
参数
alpha:平滑参数
fit_prior:是否要学习类的先验概率;false-使用统一的先验概率
class_prior: 是否指定类的先验概率;若指定则不能根据参数调整
binarize: 二值化的阈值,若为None,则假设输入由二进制向量组成
决策树DT
from sklearn import tree
model = tree.DecisionTreeClassifier(criterion=’gini’, max_depth=None,
min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0,
max_features=None, random_state=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
class_weight=None, presort=False)
"""参数
criterion :特征选择准则gini/entropy
max_depth:树的最大深度,None-尽量下分
min_samples_split:分裂内部节点,所需要的最小样本树
min_samples_leaf:叶子节点所需要的最小样本数
max_features: 寻找最优分割点时的最大特征数
max_leaf_nodes:优先增长到最大叶子节点数
支持向量机SVM
from sklearn.svm import SVC
model = SVC(C=1.0, kernel=’rbf’, gamma=’auto’)
"""参数
C:误差项的惩罚参数C
gamma: 核相关系数。浮点数,If gamma is ‘auto’ then 1/n_features will be used instead.
k近邻算法KNN
from sklearn import neighbors
#定义kNN分类模型
model = neighbors.KNeighborsClassifier(n_neighbors=5, n_jobs=1) # 分类
model = neighbors.KNeighborsRegressor(n_neighbors=5, n_jobs=1) # 回归
"""参数
n_neighbors: 使用邻居的数目
n_jobs:并行任务数
随机森林RF
from sklearn.tree import DecisionTreeClassifier
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
oob_score=False, random_state=None, verbose=0,
warm_start=False)
多层感知机(神经网络)
from sklearn.neural_network import MLPClassifier
# 定义多层感知机分类算法
model = MLPClassifier(activation='relu', solver='adam', alpha=0.0001)
"""参数
hidden_layer_sizes: 元祖
activation:激活函数
solver :优化算法{‘lbfgs’, ‘sgd’, ‘adam’}
alpha:L2惩罚(正则化项)参数
改良最优参数
- for循环
- 导入optuna函数
for循环
### from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_validate,KFold
from statistics import mean
results = []
# 最小叶子结点的参数取值
sample_leaf_options = list(range(2, 3))
# 决策树个数参数取值
n_estimators_options = list(range(165, 175))
criterion_options = ["gini","entropy"]
cv = KFold(n_splits=8, shuffle=True,random_state=0)
results = []
for leaf_size in sample_leaf_options:
for n_estimators_size in n_estimators_options:
for m in criterion_options:
clf_pro = RandomForestClassifier(min_samples_leaf=leaf_size, n_estimators=n_estimators_size, criterion=m, random_state=50)
clf_pro.fit(train_x, train_y)
pred = clf_pro.predict(test_x)
score = cross_validate(clf_pro,tatanic_x, tatanic_y, cv=cv, return_train_score=True)
results.append((leaf_size, n_estimators_size, m, mean(score['test_score'])))
print(mean(score['test_score']))
print(max(results, key=lambda x: x[3]))
导入optuna函数
安装optuna
!pip install optuna
定义objective函数
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_validate, KFold
from statistics import mean
# 导入optuna
import optuna
#格式固定,不要乱改函数名
def objective(trial):
min_samples_leaf = trial.suggest_int("min_samples_leaf", 1, 10)
n_estimators = trial.suggest_int("n_estimators", 100, 200)
criterion = trial.suggest_categorical("criterion", ["gini", "entropy"])
RFC = RandomForestClassifier(min_samples_leaf = min_samples_leaf, n_estimators = n_estimators, criterion=criterion)
RFC.fit(train_x, train_y)
cv = KFold(n_splits=8, shuffle=True,random_state=0)
score = cross_validate(clf,tatanic_x, tatanic_y, cv=cv, return_train_score=True)
return 1 - mean(score['test_score'])
利用贝叶斯找到得分最高的参数组合
study = optuna.create_study() # Create a new study.
study.optimize(objective, n_trials=10) # Invoke optimization of the objective function.
print(study.best_params)
print(1 - study.best_value)
print(study.best_trial)
模型评估与选择篇
- 交叉验证
交叉验证
# 选择随机森林作为模型
from sklearn.ensemble import RandomForestClassifier
# 导入交叉验证
from sklearn.model_selection import cross_validate, KFold
from statistics import mean
#定义模型,训练数据
clf = RandomForestClassifier(random_state=0)
clf = clf.fit(train_x, train_y)
#拆分训练与学习的数据并依次评价
cv = KFold(n_splits=10, shuffle=True,random_state=0)
score = cross_validate(clf,tatanic_x, tatanic_y, cv=cv, return_train_score=True)
print(score)
print("score",mean(score['test_score']))
import matplotlib.pyplot as plt
#用循环找出最适合的分类个数
n_range = range(5,10)
n_scores = []
for n in n_range:
cv = KFold(n_splits=n, shuffle=True,random_state=0)
score = cross_validate(clf,tatanic_x, tatanic_y, cv=cv, return_train_score=True)
n_scores.append(mean(score['test_score']))
#打印出每个所对应的交叉验证的分数
plt.plot(n_range, n_scores)
plt.xlabel('Value of n for n_splits')
plt.ylabel('Cross-Validated MSE')
plt.show()
过拟合问题
from sklearn.model_selection import learning_curve
from sklearn.datasets import load_digits
from sklearn.svm import SVC
import matplotlib.pyplot as plt
import numpy as np
#引入数据
digits=load_digits()
X=digits.data
y=digits.target
#train_size表示记录学习过程中的某一步,比如在10%,25%...的过程中记录一下
train_size,train_loss,test_loss=learning_curve(SVC(gamma=0.1),X,y,cv=10,scoring='neg_mean_squared_error',train_sizes=[0.1,0.25,0.5,0.75,1])
train_loss_mean=-np.mean(train_loss,axis=1)
test_loss_mean=-np.mean(test_loss,axis=1)
plt.figure()
#将每一步进行打印出来
plt.plot(train_size,train_loss_mean,'o-',color='r',label='Training')
plt.plot(train_size,test_loss_mean,'o-',color='g',label='Cross-validation')
plt.legend('best')
plt.show()
#将learning_curve改为validation_curve
from sklearn.model_selection import validation_curve
#改变param来观察Loss函数情况
param_range=np.logspace(-6,-2.3,5)
train_loss,test_loss=validation_curve(
SVC(),X,y,param_name='gamma',param_range=param_range,cv=10,
scoring='neg_mean_squared_error'
)
train_loss_mean=-np.mean(train_loss,axis=1)
test_loss_mean=-np.mean(test_loss,axis=1)
plt.figure()
plt.plot(param_range,train_loss_mean,'o-',color='r',label='Training')
plt.plot(param_range,test_loss_mean,'o-',color='g',label='Cross-validation')
plt.xlabel('gamma')
plt.ylabel('loss')
plt.legend(loc='best')
plt.show()
保存模型
保存为pickle文件
import pickle
# 保存模型
with open('model.pickle', 'wb') as f:
pickle.dump(model, f)
# 读取模型
with open('model.pickle', 'rb') as f:
model = pickle.load(f)
model.predict(X_test)
sklearn自带方法joblib
from sklearn.externals import joblib
# 保存模型
joblib.dump(model, 'model.pickle')
#载入模型
model = joblib.load('model.pickle')