gpt4 book ai didi

numpy - 将列表 reshape 为 (-1,1) 并返回 float 作为数据类型

转载 作者:行者123 更新时间:2023-11-30 09:52:31 24 4
gpt4 key购买 nike

我正在尝试构建逻辑回归模型,data.Exam1 是第一列

    reg = linear_model.LogisticRegression()
X = list(data.Exam1.values.reshape(-1,1)).........(1)

我已执行此操作

type(X[0]) returns numpy.ndarray

reg.fit 接受包含列表中所有 float 项目的参数,所以我这样做是因为这个异常 ValueError: Unknown label type: 'continuous'

newX = []
for item in X:
type(float(item))
newX.append(float(item))

所以当我尝试这样做时

reg.fit(newX,newY,A)

它向我抛出了这个异常

Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.

我已经在(1)中做了,当我尝试再次 reshape 它时,它再次返回ndarray,我怎样才能同时 reshape 和将项目转换为 float ? Image

最佳答案

从聊天中调整我们的解决方案

您正在尝试将录取(类型:bool)理解为考试分数(考试 1: float ,考试 2: float )的函数。你问题的关键是 sklearn.linear_model.LogisticRegression需要两个输入:

  • X :训练数据的向量/矩阵,其形状(观测值数量、预测变量数量)为 float 类型
  • Y :分类结果向量(在本例中为二进制),其形状(观察数,1),类型为 bool 或 int

您调用它的方式是尝试将 Exam2( float )作为 Exam1( float )的函数。这是根本问题。更复杂的问题是将 reshape 的 numpy 数组重新转换为列表的方式。假设datapandas.DataFrame ,你想要这样的东西:

X = np.vstack((data.Exam1, data.Exam2)).T 
print X.shape # should be (100, 2)
reg.fit(X, data.Admitted)

这里,都是data.Exam1data.Exam2是长度为 100 的向量。使用 np.vstack将它们组合成形状 (2, 100),因此我们采用转置,以便通过沿第一维度 (100, 2) 的观察使其正确定向。无需重新创建列表,甚至不需要 data.Exam1.values作为pd.Series被重铸为 np.array期间np.vstack 。同样,data.Admitted (形状为 (100,)) 与 reg.fit 配合得很好.

关于numpy - 将列表 reshape 为 (-1,1) 并返回 float 作为数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42355715/

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