gpt4 book ai didi

python-3.x - SelectKBest 以 nan 值的形式给出分数

转载 作者:行者123 更新时间:2023-11-30 08:31:10 27 4
gpt4 key购买 nike

我有一个数据集,我尝试使用 SelectKBestChi2 获取特征重要性,但 SelectKBest 给出了分数特征为nan

数据文件和代码文件位于 this链接

# Path to the data file
file_path = r"D:\Data_Sets\Mobile_Prices\data.csv"

# Reading the data from the Southern Second Order file, and also passing the column names to south_data data frame
south_data = pd.read_csv(file_path)


# Printing the number of data points and the number of columns of south_data data frame
print("The number of data points in the data :", south_data.shape[0])
print("The features of the data :", south_data.shape[1])

# Printing the head of south_data data frame
print(south_data.head())

# Check for the nulls
print(south_data.isnull().sum())

# Separate the x and y
x = south_data.drop("tss", axis = 1)
y = south_data["tss"]

# Find the scores of features
bestfit = SelectKBest(score_func=chi2, k=5)
features = bestfit.fit(x,y)
x_new = features.transform(x)

print(features.scores_)

# The output of features.scores_ is displayed as
# array([nan, nan, nan, nan, nan, nan, nan, nan, nan])

最佳答案

目标变量中的所有值都是1。这就是您的 scores_ 中的 nan 值的原因。因此,请验证您的目标变量。

仅供说明:

>>> from sklearn.datasets import load_digits
import numpy as np
>>> from sklearn.feature_selection import SelectKBest, chi2
>>> X, y = load_digits(return_X_y=True)
>>> X.shape
(1797, 64)
>>> feature_selector = SelectKBest(chi2, k=20)
>>> X_new = feature_selector.fit_transform(X, np.ones(len(X)) )
>>> feature_selector.scores_
array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan])

关于python-3.x - SelectKBest 以 nan 值的形式给出分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953958/

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