簡體   English   中英

返回無限值的Model.Matrix

[英]Model.Matrix returning infinite values

我有一個不帶行數= 1,450,683的不帶NA的數據框(由na.omit()刪除),同時將其轉換為model.matrix以饋入glmnet時,最終矩陣的某些值是Infinite。

 str(train_again)
 Classes 'tbl_df', 'tbl' and 'data.frame':  1450683 obs. of  24  variables:
$ vendor_id          : Factor w/ 2 levels "1","2": 2 1 2 2 2 2 1 2 1 2 ...
$ passenger_count    : int  1 1 1 1 1 6 4 1 1 1 ...
$ pickup_longitude   : num  -74 -74 -74 -74 -74 ...
$ pickup_latitude    : num  40.8 40.7 40.8 40.7 40.8 ...
$ dropoff_longitude  : num  -74 -74 -74 -74 -74 ...
$ dropoff_latitude   : num  40.8 40.7 40.7 40.7 40.8 ...
$ store_and_fwd_flag : Factor w/ 2 levels "N","Y": 1 1 1 1 1 1 1 1 1 1 ...
$ trip_duration      : int  455 663 2124 429 435 443 341 1551 255 1274 ...
$ month              : Factor w/ 6 levels "1","2","3","4",..: 3 6 1 4 3 1 6 5 5 5 ...
$ wday               : Factor w/ 7 levels "Fri","Mon","Sat",..: 2 4 6 7 3 3 1 3 1 6 ...
$ hour               : int  17 0 11 19 13 22 22 7 23 22 ...
$ work               : Factor w/ 2 levels "FALSE","TRUE": 2 1 2 1 1 1 1 1 1 1 ...
$ jfk_trip           : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
$ lg_trip            : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
$ average.temperature: num  45.5 72.5 22 39 46.5 33.5 70.5 60 80 56.5 ...
$ rain               : num  25 2 2 2 2 2 2 6 2 2 ...
$ s_fall             : num  2 2 2 2 2 2 2 2 2 2 ...
$ s_depth            : num  1 1 0.01 1 1 8 1 1 1 1 ...
$ total_distance     : num  2009 2513 11061 1779 1615 ...
$ number_of_steps    : int  5 6 16 4 5 5 5 17 2 6 ...
$ fastest_speed      : num  43.9 27.3 51.9 27.2 41.5 ...
$ left_turns         : int  1 2 5 2 2 1 1 4 0 2 ...
$ right_turns        : int  1 2 7 1 2 3 3 9 1 2 ...
$ turns              : int  1 2 9 1 3 3 2 6 0 3 ...


 x = model.matrix(trip_duration~.,train_again) #here train_again is a data frame with no NA's
 y = train_again$trip_duration
 sum(is.infinite(x)) #gives output as 537

可能是什么原因? 我的原始數據集是否有問題?

考慮到您的評論,您的變量之一具有Inf (不是NA )值,我建議您使用以下內容:

(使用tidyverse,因為您已經顯示出小標題。)刪除響應變量:

predvars <- dplyr::select(train_again,-trip_duration)

查找所有有限行(無NANaNInf ):

all_finite <- apply(is.finite(predvars),1,all)

您可以在model.matrix()使用單邊公式:

x <- model.matrix(~.,predvars[all_finite,])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM