【问题标题】:I am new to ML I dont understand that sklearn part我是 ML 新手,我不明白 sklearn 部分
【发布时间】:2020-03-01 23:41:08
【问题描述】:

我们在第 10 行 train, test = train_test_split(data, test_size=0.2) 做什么?

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB as gnb
from sklearn.metrics import accuracy_score
data = pd.read_csv(r'C:\Users\avina\OneDrive\Desktop\New folder\program2_diabetes-20191105T144141Z-001\program2_diabetes\die.csv')
print (data.describe())

features = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'BMI', 'Age', 'Insulin', 'DiabetesPedigreeFunction']
target = 'Class'
train, test = train_test_split(data, test_size=0.2)
clf = gnb().fit(train[features], train[target]) 
y_predicted = clf.predict(test[features])
print ("Accuracy ",round(accuracy_score(test[target], y_predicted)*100,2)," %")

【问题讨论】:

  • 我们将数据分成两组,一组进行训练,另一组进行验证。测试大小为总大小的 20%
  • 你检查docs了吗?

标签: python-3.x pandas machine-learning scikit-learn


【解决方案1】:

train, test = train_test_split(data, test_size=0.2)

它将您的数据帧分成 2 个较小的数据帧,一个包含 80% 的原始数据(训练)和 20%(测试)。

您可以检查形状并断言一切都在那里

train.shape[0] + test.shape[0] == data.shape[0]

【讨论】:

    【解决方案2】:

    这背后的想法是将数据分成两组:

    • 训练集:我们用来训练模型以调整其参数值以调整数据的集。
    • 验证集:我们用来测试我们的模型调整数据性能的集,它以前没有见过。

    模型在训练集中的表现仅表明模型对这些数据的优劣,但如果我们想解决模型的泛化问题,我们必须使用新的未见数据对其进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多