列表

详情


AI1. 鸢尾花分类_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行。

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python 3 解法, 执行用时: 1102ms, 内存消耗: 524288KB, 提交时间: 2022-06-23

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):
     
    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))

Python 3 解法, 执行用时: 1108ms, 内存消耗: 524288KB, 提交时间: 2022-06-24

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):
     
    #code start here
    clf = GaussianNB()
    clf.fit(train_input_features, train_outputs)
    y_pred = clf.predict(prediction_features)
    return y_pred
    #code end here

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:
    
    #code start here
    
    print(metrics.accuracy_score(y_test, y_pred))
    #code end here

Python 3 解法, 执行用时: 1119ms, 内存消耗: 524288KB, 提交时间: 2022-07-05

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):
     
    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))

Python 3 解法, 执行用时: 1138ms, 内存消耗: 524288KB, 提交时间: 2022-07-17

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):
     
    #code start here
    
    knn = GaussianNB()
    knn.fit(train_input_features, train_outputs)
    return knn.predict(prediction_features)

    #code end here

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:
    
    #code start here
     print(metrics.accuracy_score(y_test,y_pred))

    #code end here

Python 3 解法, 执行用时: 1140ms, 内存消耗: 524288KB, 提交时间: 2022-07-02

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):
     
    #code start here
    gnb = GaussianNB()
    gnb.fit(train_input_features, train_outputs)
    return gnb.predict(prediction_features)


    #code end here

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:
    
    #code start here
    r = 0
    for i in range(0, len(y_pred)):
        if y_pred[i] == y_test[i]:
            r = r + 1
    print(r / len(y_pred))
            


    #code end here

上一题