開始使用免費開始

隨機模型

在這個練習中,你會重建累積增益曲線的基準線,也就是隨機模型的累積增益曲線。

為此,你需要建立隨機預測。plot_cumulative_gain 方法要求對每個預測提供兩個數值:一個代表目標為 0,另一個代表目標為 1。這兩個數值必須相加為 1,所以一個有效的預測清單例如是 [(0.02,0.98),(0.27,0.73),...,(0.09,0.91)]

在 Python 中,你可以如下產生介於 ab 之間的隨機值:

import random
random_value = random.uniform(a,b)

本練習屬於課程

Python 預測分析入門

檢視課程

練習說明

  • 匯入 randommatplotlibscikitplot 模組。
  • 建立一個包含介於 0 到 1 之間隨機數的清單 random_predictions
  • 調整 random_predictions,使其包含元組 (r,a),其中 r 為清單的原始值,a 滿足 $r+a=1$。
  • 目標的真實值在 targets_test 中。顯示你這個隨機模型的累積增益圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the modules
import ____
import ____ as plt
import ____ as skplt

# Generate random predictions
random_predictions = [random.uniform(____,____) for _ in range(len(targets_test))]

# Adjust random predictions
random_predictions = [(r, ____ - ____) for r in random_predictions]

# Plot the cumulative gains graph
skplt.metrics.plot_cumulative_gain(targets_test, ____)
plt.show()
編輯並執行程式碼