8000 Merge pull request #15 from topleft/chap15 · coderwxy/book1-exercises@af9fc21 · GitHub
[go: up one dir, main page]

Skip to content

Commit af9fc21

Browse files
committed
Merge pull request realpython#15 from topleft/chap15
review exercises and rename
2 parents c3d717e + f4173a1 commit af9fc21

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Year,Temperature,Pirates
2+
1820,14.25,45000
3+
1860,14.4,35000
4+
1880,14.55,20000
5+
1920,14.9,15000
6+
1940,15.25,5000
7+
1980,15.6,400
8+
2000,15.9,17

refactor/chp15/solutions/15-1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 15.1 review exercises
2+
3+
# Setup
4+
import numpy
5+
6+
7+
# Create a 3x3 array of the number 3 through 11 using reshape()
8+
first_matrix = numpy.arange(3, 12)
9+
first_matrix = first_matrix.reshape(3, 3)
10+
11+
# Display the min, max and mean of all entries in the matrix
12+
print "Min is", first_matrix.min()
13+
print "Max is", first_matrix.max()
14+
print "Mean is", first_matrix.mean()
15+
16+
# Square every entry and save in a new matrix
17+
second_matrix = first_matrix ** 2
18+
19+
# Put first_matrix on top of second_matrix
20+
third_matrix = numpy.vstack([first_matrix, second_matrix])
21+
22+
# Calculate the dot product of third_matrix by first_matrix
23+
print(numpy.dot(third_matrix, first_matrix))
24+
25+
# Reshape third_matrix into a 3x3x2 matrix
26+
third_matrix = third_matrix.reshape(3, 3, 2)
27+
print(third_matrix)

refactor/chp15/solutions/pirates.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 15.2 pirates.py
2+
# Graph pirates versus global warming
3+
4+
from matplotlib import pyplot as plt
5+
import csv
6+
import os
7+
path = "C:/Real Python/refactor/chp15/practice_files"
8+
9+
years = []
10+
temperatures = []
11+
pirates = []
12+
13+
with open(os.path.join(path, "pirates.csv"), "rb") as my_file:
14+
my_file_reader = csv.reader(my_file)
15+
my_file_reader.next() # skip header row
16+
for year, temperature, pirate_count in my_file_reader:
17+
years.append(year)
18+
temperatures.append(temperature)
19+
pirates.append(pirate_count)
20+
21+
plt.plot(pirates, temperatures, "r-o")
22+
23+
# label graph
24+
plt.title("Global temperature as a function of pirate population")
25+
plt.xlabel("Total pirates")
26+
plt.ylabel("Average global temperature (Celsius)")
27+
plt.axis([-300, 48000, 14, 16])
28+
29+
# annotate points with years
30+
for i in range(0, len(years)):
31+
plt.annotate(str(years[i]), xy=(pirates[i], temperatures[i]))
32+
33+
# save and display graph
34+
plt.savefig(os.path.join(path, "Output/pirates.png"))
35+
plt.show()

0 commit comments

Comments
 (0)
0