描述开发项目模型的一系列情境和因素,包括问题、需求、机会、市场环境、竞争情况等
传统机器学习在解决实际问题中主要分为两类:有监督学习:已知输入、输出之间的关系而进行的学习,从而产生一个能够对已知输入给出合适输出的模型。这些算法在图像分类、语音识别、自然语言处理、推荐系统等领域有着广泛的应用 无监督学习:已知输入,无输出结果而进行的学习,发现数据中的潜在特征和规律而训练的模型。这些算法在数据挖掘、图像处理、自然语言处理等领域有着广泛的应用
传统机器学习达到的目的主要分为两类分析影响结果的主要因素 充分必要条件下预测结果 传统机器学习算法在实际开发中主要分两类 基于树的算法 非基于树的算法2 数据整体情况2.1 数据加载数据分析3剑客:numpy pandas matplotlib
# 导入相关包
import os
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_rows', None)
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
import seaborn as sns
import plotly.express as px
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
1、 pandas读取数据: pd.read_csv(),训练数据一般从csv文件加载。读取数据返回DataFrame,df.head() 查看前5条件数据分布
# 读取数据
df = pd.read_csv('./xxx.csv')
df.head()
2、查看数据总体信息
df.info()
3、 查看数据描述
# 数据总数、平均值、标准差、最大最小值,25% 50% 75% 分位值
df.describe().T
4、统计数据空值
df.isnull().sum()
5、 查看数据形状
df.shape
6、查看数据类型
df.dtypes
如果正、负样本不均衡怎么做?
hv.Distribution(np.round(df['列名'])).opts(title='标题', color='green', xlabel='x轴标签名', ylabel='y轴标签名')\
.opts(opts.Distribution(width=1000, height=600, tools=['hover'], show_grid=True))
hv.Distribution(df['BF_IRON_DUR']).opts(title='XXX时长', color='red', xlabel='时长(秒)', ylabel='Destiny')\
.opts(opts.Distribution(width=1000, height=600, tools=['hover'], show_grid=True))
plt.figure(figsize=(15,10))
for i,col in enumerate(df.columns, 1):
plt.subplot(5,3,i)
plt.title(f"Distribution of {col} Data")
sns.histplot(df[col],kde=True)
plt.tight_layout()
plt.plot()
iron_temp = df['IRON_TEMPERATURE'].iloc[:300]
temp = df['TEMPERATURE'].iloc[:300]
(hv.Curve(iron_temp, label='XXX') * hv.Curve(temp, label='XXX')).opts(title="XXXX温度对比", ylabel="IRON_TEMPERATURE", xlabel='TEMPERATURE')\
.opts(opts.Curve(width=1500, height=500,tools=['hover'], show_grid=True))
利用箱形图找出离群值并可过滤剔除
**Minimum 最小值
First quartile 1/4分位值
Median 中间值
Third quartile 3/4分位值
Maximum 最大值**
可采用以下两种方法进行补全
# 引入随机森林模型
from sklearn.ensemble import RandomForestRegressor
# 随机森林模型
rfr = RandomForestRegressor(random_state=None, n_estimators=500, n_jobs=-1)
# 利用已知输入和输出数据进行模型训练
rfr.fit(known_X, known_y)
# 输出模型得分
score = rfr.score(known_X, known_y)
print('模型得分', score)
# 获得缺失的特征数据X预测并补全
unknown_predict = rfr.predict(unKnown_X)
# 引入简单归类包
from sklearn.impute import SimpleImputer
# 对缺失的列进行平均值补全
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
# 进行模型训练
imputer = imputer.fit_transform(df[['TEMPERATURE']])
# 输出训练结果
imputer
特征衍生、选择、缩放、分布、重要性
特征转换——单特征自己进行变换,例如取绝对值、进行幂函数变换等
特征组合——多特征之间组合变换,如四则运算、交叉组合、分组统计等
corr相关性系数,删除相关性强、冗余的特征,对分析特征权重很重要
# 浅颜色代表正相关 深颜色代表负相关
plt.figure(figsize=(16, 16))
sns.heatmap(df.corr(), cmap='BrBG', annot=True, linewidths=.5)
_ = plt.xticks(rotation=45)
缩放方法
df_tree = df.apply(LabelEncoder().fit_transform)
df_tree.head()
注意:只有在特征没有冗余或被拆分的情况下,分析特征的重要性才有意义
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
clf.fit(X, y)
clf.feature_importances_
plt.rcParams['figure.figsize'] = (12, 6)
plt.style.use('fivethirtyeight')
feature = list(X.columns)
importances = clf.feature_importances_
feat_name = np.array(feature)
index = np.argsort(importances)[::-1]
plt.bar(range(len(index)), importances[index], color='lightblue')
plt.step(range(15), np.cumsum(importances[index]))
_ = plt.xticks(range(15), labels=feat_name[index], rotation='vertical', fontsize=14)
训练数据80% 测试数据20% 训练数据80% 在分80%为训练数据,20%为验证数据from sklearn.model_selection import train_test_split X = df.drop('TEMPERATURE', axis=1) y = df['TEMPERATURE'] X_train_all, X_test, y_train_all, y_test = train_test_split(X, y, test_size=0.2) X_train, X_valid, y_train, y_valid = train_test_split(X_train_all, y_train_all, test_size=0.2) print(X_train.shape, y_train.shape) print(X_test.shape, y_test.shape) print(X_valid.shape, y_valid.shape)
在这里插入图片描述
非基于树的算法LinearRegression LogisticRegression Naive Bayes SVM KNN K-Means 基于树的算法 Decission Trees Extra Trees Random Forest XGBoost GBM LightGBM
bar = sns.barplot(data=cvResDf.sort_values(by='cv_mean', ascending=False),
x='cv_mean', y='algorithm', **{'xerr': cv_std})
bar.set(xlim=(0.7, 0.9))
tesorflow
import keras
d_model = keras.models.Sequential()
d_model.add(keras.layers.Dense(units=256, activation='relu', input_shape=(X_train_scaler.shape[1:])))
d_model.add(keras.layers.Dense(units=128, activation='relu'))
d_model.add(keras.layers.Dense(units=1))
out_put_dir = './'
if not os.path.exists(out_put_dir):
os.mkdir(out_put_dir)
out_put_file = os.path.join(out_put_dir, 'model.keras')
callbacks = [
keras.callbacks.TensorBoard(out_put_dir),
keras.callbacks.ModelCheckpoint(out_put_file, save_best_only=True, save_weights_only=True),
keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)
]
d_model.compile(optimizer='Adam', loss='mean_squared_error', metrics=['mse'])
history = d_model.fit(X_train_scaler, y_train, epochs=100, validation_data=(X_valid_scaler, y_valid), callbacks=callbacks)
pytorch
import pandas as pd
import torch
from torch import nn
data = pd.read_csv('XXX.csv', header=None)
print(data.head())
X = data.iloc[:, :-1]
print(X.shape)
Y = data.iloc[:, -1]
Y.replace(-1, 0, inplace=True)
print(Y.value_counts())
X = torch.from_numpy(X.values).type(torch.FloatTensor)
Y = torch.from_numpy(Y.values.reshape(-1, 1)).type(torch.FloatTensor)
model = nn.Sequential(
nn.Linear(15, 1),
nn.Sigmoid()
)
print(model)
loss_fn = nn.BCELoss()
opt = torch.optim.SGD(model.parameters(), lr=0.0001)
batch_size = 32
steps = X.shape[0] // batch_size
for epoch in range(1000):
for batch in range(steps):
start = batch * batch_size
end = start + batch_size
x = X[start:end]
y = Y[start:end]
y_pred = model(x)
loss = loss_fn(y_pred, y)
opt.zero_grad()
loss.backward()
opt.step()
print(model.state_dict())
accuracy = ((model(X).data.numpy() > 0.5) == Y.numpy()).mean()
print('accuracy = ', accuracy)
选出相对表现优秀的模型进行优化,经过调参和工程反复应用情况,选择最优模型5.1 网络搜索
#DecisionTreeRegressor模型
GTR = DecisionTreeRegressor()
gb_param_grid = {
'criterion': ['squared_error', 'friedman_mse', 'absolute_error', 'poisson'],
'splitter': ['best', 'random'],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
modelgsGTR = GridSearchCV(GTR,param_grid = gb_param_grid, cv=kfold,
scoring="neg_mean_squared_error", n_jobs= -1, verbose = 1)
modelgsGTR.fit(X_train,y_train)
modelgsGTR.best_score_
L2使得所有w均变小
L1使得最不重要的特征维度变小,增强泛化能力,也起到降维的作用。L1在实际应用中较多。
thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
recalls = [] # 召回率
precisions = [] # 精确度
aucs = [] # 曲线下面积
y_pred_proba = grid_search.predict_proba(X_test)
for threshold in thresholds:
y_ = y_pred_proba[:,1] >= threshold
cm = confusion_matrix(y_test,y_)
# TP/(TP + FN)
recalls.append(cm[1,1]/(cm[1,0] + cm[1,1])) # 召回率
# TP/(TP + FP)
precisions.append(cm[1,1]/(cm[0,1] + cm[1,1])) # 精确率
fpr,tpr,_ = roc_curve(y_test,y_)
auc_ = auc(fpr,tpr)
aucs.append(auc_)
plt.figure(figsize=(12,6))
plt.plot(thresholds,recalls,label = 'Recall')
plt.plot(thresholds,aucs,label = 'auc')
plt.plot(thresholds,precisions,label = 'precision')
plt.legend()
plt.xlabel('thresholds')
* 线性回归(MES 均方误差)

* 逻辑回归(交叉熵)
请尊重别人的劳动成果 转载请务必注明出处
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有