Printing results
- Machine Learning course
- Beginner
- Lesson 40/112
- 7 min
Print the analysis results.
A readable report
If results are to be shown to people, the numbers must be formatted. Inside an f-string, :.1f shows one decimal place:
ort = veri[:, 2].mean()
print(f"Ortalama nem: %{ort:.1f}") # Ortalama nem: %48.8
print(f"{'Gün':<5}{'Sıcaklık':>10}") # aligned columns< aligns left, > right; the number sets the width.
A row-by-row table
To print each day as a row, the loop walks over the array's rows. int converts to an integer and an f-string handles the format:
for gun, sic, nem in veri:
print(f"{int(gun)}. gün: {sic:.1f} °C, nem %{nem:.0f}")In a two-dimensional array for gives one row each time; the row can be unpacked directly into variables.
Practice Write the report
Compute the temperature and humidity means from the clean data. Print the lines Ortalama sıcaklık: 23.0 and Ortalama nem: 48.8 with one decimal. Then print the hottest day as En sıcak gün: 4.
Starter code
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 yazSample solution
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)
sic, nem = veri[:, 1], veri[:, 2]
print(f"Ortalama sıcaklık: {sic.mean():.1f}")
print(f"Ortalama nem: {nem.mean():.1f}")
print(f"En sıcak gün: {int(veri[sic.argmax(), 0])}")Expected output
Ortalama sıcaklık: 23.0 Ortalama nem: 48.8 En sıcak gün: 4
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does <code>f"{48.75:.1f}"</code> produce?
- 48.75
- 48.8 Correct answer
- 48.7
- 49
Explanation: It is rounded to one decimal; 48.75 → 48.8.
2 Show the number with two decimals.
print(f"{ort____}")
print(f"{ort:.2f}")
Explanation: :.2f is fixed-point format with two decimal places.
3 Match the formats with the results (x = 3.14159).
| Term | Meaning |
|---|---|
{x:.1f} |
3.1 |
{x:.3f} |
3.142 |
{x:.0f} |
3 |
{x:6.2f} |
3.14 |