gpt4 book ai didi

python - 如何在 scikit 中加载 CSV 数据并将其用于朴素贝叶斯分类

转载 作者:太空狗 更新时间:2023-10-30 00:32:59 25 4
gpt4 key购买 nike

尝试加载自定义数据以在 Scikit 中执行 NB 分类。需要帮助将示例数据加载到 Scikit 中,然后执行 NB。如何加载目标的分类值。

使用相同的数据进行训练和测试,或者使用完整的数据集进行测试。

Sl No,Member ID,Member Name,Location,DOB,Gender,Marital Status,Children,Ethnicity,Insurance Plan ID,Annual Income ($),Twitter User ID
1,70000001,Fly Dorami,New York,39786,M,Single,,Asian,2002,0,548900028
2,70000002,Bennie Ariana,Pennsylvania,6/24/1940,F,Single,,Caucasian,2002,66313,
3,70000003,Brad Farley,Pennsylvania,12001,F,Married,4,African American,2002,98444,
4,70000004,Daggoo Cece,Indiana,14032,F,Married,2,Hispanic,2001,41896,113481472.

最佳答案

以下应该可以帮助您入门,您将需要 pandas 和 numpy。您可以将 .csv 加载到数据框中,并使用它来输入到模型中。你们都需要根据您要分离的内容来定义目标(0 表示负数,1 表示正数,假设是二元分类)。

from sklearn.naive_bayes import GaussianNB
import pandas as pd
import numpy as np

# create data frame containing your data, each column can be accessed # by df['column name']
df = pd.read_csv('/your/path/yourFile.csv')

target_names = np.array(['Positives','Negatives'])

# add columns to your data frame
df['is_train'] = np.random.uniform(0, 1, len(df)) <= 0.75
df['Type'] = pd.Factor(targets, target_names)
df['Targets'] = targets

# define training and test sets
train = df[df['is_train']==True]
test = df[df['is_train']==False]

trainTargets = np.array(train['Targets']).astype(int)
testTargets = np.array(test['Targets']).astype(int)

# columns you want to model
features = df.columns[0:7]

# call Gaussian Naive Bayesian class with default parameters
gnb = GaussianNB()

# train model
y_gnb = gnb.fit(train[features], trainTargets).predict(train[features])

关于python - 如何在 scikit 中加载 CSV 数据并将其用于朴素贝叶斯分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18395952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com