文章目录

  • 1. 鸢尾花分类(1)
  • 2. 鸢尾花分类_2


废话少说速度上号刷题卷起来




1. 鸢尾花分类(1)

描述
请编写代码实现train_and_predict功能,实现能够根据四个特征对三种类型的鸢尾花进行分类。
train_and_predict函数接收三个参数:
train_input_features—二维NumPy数组,其中每个元素都是一个数组,它包含:萼片长度、萼片宽度、花瓣长度和花瓣宽度。
train_outputs—一维NumPy数组,其中每个元素都是一个数字,表示在train_input_features的同一行中描述的鸢尾花种类。0表示鸢尾setosa,1表示versicolor,2代表Iris virginica。
prediction_features—二维NumPy数组,其中每个元素都是一个数组,包含:萼片长度、萼片宽度、花瓣长度和花瓣宽度。
该函数使用train_input_features作为输入数据,使用train_outputs作为预期结果来训练分类器。请使用训练过的分类器来预测prediction_features的标签,并将它们作为可迭代对象返回(如list或numpy.ndarray)。结果中的第n个位置是prediction_features参数的第n行。

实现代码:

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB

def train_and_predict(train_input_features, train_outputs, prediction_features):
    """
    :param train_input_features: (numpy.array) A two-dimensional NumPy array where each element
                        is an array that contains: sepal length, sepal width, petal length, and petal width   
    :param train_outputs: (numpy.array) A one-dimensional NumPy array where each element
                        is a number representing the species of iris which is described in
                        the same row of train_input_features. 0 represents Iris setosa,
                        1 represents Iris versicolor, and 2 represents Iris virginica.
    :param prediction_features: (numpy.array) A two-dimensional NumPy array where each element
                        is an array that contains: sepal length, sepal width, petal length, and petal width
    :returns: (list) The function should return an iterable (like list or numpy.ndarray) of the predicted 
                        iris species, one for each item in prediction_features
    """   
    clf = GaussianNB()
    clf.fit(train_input_features, train_outputs)
    y_pred = clf.predict(prediction_features)
    return y_pred

iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,
                                                    test_size=0.3, random_state=0)



y_pred = train_and_predict(X_train, y_train, X_test)


if y_pred is not None:
    print(metrics.accuracy_score(y_test, y_pred))




2. 鸢尾花分类_2

描述
机器学习库 sklearn 自带鸢尾花分类数据集,分为四个特征和三个类别,其中这三个类别在数据集中分别表示为 0, 1 和 2,请实现 transform_three2two_cate 函数的功能,该函数是一个无参函数,要求将数据集中 label 为 2 的数据进行移除,也就是说仅保留 label 为 0 和为 1 的情况,并且对 label 为 0 和 1 的特征数据进行保留,返回值为 numpy.ndarray 格式的训练特征数据和 label 数据,分别为命名为 new_feat 和 new_label。
然后在此基础上,实现 train_and_evaluate 功能,并使用生成的 new_feat 和 new_label 数据集进行二分类训练,限定机器学习分类器只能从逻辑回归和决策树中进行选择,将训练数据和测试数据按照 8:2 的比例进行分割。

要求输出测试集上的 accuracy_score,同时要求 accuracy_score 要不小于 0.95。

实现代码:

def transform_three2two_cate():
    data = datasets.load_iris()
    index_arr = np.where(data.target == 2)[0]
    new_feat = np.delete(data.data, index_arr, 0)
    new_label = np.delete(data.target, index_arr)


    #code end here
    return new_feat,new_label

def train_and_evaluate():
    data_X,data_Y = transform_three2two_cate()
    train_x,test_x,train_y,test_y = train_test_split(data_X,data_Y,test_size = 0.2)
    #已经划分好训练集和测试集,接下来请实现对数据的训练
    #code start here
    lr_model = LogisticRegression().fit(train_x, train_y)
    y_predict = lr_model.predict(test_x)


    #code end here
    #注意模型预测的label需要定义为 y_predict,格式为list或numpy.ndarray
    print(accuracy_score(y_predict,test_y))