x_train = train.drop(['id','label'], axis=1) y_train = train['label'] x_test=test.drop(['id'], axis=1) def abs_sum(y_pre,y_tru): y_pre=np.array(y_pre) y_tru=np.array(y_tru) loss=sum(sum(abs(y_pre-y_tru))) return loss def cv_model(clf, train_x, train_y, test_x, clf_name): folds = 5 seed = 2021 kf = KFold(n_splits=folds, shuffle=True, random_state=seed) test = np.zeros((test_x.shape[0],4)) cv_scores = [] onehot_encoder = OneHotEncoder(sparse=False) for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)): print('************************************ {} ************************************'.format(str(i+1))) trn_x, trn_y, val_x, val_y = train_x.iloc[train_index], train_y[train_index], train_x.iloc[valid_index], train_y[valid_index] if clf_name == "lgb": train_matrix = clf.Dataset(trn_x, label=trn_y) valid_matrix = clf.Dataset(val_x, label=val_y) params = { 'boosting_type': 'gbdt', 'objective': 'multiclass', 'num_class': 4, 'num_leaves': 2 ** 5, 'feature_fraction': 0.8, 'bagging_fraction': 0.8, 'bagging_freq': 4, 'learning_rate': 0.1, 'seed': seed, 'nthread': 28, 'n_jobs':24, 'verbose': -1, } model = clf.train(params, train_set=train_matrix, valid_sets=valid_matrix, num_boost_round=2000, verbose_eval=100, early_stopping_rounds=200) val_pred = model.predict(val_x, num_iteration=model.best_iteration) test_pred = model.predict(test_x, num_iteration=model.best_iteration) val_y=np.array(val_y).reshape(-1, 1) val_y = onehot_encoder.fit_transform(val_y) print('预测的概率矩阵为:') print(test_pred) test += test_pred score=abs_sum(val_y, val_pred) cv_scores.append(score) print(cv_scores) print("%s_scotrainre_list:" % clf_name, cv_scores) print("%s_score_mean:" % clf_name, np.mean(cv_scores)) print("%s_score_std:" % clf_name, np.std(cv_scores)) test=test/kf.n_splits return test def lgb_model(x_train, y_train, x_test): lgb_test = cv_model(lgb, x_train, y_train, x_test, "lgb") return lgb_test lgb_test = lgb_model(x_train, y_train, x_test) 这段代码运用了什么学习模型
时间: 2024-04-23 21:23:52 浏览: 21
这段代码运用了LightGBM模型(lgb)进行多分类任务的学习和预测。其中,使用了K折交叉验证(KFold)来划分训练集和验证集,避免过拟合和欠拟合。在训练过程中,使用了绝对误差和(abs_sum)作为损失函数。在LightGBM模型的参数设置上,使用了gbdt算法进行梯度提升决策树,num_class参数设置为4,表示有4个类别;num_leaves参数设置为2的5次方,表示叶节点的数量;feature_fraction和bagging_fraction是特征和样本的子抽样比例;learning_rate是学习率;early_stopping_rounds设置为200,表示在验证集上连续200次迭代中没有提高时,停止训练;n_jobs和nthread是并行训练的参数。最终,返回了测试集上的预测结果(lgb_test)。
相关问题
import pandas as pd import numpy as np from sklearn.model_selection import StratifiedKFold, KFold from sklearn.svm import SVR train = pd.read_csv('train.csv') test = pd.read_csv('test.csv') # 分离数据集 X_train_c = train.drop(['ID', 'CLASS'], axis=1).values y_train_c = train['CLASS'].values X_test_c = test.drop(['ID'], axis=1).values nfold = 5 kf = KFold(n_splits=nfold, shuffle=True, random_state=2020) prediction1 = np.zeros((len(X_test_c),)) i = 0 for train_index, valid_index in kf.split(X_train_c, y_train_c): print("\nFold {}".format(i + 1)) X_train, label_train = X_train_c[train_index], y_train_c[train_index] X_valid, label_valid = X_train_c[valid_index], y_train_c[valid_index] clf = SVR(kernel='rbf', C=1, gamma='scale') clf.fit(X_train, label_train) x1 = clf.predict(X_valid) y1 = clf.predict(X_test_c) prediction1 += y1 / nfold i += 1 result1 = np.round(prediction1) id_ = range(210, 314) df = pd.DataFrame({'ID': id_, 'CLASS': result1}) df.to_csv("baseline.csv", index=False)
这段代码是一个基线模型,使用了支持向量回归(SVR)算法,通过 K 折交叉验证来训练模型,并对测试集进行预测。其中,训练数据集和测试数据集均为 CSV 格式的文件,需要使用 pandas 库读取。具体的实现过程如下:
1. 读取训练集和测试集的数据,并进行预处理。
```python
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
X_train_c = train.drop(['ID', 'CLASS'], axis=1).values
y_train_c = train['CLASS'].values
X_test_c = test.drop(['ID'], axis=1).values
```
2. 定义 K 折交叉验证器,并进行模型训练和预测。
```python
nfold = 5
kf = KFold(n_splits=nfold, shuffle=True, random_state=2020)
prediction1 = np.zeros((len(X_test_c),))
i = 0
for train_index, valid_index in kf.split(X_train_c, y_train_c):
print("\nFold {}".format(i + 1))
X_train, label_train = X_train_c[train_index], y_train_c[train_index]
X_valid, label_valid = X_train_c[valid_index], y_train_c[valid_index]
clf = SVR(kernel='rbf', C=1, gamma='scale')
clf.fit(X_train, label_train)
x1 = clf.predict(X_valid)
y1 = clf.predict(X_test_c)
prediction1 += y1 / nfold
i += 1
```
3. 对测试集的预测结果进行处理,并将结果保存到 CSV 文件中。
```python
result1 = np.round(prediction1)
id_ = range(210, 314)
df = pd.DataFrame({'ID': id_, 'CLASS': result1})
df.to_csv("baseline.csv", index=False)
```
其中,`prediction1` 是对测试集的预测结果进行累加的数组,`result1` 是将累加结果四舍五入后得到的最终预测结果。最后将结果保存到 CSV 文件中,方便后续的提交。
解决import pandas as pd import numpy as np # 读取数据 train = pd.read_csv("D:\数据挖掘\实验\实验六\clean_train.csv") test = pd.read_csv("D:\数据挖掘\实验\实验六\clean_test.csv") # 抽取正负样本 train = train[train['label'] == 1].sample(sum(train['label'] == 1)).append(train[train['label'] == 0].sample(sum(train['label'] == 0))) test = test[test['label'] == 1].sample(sum(test['label'] == 1)).append(test[test['label'] == 0].sample(sum(test['label'] == 0))) # 删除列 x_train = train.drop(['user_id', 'merchant_id', 'coupon_id','date_received', 'date'], axis=1) x_test = test.drop(['user_id', 'merchant_id', 'coupon_id','date_received', 'date', 'label'], axis=1) # 处理无穷数据(无穷数据大或者无穷数据小) x_train[np.isinf(x_train)] = 0 x_test[np.isinf(x_test)] = 0from sklearn.tree import DecisionTreeClassifier model_dt1 = DecisionTreeClassifier(max_leaf_nodes=16, random_state=123).fit(x_train.drop(['label'], axis=1), x_train['label'])报错ValueError: Input contains NaN, infinity or a value too large for dtype('float32').问题
你在处理无穷数据时使用了 np.isinf() 函数,但是你并没有处理 NaN 值。你可以使用 Pandas 库中的 fillna() 函数来处理 NaN 值。下面是一个处理 NaN 和无穷数据的代码示例:
```python
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
# 读取数据
train = pd.read_csv("D:\数据挖掘\实验\实验六\clean_train.csv")
test = pd.read_csv("D:\数据挖掘\实验\实验六\clean_test.csv")
# 抽取正负样本
train = train[train['label'] == 1].sample(sum(train['label'] == 1)).append(train[train['label'] == 0].sample(sum(train['label'] == 0)))
test = test[test['label'] == 1].sample(sum(test['label'] == 1)).append(test[test['label'] == 0].sample(sum(test['label'] == 0)))
# 删除列
x_train = train.drop(['user_id', 'merchant_id', 'coupon_id','date_received', 'date'], axis=1)
x_test = test.drop(['user_id', 'merchant_id', 'coupon_id','date_received', 'date', 'label'], axis=1)
# 处理无穷数据和 NaN
x_train = x_train.replace([np.inf, -np.inf], np.nan)
x_train.fillna(0, inplace=True)
x_test = x_test.replace([np.inf, -np.inf], np.nan)
x_test.fillna(0, inplace=True)
# 训练决策树模型
model_dt1 = DecisionTreeClassifier(max_leaf_nodes=16, random_state=123).fit(x_train.drop(['label'], axis=1), x_train['label'])
```
这样处理之后,你的数据集中就不应该包含 NaN 和无穷值了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)