0


不平衡数据集分类实战:成人收入数据集分类模型训练和评估

许多二分类任务并不是每个类别都有相同数量的数据,存在着数据分布不平衡的情况。

一个常用的例子是成人收入数据集,它涉及到社交关系、教育水平等个人数据,以此来预测成人的收入水平,判断其是否拥有5万美元/年的个人收入。数据集中个人收入低于5万美元的数据比高于5万美元的数据要明显多一些,存在着一定程度的分布不平衡。
针对这一数据集,可以使用很多不平衡分类的相关算法完成分类任务。

在本教程中,您将了解如何为数据分布不平衡的成人收入数据集开发分类模型并对其进行评估。

学习本教程后,您将知道:

  • 如何加载和分析数据集,并对如何进行数据预处理和模型选择有一定启发。
  • 如何使用一个稳健的测试工具系统地评估机器学习模型的效能。
  • 如何拟合最终模型并使用它预测特定情况所对应的类标签。

针对成人收入不平衡分类的具体内容如下:

教程大纲

本教程主要分为了以下五个部分:

  1. 成人收入数据集介绍
  2. 数据集分析
  3. 基础模型和性能评价
  4. 模型评价
  5. 对新输入数据进行预测

成人收入数据集介绍

在这个教程中,我们将使用一个数据分布不平衡的机器学习常用数据集,称为“成人收入”或简称“成人”数据集。
该数据集归Ronny Kohavi和Barry Becker所有,取自1994年美国人口普查局的数据,包含有教育水平等个人详细数据,用于预测个人年收入是否超过或低于50000美元。
数据集提供14个输入变量,这些变量数据的类型有标签数据、序列数据、连续数据。变量的完整列表如下:

  • 年龄。
  • 阶级。
  • 最终重量。
  • 教育程度。
  • 教育年限。
  • 婚姻状况。
  • 职业。
  • 社交。
  • 种族。
  • 性别。
  • 资本收益。
  • 资本损失。
  • 每周工作小时数。
  • 国籍。

总共有48842行数据,3620行含有缺失数据,45222行具有完整的数据,其中缺失值用

  1. ?

标记。
有'>50K'和'<=50K'两类标签数据,也就是说它是一个二分类任务。同时这些标签数据分布不平衡,'<=50K'类标签比重更大。
考虑到标签数据分布不平衡的情况并不严重,并且两个标签同等重要,本教程采用常见的分类准确度或分类误差来反映此数据集上的相关模型性能。

分析数据集

成人数据集是一个广泛使用的标准机器学习数据集,用于探索和演示许多一般性的或专门为不平衡分类设计的机器学习算法。

首先,下载数据集并将其保存在当前工作目录中,命名为“adult-all.csv”.

接下来让我们考察一下该数据集。文件的前几行如下:

  1. 39,State-gov,77516,Bachelors,13,Never-married,Adm-clerical,Not-in-family,White,Male,2174,0,40,United-States,<=50K
  2. 50,Self-emp-not-inc,83311,Bachelors,13,Married-civ-spouse,Exec-managerial,Husband,White,Male,0,0,13,United-States,<=50K
  3. 38,Private,215646,HS-grad,9,Divorced,Handlers-cleaners,Not-in-family,White,Male,0,0,40,United-States,<=50K
  4. 53,Private,234721,11th,7,Married-civ-spouse,Handlers-cleaners,Husband,Black,Male,0,0,40,United-States,<=50K
  5. 28,Private,338409,Bachelors,13,Married-civ-spouse,Prof-specialty,Wife,Black,Female,0,0,40,Cuba,<=50K
  6. ...

我们可以看到,输入变量包含有连续数据、标签数据以及序号数据,对于标签数据需要进行二进制或者独热编码。同时也需要注意到,目标变量是用字符串表示的,而对于二分类问题,需要用0/1进行标签编码,因此对于占比多的多数标签编码为0,而占比较少的少数标签则编码为1。缺失的数据用

  1. ?

表示,通常可以估算这些值,也可以直接从数据集中删除这些行。

具体的载入数据集方法可使用

  1. read_csv()

这一Pandas包的内置函数,只需要指定文件名、是否读入标题行以及缺失值的对应符号(本数据为

  1. ?

,缺失值会被处理为

  1. NaN

数据):

  1. # define the dataset location
  2. filename = 'adult-all.csv'
  3. # load the csv file as a data frame
  4. dataframe = read_csv(filename, header=None, na_values='?')

成功加载数据集后,我们需要移除缺失数据所在的行,并统计数据大小:

  1. # drop rows with missing
  2. dataframe = dataframe.dropna()
  3. # summarize the shape of the dataset
  4. print(dataframe.shape)

通过

  1. Counter

函数我们可以统计数据集分布情况:

  1. # summarize the class distribution
  2. target = dataframe.values[:,-1]
  3. counter = Counter(target)
  4. for k,v in counter.items():
  5. per = v / len(target) * 100
  6. print('Class=%s, Count=%d, Percentage=%.3f%%' % (k, v, per))

上述函数集合到一起,就实现了数据加载和相关统计工作。完整代码如下:

  1. # load and summarize the dataset
  2. from pandas import read_csv
  3. from collections import Counter
  4. # define the dataset location
  5. filename = 'adult-all.csv'
  6. # load the csv file as a data frame
  7. dataframe = read_csv(filename, header=None, na_values='?')
  8. # drop rows with missing
  9. dataframe = dataframe.dropna()
  10. # summarize the shape of the dataset
  11. print(dataframe.shape)
  12. # summarize the class distribution
  13. target = dataframe.values[:,-1]
  14. counter = Counter(target)
  15. for k,v in counter.items():
  16. per = v / len(target) * 100
  17. print('Class=%s, Count=%d, Percentage=%.3f%%' % (k, v, per))

运行结果如下:

  1. (45222, 15)
  2. Class= <=50K,Count=34014,Percentage=75.216%
  3. Class= >50K, Count=11208, Percentage=24.784%

在上述代码中,首先我们加载了数据集,并确认了行和列的数量,即45222行,15列(14个输入变量和一个目标变量)。然后分析了数据分布情况,发现数据分布是不平衡的,大约75%的数据都是(<=50K),而只有大约25%的数据是(>50K)。

通过创建直方图,我们可以更直观地看到数据分布情况。具体做法如下:
首先,调用

  1. select_dtypes

函数选取数值型数据。

  1. ...
  2. # select columns with numerical data types
  3. num_ix = df.select_dtypes(include=['int64', 'float64']).columns
  4. # select a subset of the dataframe with the chosen columns
  5. subset = df[num_ix]

然后通过

  1. matplotlib

绘图包进行显示。

  1. # create histograms of numeric input variables
  2. from pandas import read_csv
  3. from matplotlib import pyplot
  4. # define the dataset location
  5. filename = 'adult-all.csv'
  6. # load the csv file as a data frame
  7. df = read_csv(filename, header=None, na_values='?')
  8. # drop rows with missing
  9. df = df.dropna()
  10. # select columns with numerical data types
  11. num_ix = df.select_dtypes(include=['int64', 'float64']).columns
  12. # select a subset of the dataframe with the chosen columns
  13. subset = df[num_ix]
  14. # create a histogram plot of each numeric variable
  15. subset.hist()
  16. pyplot.show()

运行上述代码,将为数据集中的六个输入变量分别创建一个直方图。

我们可以看到它们有着不同的分布情况,有些是高斯分布,有些是指数分布或离散分布。同样可以看出,他们的变化范围差异较大。而为了得到较好的算法效果,我们通常需要将数据分布缩放到相同的范围,因此需要进行相应的幂变换。

基础模型和性能评价

k-fold交叉验证方法能够较好估计模型的性能。在这里我们将使用k=10的重复分层k-fold交叉验证方法来评估相关模型,这意味着每个折叠将包含约

  1. 45222/10=4522

个数据。而分层表示每一个折叠将包含相同的混合比例(即每个折叠中指标数据都具有75%-25%的分布特征)。重复表示评估过程将被多次执行,以避免偶然结果和更好地捕获所选模型的方差,本教程中,我们将重复三次。这意味着将对单个模型进行

  1. 10×3=30

次拟合和评估,并记录每次运行结果的平均值和标准差。

上述方法可以通过scikit-learn包里面的

  1. RepeatedStratifiedKFold

