为什么在二维数组的Numpy切片中使用负索引?

时间:2020-10-06 22:15:33

标签: python numpy linear-regression

我试图理解一个包含以下内容的Udacity线性回归示例:

data = np.loadtxt('data.csv',delimiter=',') # This is known to be a 2-columns, many rows array
X = data[:,:-1]
y = data[:,-1]

因此,据我所知,X是一个1列数组,用于捕获data的除最后一个列之外的所有列(因此实际上仅捕获第一列)和y是一个仅捕获data的最后一列的1列数组。

我的问题是为什么不这样写代码:

X = data[:,0]
y = data[:,1]

会不会更清晰/更清洁?

1 个答案:

答案 0 :(得分:1)

(n, 1)X = data[:, :1] X = data[:, 0, None] X = data[:, 0].reshape(-1, 1) 的2D数组,因为切片可保留维数。其他措词可能是

y

(n,)是一个// ----------------------------------------- // COUNT AMOUNT UNTIL CUSTOMER REACHES NEXT TIER function amount_until_tier() { $count = count_orders(); $tier_name = tier_status(); //$amount_until = abs($count - $next_tier); if ( $count >= 50 ) { echo 'You\'ve made it! You\'re VIP. Expect random rewards.'; } else { //echo 'You need <strong>' . $amount_until . '</strong> more orders to reach <strong>' . $tier_name . '</strong>'; } $the_digits = ['5','15','30','50']; $showModal = false; if (in_array($count, $the_digits)) { if (!isset( $_COOKIE["tier_advance"] )) { $showModal= true; } else { if ($_COOKIE["tier_advance"] != "$count") { $showModal = true; } else { // echo "false because tier_advance !== count } } } else { // echo "false because not in the_digits"; } if ($showModal) { echo '<script> jQuery(window).on("load",function(){ jQuery("#tier_advance").modal("show"); }); jQuery(document).on("click", "#tier_confirm", function(){ // Set a cookie Cookies.set("tier_advance", "' . $count . '", { expires: 356 }); }); </script> <!-- BEGIN Share Modal --> <div class="modal fade" id="tier_advance" tabindex="-1" role="dialog"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header text-center"> <h5>CONGRATULATIONS!' . $count . '</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body text-center"> <p>You are now a <strong class="all-caps"> ' . $tier_name . ' </strong>tier customer!</p> <p>Expect some sweet rewards to start coming your way!</p> </div> <div class="modal-footer modal-footer-centered"> <button id="tier_confirm" type="button" class="btn btn-secondary" data-dismiss="modal">Awesome, thanks!</button> </div> </div> </div> </div> <!-- END Share Modal -->'; } } add_action('wp_head', 'amount_until_tier'); 一维数组。

这些形状对于用于实现回归的线性代数很重要。