> 文档中心 > 【Python刷题篇】——Python机器学习-鸢尾花分类

【Python刷题篇】——Python机器学习-鸢尾花分类

在这里插入图片描述

🤵‍♂️ 个人主页: @北极的三哈 个人主页

👨‍💻 作者简介:Python领域新星创作者。

📒 系列专栏:《牛客题库-Python篇》

🌐推荐《牛客网》——找工作神器|笔试题库|面试经验|实习经验内推求职就业一站解决

👉 点击链接进行注册学习

【Python刷题篇】——Python机器学习-鸢尾花分类


Python机器学习:AI1-AI2 鸢尾花分类

【Python刷题篇】——Python机器学习-鸢尾花分类


AI1 鸢尾花分类1

难度:中等

描述
请编写代码实现train_and_predict功能,实现能够根据四个特征对三种类型的鸢尾花进行分类。

train_and_predict函数接收三个参数:

train_input_features—二维NumPy数组,其中每个元素都是一个数组,它包含:萼片长度、萼片宽度、花瓣长度和花瓣宽度。

train_outputs—一维NumPy数组,其中每个元素都是一个数字,表示在train_input_features的同一行中描述的鸢尾花种类。0表示鸢尾setosa1表示versicolor2代表Iris virginica

prediction_features—二维NumPy数组,其中每个元素都是一个数组,包含:萼片长度、萼片宽度、花瓣长度和花瓣宽度。

该函数使用train_input_features作为输入数据,使用train_outputs作为预期结果来训练分类器。请使用训练过的分类器来预测prediction_features的标签,并将它们作为可迭代对象返回(如listnumpy.ndarray)。结果中的第n个位置是prediction_features参数的第n行。

代码:

import numpy as npfrom sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn import metricsfrom sklearn.naive_bayes import GaussianNBdef train_and_predict(train_input_features, train_outputs, prediction_features):#code start here    model = GaussianNB()    model.fit(train_input_features, train_outputs)    y = model.predict(prediction_features)    return y    #code end hereiris = 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:#code start here    print(metrics.accuracy_score(y_test, y_pred))    #code end here

AI2 鸢尾花分类2

难度:简单

描述
机器学习库 sklearn 自带鸢尾花分类数据集,分为四个特征和三个类别,其中这三个类别在数据集中分别表示为 0, 12,请实现 transform_three2two_cate 函数的功能,该函数是一个无参函数,要求将数据集中 label2 的数据进行移除,也就是说仅保留 label0 和为 1 的情况,并且对 label01 的特征数据进行保留,返回值为 numpy.ndarray 格式的训练特征数据和 label 数据,分别为命名为 new_featnew_label

然后在此基础上,实现 train_and_evaluate 功能,并使用生成的 new_featnew_label 数据集进行二分类训练,限定机器学习分类器只能从逻辑回归和决策树中进行选择,将训练数据和测试数据按照 8:2 的比例进行分割。

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

代码:

import numpy as npfrom sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import f1_score,roc_auc_score,accuracy_scorefrom sklearn.tree import DecisionTreeClassifier def transform_three2two_cate():    data = datasets.load_iris()    #其中data特征数据的key为data,标签数据的key为target    #需要取出原来的特征数据和标签数据,移除标签为2的label和特征数据,返回值new_feat为numpy.ndarray格式特征数据,new_label为对应的numpy.ndarray格式label数据    #需要注意特征和标签的顺序一致性,否则数据集将混乱    #code start here    X, y = data['data'], data['target']    #np.where方法找到y!=2的索引,根据索引得到题目要求的训练测试数据    index = np.where(y!=2)    new_feat,new_label = X[index],y[index]    #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    model = LogisticRegression()    model.fit(train_x,train_y)    y_predict = model.predict(test_x)    #code end here    #注意模型预测的label需要定义为 y_predict,格式为list或numpy.ndarray    print(accuracy_score(y_predict,test_y)) if __name__ == "__main__":    train_and_evaluate()    #要求执行train_and_evaluate()后输出为:    #1、{0,1},代表数据label为0和1    #2、测试集上的准确率分数,要求>0.95

推 荐:牛客题霸-经典高频面试题库

🌐 找工作神器-|笔试题库|面试经验|大厂面试题 👉 点击链接进行注册学习
【Python刷题篇】——Python机器学习-鸢尾花分类