如何为多项逻辑回归创建x (n, m)和y (n,)形状的掩码?

  • 本文关键字:掩码 回归 创建 python numpy
  • 更新时间 :
  • 英文 :


我需要运行一个多项逻辑回归与X, y和小时,应该作为掩码值,我感兴趣的。但是它们的形状是不同的,我不能让它们适合多项式而不出现nan问题。

这些是在我创建回归掩码之前我的数组的形状和np.unique()打印:

x :
[-717.10062214 -712.98025999 -706.55113102 ...  706.78297915  719.08540065
nan]
y :[ 1.  2.  3.  4.  5. nan]
hours : [  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.
15.  16.  17.  18.  19.  20.  21.  22.  23.  24.  25.  26.  27.  28.
29.  30.  31.  32.  33.  34.  35.  36.  37.  38.  39.  40.  41.  42.
43.  44.  45.  46.  47.  48.  49.  50.  51.  52.  53.  54.  55.  56.
57.  58.  59.  60.  61.  62.  63.  64.  65.  66.  67.  68.  69.  70.
71.  72.  73.  74.  75.  76.  77.  78.  79.  80.  81.  82.  83.  84.
85.  86.  87.  88.  89.  90.  91.  93.  94.  95.  96.  98.  99. 100.
104. 105. 106. 109. 110. 119. 120. 122. 133. 160.  nan]
# Shapes
X shape: (293240, 4), Y Shape: (29324,) , h shape: (293240,)

这些是我需要的数组。由于我需要将它们放入逻辑回归中,所以我需要删除nan并使输入为二进制,以便在train_testrongplit中使用它们来为逻辑回归做准备,因此我使用了一个名为regmask的掩码:

# Making and applying masks
regmask = np.all(np.isfinite(x),1) & np.isfinite(y) & (hours == 40)
X_train, X_test, y_train, y_test = train_test_split(x[regmask], y[regmask], 
test_size=0.25, random_state=42)
ERROR:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[64], line 57
54 print(f"X shape: {x_all.shape}, Y Shape: {y.shape} , h shape: {hours.shape}")
56 ## Making and applying masks
---> 57 regmask = np.all(np.isfinite(x_all),1) & np.isfinite(y) & (hours == 40)
59 print(f"X masked shape: {x_all[regmask].shape}, Y masked Shape: {y[regmask].shape} , h masked shape: {hours[regmask].shape}")
61 print(np.sum(regmask))
ValueError: operands could not be broadcast together with shapes (293240,) (29324,) 

我得到了错误,因为形状不同。但是,我如何删除nan,并保持形状相同,以适应它们到train_testrongplit?理想情况下,一旦我解决了这个形状问题,所有其他的都会顺利进行。

时,数组将不会组合在一起。所以我发现这个问题是在准备y并修改它以匹配(293240,1)形状时出现的,这将使它与x一起工作。