CodeBlanks Logo Icon CodeBlanks

Computing statistics

Compute a statistical summary of the data.

Column statistics

We'll compute a summary for each column of the clean data. The day column (0) is only a sequence number and its statistics are meaningless, so it is skipped with veri[:, 1:]:

olcum = veri[:, 1:]          # temperature and humidity
print(olcum.mean(axis=0))    # [23.   48.75]
print(olcum.max(axis=0))     # [26. 60.]

A per-feature summary lets you see the data's range and catch faulty values (for example 99 °C).

argmax: the hottest day

argmax gives the position of the largest value. With that position, the matching value is read from another column:

i = veri[:, 1].argmax()
print(i)              # 3  (0-based)
print(veri[i, 0])     # 4.0  -> day 4

argmin does the same for the smallest value.

Practice Compute the summary

Print the mean, standard deviation, minimum and maximum of the temperature column (veri[:, 1]) in a single print. Then find the hottest day with argmax and print it as an integer (int).

Starter code

model.py
import io
import numpy as np

csv = "gun,sicaklik,nem\n1,20,40\n2,22,\n3,,60\n4,26,50\n5,24,45\n"
veri = np.genfromtxt(io.StringIO(csv), delimiter=",", skip_header=1)
veri = np.where(np.isnan(veri), np.nanmean(veri, axis=0), veri)

# buraya yaz

Sample solution

model.py
import io
import numpy as np

csv = "gun,sicaklik,nem\n1,20,40\n2,22,\n3,,60\n4,26,50\n5,24,45\n"
veri = np.genfromtxt(io.StringIO(csv), delimiter=",", skip_header=1)
veri = np.where(np.isnan(veri), np.nanmean(veri, axis=0), veri)

s = veri[:, 1]
print(s.mean(), s.std(), s.min(), s.max())
print(int(veri[s.argmax(), 0]))

Expected output

23.0 2.0 20.0 26.0
4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How does filling with the mean affect the column's mean?

  • It decreases it
  • It makes it nan
  • It doesn't change it Correct answer
  • It increases it

Explanation: Adding a value equal to the mean keeps the mean; it does shrink the standard deviation slightly.

2 Find the position of the lowest humidity.

Question
i = veri[:, 2].____()
Answer
i = veri[:, 2].argmin()

Explanation: argmin returns the position, not the value.

3 Match the expressions.

Term Meaning
veri[:, 1:] Columns except the day
max(axis=0) Column maxima
argmax() Position of the maximum
veri[i, 0] That row's day

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation