返回

SAS神经网络教程:使用鸢尾花数据集预测花卉类型

开发工具

1. 导入数据

首先,我们需要将鸢尾花数据集导入到SAS中。鸢尾花数据集是一个著名的分类数据集,包含150个鸢尾花样本,分为三种不同的类型:山鸢尾、变色鸢尾和维吉尼亚鸢尾。

data iris;
  input sepal_length sepal_width petal_length petal_width species;
  datalines;
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
...
...
6.9,3.1,5.4,2.1,virginica

2. 准备数据

接下来,我们需要准备数据以供神经网络模型使用。首先,我们需要将鸢尾花物种变量转换为数字形式。

data iris_prepared;
  set iris;
  species_num = input(species, 1.);
run;

然后,我们需要将数据分为训练集和测试集。训练集用于训练神经网络模型,而测试集用于评估模型的准确性。

proc surveyselect data=iris_prepared method=srs out=iris_train p=0.75;
run;

data iris_test;
  set iris_prepared;
  if _sampstat_ = 0 then output;
run;

3. 训练神经网络模型

现在,我们可以开始训练神经网络模型了。我们将使用SAS的集成神经网络程序PROC NETWORKS。

proc networks data=iris_train;
  net iris_net;
  layer 1 type=input size=4;
  layer 2 type=hidden size=6 activation=tanh;
  layer 3 type=output size=3 activation=softmax;
  connect 1 to 2;
  connect 2 to 3;
  train epochs=1000 lr=0.1;
run;

4. 评估模型的准确性

训练好模型后,我们需要评估其准确性。我们将使用测试集来评估模型的准确性。

proc score data=iris_test model=iris_net;
  output predicted_species=predicted_species;
run;

proc freq data=iris_test;
  tables species*predicted_species;
run;

5. 使用模型预测新数据的花卉类型

最后,我们可以使用训练好的模型来预测新数据的花卉类型。

data new_iris;
  input sepal_length sepal_width petal_length petal_width;
  datalines;
5.0,3.6,1.3,0.25;
proc score data=new_iris model=iris_net;
  output predicted_species=predicted_species;
run;

proc print data=new_iris;
  var sepal_length sepal_width petal_length petal_width predicted_species;
run;