函数实现。
具体代码如下:

  1. # evaluate a model
  2. defevaluate_model(X, y, model):
  3. # define evaluation procedure
  4. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  5. # evaluate model
  6. scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
  7. return scores

通过

  1. evaluate_model()

函数我们实现了获取加载的数据集和定义的模型,使用重复分层k-fold交叉验证对其进行评估,然后返回一个准确度列表。

而如何生成X、Y数据呢?我们可以定义一个函数来加载数据集并对目标列进行编码,然后返回所需数据。具体代码如下:

  1. # load the dataset
  2. defload_dataset(full_path):
  3. # load the dataset as a numpy array
  4. dataframe = read_csv(full_path, header=None, na_values='?')
  5. # drop rows with missing
  6. dataframe = dataframe.dropna()
  7. # split into inputs and outputs
  8. last_ix = len(dataframe.columns) - 1
  9. X, y = dataframe.drop(last_ix, axis=1), dataframe[last_ix]
  10. # select categorical and numerical features
  11. cat_ix = X.select_dtypes(include=['object', 'bool']).columns
  12. num_ix = X.select_dtypes(include=['int64', 'float64']).columns
  13. # label encode the target variable to have the classes 0 and 1
  14. y = LabelEncoder().fit_transform(y)
  15. return X.values, y, cat_ix, num_ix

通过以上步骤,我们就可以使用这个测试工具评估数据集的相关模型了。

为了更好地评估若干模型之间的差距,我们可以通过scikit库里面的

  1. DummyClassifier

类建立一个基准模型。相关代码如下:

  1. # define the reference model
  2. model = DummyClassifier(strategy='most_frequent')
  3. # evaluate the model
  4. scores = evaluate_model(X, y, model)
  5. # summarize performance
  6. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

上述函数集合到一起,就实现了一个基准算法对于数据集的预测分类和评价。完整代码如下:

  1. # test harness and baseline model evaluation for the adult dataset
  2. from collections import Counter
  3. from numpy import mean
  4. from numpy import std
  5. from numpy import hstack
  6. from pandas import read_csv
  7. from sklearn.preprocessing import LabelEncoder
  8. from sklearn.model_selection import cross_val_score
  9. from sklearn.model_selection import RepeatedStratifiedKFold
  10. from sklearn.dummy import DummyClassifier
  11. # load the dataset
  12. defload_dataset(full_path):
  13. # load the dataset as a numpy array
  14. dataframe = read_csv(full_path, header=None, na_values='?')
  15. # drop rows with missing
  16. dataframe = dataframe.dropna()
  17. # split into inputs and outputs
  18. last_ix = len(dataframe.columns) - 1
  19. X, y = dataframe.drop(last_ix, axis=1), dataframe[last_ix]
  20. # select categorical and numerical features
  21. cat_ix = X.select_dtypes(include=['object', 'bool']).columns
  22. num_ix = X.select_dtypes(include=['int64', 'float64']).columns
  23. # label encode the target variable to have the classes 0 and 1
  24. y = LabelEncoder().fit_transform(y)
  25. return X.values, y, cat_ix, num_ix
  26. # evaluate a model
  27. defevaluate_model(X, y, model):
  28. # define evaluation procedure
  29. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  30. # evaluate model
  31. scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
  32. return scores
  33. # define the location of the dataset
  34. full_path = 'adult-all.csv'
  35. # load the dataset
  36. X, y, cat_ix, num_ix = load_dataset(full_path)
  37. # summarize the loaded dataset
  38. print(X.shape, y.shape, Counter(y))
  39. # define the reference model
  40. model = DummyClassifier(strategy='most_frequent')
  41. # evaluate the model
  42. scores = evaluate_model(X, y, model)
  43. # summarize performance
  44. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

运行结果如下:

  1. (45222, 14) (45222,) Counter({0: 34014, 1: 11208})
  2. Mean Accuracy: 0.752 (0.000)

通过上述代码,我们首先加载数据并进行预处理。然后通过

  1. DummyClassifier()

进行分类,并通过

  1. RepeatedStratifiedKFold()

进行评价。可以看到,基准算法达到了约75.2%的准确度。这一结果指出了相关模型的准确度下限;任何平均准确度高于75.2%的模型都可被视为有效模型,而低于75.2%则通常被认为是无效的。

模型评价

在上一节中,我们看到,基准算法的性能良好,但还有很大的优化空间。
在本节中,我们将使用上一节中所描述的评价方法评估作用于同一数据集的不同算法。
目的是演示如何系统地解决问题,以及某些为不平衡分类问题设计的算法。

不同的机器学习算法

在这里,我们选取一系列非线性算法来进行具体的评价,如:

  • 决策树(CART,Decision Tree)
  • 支持向量机(SVM,Support Vector Machine)
  • 袋装决策树(BAG,Bagged Decision Trees)
  • 随机森林(RF,Random Forest)
  • 爬坡机(GBM,Gradient Boosting Machine)

首先定义一个列表,依次定义每个模型并将它们添加到列表中,以便于后面将运算的结果进行列表显示。代码如下:

  1. # define models to test
  2. defget_models():
  3. models, names = list(), list()
  4. # CART
  5. models.append(DecisionTreeClassifier())
  6. names.append('CART')
  7. # SVM
  8. models.append(SVC(gamma='scale'))
  9. names.append('SVM')
  10. # Bagging
  11. models.append(BaggingClassifier(n_estimators=100))
  12. names.append('BAG')
  13. # RF
  14. models.append(RandomForestClassifier(n_estimators=100))
  15. names.append('RF')
  16. # GBM
  17. models.append(GradientBoostingClassifier(n_estimators=100))
  18. names.append('GBM')
  19. return models, names

针对每一个算法,我们将主要使用默认的模型超参数。对标签变量进行独热编码,对连续型数据变量通过

  1. MinMaxScaler

进行规范化处理。具体的,建立一个Pipeline,其中第一步使用

  1. ColumnTransformer()

函数;第二步使用

  1. OneHotEncoder()

函数;第三步使用

  1. MinMaxScaler

函数。相关代码如下:

  1. ...
  2. # define steps
  3. steps = [('c',OneHotEncoder(handle_unknown='ignore'),cat_ix), 'n',MinMaxScaler(),num_ix)]
  4. # one hot encode categorical, normalize numerical
  5. ct = ColumnTransformer(steps)
  6. # wrap the model i a pipeline
  7. pipeline = Pipeline(steps=[('t',ct),('m',models[i])])
  8. # evaluate the model and store results
  9. scores = evaluate_model(X, y, pipeline)
  10. # summarize performance
  11. print('>%s %.3f (%.3f)' % (names[i], mean(scores), std(scores)))

同时,我们可以通过作图进行直观的比较:

  1. ...
  2. # plot the results
  3. pyplot.boxplot(results, labels=names, showmeans=True)
  4. pyplot.show()

上述代码集合到一起,我们就实现了对于若干算法性能的对比。完整代码如下:

  1. # spot check machine learning algorithms on the adult imbalanced dataset
  2. from numpy import mean
  3. from numpy import std
  4. from pandas import read_csv
  5. from matplotlib import pyplot
  6. from sklearn.preprocessing import LabelEncoder
  7. from sklearn.preprocessing import OneHotEncoder
  8. from sklearn.preprocessing import MinMaxScaler
  9. from sklearn.pipeline import Pipeline
  10. from sklearn.compose import ColumnTransformer
  11. from sklearn.model_selection import cross_val_score
  12. from sklearn.model_selection import RepeatedStratifiedKFold
  13. from sklearn.tree import DecisionTreeClassifier
  14. from sklearn.svm import SVC
  15. from sklearn.ensemble import RandomForestClassifier
  16. from sklearn.ensemble import GradientBoostingClassifier
  17. from sklearn.ensemble import BaggingClassifier
  18. # load the dataset
  19. defload_dataset(full_path):
  20. # load the dataset as a numpy array
  21. dataframe = read_csv(full_path, header=None, na_values='?')
  22. # drop rows with missing
  23. dataframe = dataframe.dropna()
  24. # split into inputs and outputs
  25. last_ix = len(dataframe.columns) - 1
  26. X, y = dataframe.drop(last_ix, axis=1), dataframe[last_ix]
  27. # select categorical and numerical features
  28. cat_ix = X.select_dtypes(include=['object', 'bool']).columns
  29. num_ix = X.select_dtypes(include=['int64', 'float64']).columns
  30. # label encode the target variable to have the classes 0 and 1
  31. y = LabelEncoder().fit_transform(y)
  32. return X.values, y, cat_ix, num_ix
  33. # evaluate a model
  34. defevaluate_model(X, y, model):
  35. # define evaluation procedure
  36. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  37. # evaluate model
  38. scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
  39. return scores
  40. # define models to test
  41. defget_models():
  42. models, names = list(), list()
  43. # CART
  44. models.append(DecisionTreeClassifier())
  45. names.append('CART')
  46. # SVM
  47. models.append(SVC(gamma='scale'))
  48. names.append('SVM')
  49. # Bagging
  50. models.append(BaggingClassifier(n_estimators=100))
  51. names.append('BAG')
  52. # RF
  53. models.append(RandomForestClassifier(n_estimators=100))
  54. names.append('RF')
  55. # GBM
  56. models.append(GradientBoostingClassifier(n_estimators=100))
  57. names.append('GBM')
  58. return models, names
  59. # define the location of the dataset
  60. full_path = 'adult-all.csv'
  61. # load the dataset
  62. X, y, cat_ix, num_ix = load_dataset(full_path)
  63. # define models
  64. models, names = get_models()
  65. results = list()
  66. # evaluate each model
  67. for i in range(len(models)):
  68. # define steps
  69. steps = [('c',OneHotEncoder(handle_unknown='ignore'),cat_ix), ('n',MinMaxScaler(),num_ix)]
  70. # one hot encode categorical, normalize numerical
  71. ct = ColumnTransformer(steps)
  72. # wrap the model i a pipeline
  73. pipeline = Pipeline(steps=[('t',ct),('m',models[i])])
  74. # evaluate the model and store results
  75. scores = evaluate_model(X, y, pipeline)
  76. results.append(scores)
  77. # summarize performance
  78. print('>%s %.3f (%.3f)' % (names[i], mean(scores), std(scores)))
  79. # plot the results
  80. pyplot.boxplot(results, labels=names, showmeans=True)
  81. pyplot.show()

运行结果如下:

  1. >CART 0.812 (0.005)
  2. >SVM 0.837 (0.005)
  3. >BAG 0.852 (0.004)
  4. >RF 0.849 (0.004)
  5. >GBM 0.863 (0.004)

我们可以看到所选择的所有算法都达到了75.2%以上的分类准确度。其中GBM算法表现最好,分类准确度约为86.3%。这一结果只是略好于基准算法的结果。而图中虽然存在一些异常值(图上的圆圈),但每个算法的结果都高于75%的基线。每个算法的分布看起来也很紧凑,中位数和平均值基本持平,这表明算法在这个数据集上是相当稳定的。这突出表明,重要的不仅仅是模型性能的综合趋势,更应该考虑的是对于少数类别的分类结果准确度(这在少数民族的相关例子中尤为重要)。

对新输入数据进行预测

本节中,我们将使用

  1. GradientBoostingClassfier

分类模型用于新输入数据的预测。拟合这个模型需要定义

  1. ColumnTransformer

来对标签数据变量进行编码并缩放连续数据变量,并且在拟合模型之前在训练集上构造一个Pipeline来执行这些变换。具体代码如下:

  1. ...
  2. # define model to evaluate
  3. model = GradientBoostingClassifier(n_estimators=100)
  4. # one hot encode categorical, normalize numerical
  5. ct = ColumnTransformer([('c',OneHotEncoder(),cat_ix), ('n',MinMaxScaler(),num_ix)])
  6. # scale, then oversample, then fit model
  7. pipeline = Pipeline(steps=[('t',ct), ('m',model)])

函数定义完成后,我们就可以调用该函数进行参数拟合了:

  1. ...
  2. # fit the model
  3. pipeline.fit(X, y)

拟合阶段过后,通过

  1. predict()

函数进行预测,返回输入数据对应的标签是“<=50K”还是“>50K”:

  1. ...
  2. # define a row of data
  3. row = [...]
  4. # make prediction
  5. yhat = pipeline.predict([row])

通过

  1. GradientBoostingClassfier

分类模型进行预测的完整代码如下:

  1. # fit a model and make predictions for the on the adult dataset
  2. from pandas import read_csv
  3. from sklearn.preprocessing import LabelEncoder
  4. from sklearn.preprocessing import OneHotEncoder
  5. from sklearn.preprocessing import MinMaxScaler
  6. from sklearn.compose import ColumnTransformer
  7. from sklearn.ensemble import GradientBoostingClassifier
  8. from imblearn.pipeline import Pipeline
  9. # load the dataset
  10. defload_dataset(full_path):
  11. # load the dataset as a numpy array
  12. dataframe = read_csv(full_path, header=None, na_values='?')
  13. # drop rows with missing
  14. dataframe = dataframe.dropna()
  15. # split into inputs and outputs
  16. last_ix = len(dataframe.columns) - 1
  17. X, y = dataframe.drop(last_ix, axis=1), dataframe[last_ix]
  18. # select categorical and numerical features
  19. cat_ix = X.select_dtypes(include=['object', 'bool']).columns
  20. num_ix = X.select_dtypes(include=['int64', 'float64']).columns
  21. # label encode the target variable to have the classes 0 and 1
  22. y = LabelEncoder().fit_transform(y)
  23. return X.values, y, cat_ix, num_ix
  24. # define the location of the dataset
  25. full_path = 'adult-all.csv'
  26. # load the dataset
  27. X, y, cat_ix, num_ix = load_dataset(full_path)
  28. # define model to evaluate
  29. model = GradientBoostingClassifier(n_estimators=100)
  30. # one hot encode categorical, normalize numerical
  31. ct = ColumnTransformer([('c',OneHotEncoder(),cat_ix), ('n',MinMaxScaler(),num_ix)])
  32. # scale, then oversample, then fit model
  33. pipeline = Pipeline(steps=[('t',ct), ('m',model)])
  34. # fit the model
  35. pipeline.fit(X, y)
  36. # evaluate on some <=50K cases (known class 0)
  37. print('<=50K cases:')
  38. data = [[24, 'Private', 161198, 'Bachelors', 13, 'Never-married', 'Prof-specialty', 'Not-in-family', 'White', 'Male', 0, 0, 25, 'United-States'],
  39. [23, 'Private', 214542, 'Some-college', 10, 'Never-married', 'Farming-fishing', 'Own-child', 'White', 'Male', 0, 0, 40, 'United-States'],
  40. [38, 'Private', 309122, '10th', 6, 'Divorced', 'Machine-op-inspct', 'Not-in-family', 'White', 'Female', 0, 0, 40, 'United-States']]
  41. for row in data:
  42. # make prediction
  43. yhat = pipeline.predict([row])
  44. # get the label
  45. label = yhat[0]
  46. # summarize
  47. print('>Predicted=%d (expected 0)' % (label))
  48. # evaluate on some >50K cases (known class 1)
  49. print('>50K cases:')
  50. data = [[55, 'Local-gov', 107308, 'Masters', 14, 'Married-civ-spouse', 'Prof-specialty', 'Husband', 'White', 'Male', 0, 0, 40, 'United-States'],
  51. [53, 'Self-emp-not-inc', 145419, '1st-4th', 2, 'Married-civ-spouse', 'Exec-managerial', 'Husband', 'White', 'Male', 7688, 0, 67, 'Italy'],
  52. [44, 'Local-gov', 193425, 'Masters', 14, 'Married-civ-spouse', 'Prof-specialty', 'Wife', 'White', 'Female', 4386, 0, 40, 'United-States']]
  53. for row in data:
  54. # make prediction
  55. yhat = pipeline.predict([row])
  56. # get the label
  57. label = yhat[0]
  58. # summarize
  59. print('>Predicted=%d (expected 1)' % (label))

运行结果如下:

  1. <=50K cases:
  2. >Predicted=0 (expected 0)
  3. >Predicted=0 (expected 0)
  4. >Predicted=0 (expected 0)
  5. >50K cases:
  6. >Predicted=1 (expected 1)
  7. >Predicted=1 (expected 1)
  8. >Predicted=1 (expected 1)

运行该代码,我们首先实现了模型在训练数据集上的训练,然后针对新的输入数据进行预测。可以看到,预测值和真实值是一致的,说明模型具有很好的预测功能。

标签:

“不平衡数据集分类实战:成人收入数据集分类模型训练和评估”的评论:

还没有评论