Problem 291412 · medium · Level 02 Linear Data Structures

Will It Rain Tomorrow?

classification · generalisation · hidden test set · majority baseline · nearest neighbour

A hill-top weather station records two readings every evening, a pressure index and a humidity index (both about 0 to 10), and the next day someone writes down whether it rained. Rainy days are the minority. Write a forecaster.

Write classify(X_train, y_train, X_test) that learns from the labelled days (X_train[i] = [pressure, humidity], y_train[i] is "rain" or "dry") and returns a list with one prediction, "rain" or "dry", for every row of X_test.

The tests call judge_forecast(classify, n, seed). It gives your function the log weather_log(n, seed) as training data and 1000 hidden days drawn from the same weather as X_test; it keeps their labels to itself and counts how many of your predictions are right. weather_log(n, seed) is available in your code, so you can explore a log with Run (for example, train on one log and measure your accuracy on another).

How this problem is scored

A forecaster passes a test when it is right on more hidden days than the forecaster that always predicts the most common label of the training log. Its quality (0 to 100) says how much of the gap between that baseline and the best possible rule it closes: the best possible rule knows exactly how the station's weather is generated and is still wrong on some days, because rainy and dry evenings can look the same. Its accuracy on the same hidden days is shown next to every test. Match the reference solution's quality (the par in the header) for the third star.

Examples

Input:  weather_log(8, 3)
Output: ([[2.2, 9.4], [6.6, 4.3], [7.9, 6.3], [6.5, 8.6], [4.1, 4.7], [3.3, 4.1], [4.4, 3.8], [2.9, 3.1]],
         ["dry", "dry", "rain", "rain", "dry", "dry", "dry", "dry"])

Input:  judge_forecast(classify, 60, 1)
Output: a summary such as {"correct": 773, "total": 1000, "hidden_counts": {"rain": 336, "dry": 664}}
        this one passes: always "dry" would be right on only 664 days

Constraints

  • 60 <= len(X_train) <= 400, len(X_test) = 1000
  • each test must finish in well under a second in your browser: comparing every hidden day with every training day is fine
  • your predictions must not depend on the clock; if you use randomness, use a random.Random with a fixed seed

Goals

  • Build a classifier from labelled examples and apply it to examples you cannot see the labels of
  • Beat the majority baseline on new data, not only on the training data
  • Compare a model with the best accuracy the data allows
Starting Python…