We are going to use a LIBRARY called numpy
In [63]:
import numpy
In [64]:
numpy.loadtxt(fname = 'data/weather-01.csv', delimiter = ',')
Out[64]:
In [65]:
weight_kg = 55
In [66]:
print(weight_kg)
In [67]:
print('Weight in pounds: ', weight_kg * 2.2)
In [68]:
weight_kg = 57
In [69]:
weight_kg = 57.5
In [70]:
print('New weight: ', weight_kg * 2.2)
In [71]:
%whos
In [72]:
data = numpy.loadtxt(fname = 'data/weather-01.csv', delimiter = ',')
In [73]:
data
Out[73]:
In [74]:
print(type(data))
In [75]:
type(data)
Out[75]:
In [76]:
%whos
In [77]:
# Finding out the data type
print(data.dtype)
In [78]:
# Find out the shape
print(data.shape)
In [79]:
# This is 60 rows by(*) 40 columns
In [80]:
# Getting a single number out of the array
print ("First value in data:", data [0, 0]) # numbers start at 0, unlike 1 for R - so first row is 0
In [81]:
print ('A middle value: ', data[30, 20])
In [82]:
# Lets get the first 10 columns for the first 4 rows
print(data[0:4, 0:10]) # start at 0 and go up to, but dont include 4 (so 0:3, in R would be 1:4)
In [83]:
# dont have to start slicing at 0
print(data[5:10, 7:15])
In [84]:
# Dont even need to include the UPPER and LOWER bounds
smallchunk = data[:3, 36:]
print(smallchunk)
In [85]:
# Arithmetic on arrays
doublesmallchunk = smallchunk * 2.0
In [86]:
print(doublesmallchunk)
In [87]:
triplesmallchunk = smallchunk + doublesmallchunk
In [88]:
print(triplesmallchunk)
In [89]:
print (numpy.mean(data))
In [90]:
numpy.mean(data)
Out[90]:
In [91]:
print (numpy.max(data))
In [92]:
print(numpy.min(data))
In [93]:
# Get a set of data for the first station (data set is columns (time intervals) and rows (weather stations))
station_0 = data[0, :] # can put just : for all columns
In [94]:
print(numpy.max(station_0))
In [95]:
# We dont need to create 'temporary' array slices
# We can refer to what we call araay axes
In [96]:
print(numpy.mean(data, axis = 1))
In [97]:
# do some simple visualisations
In [98]:
import matplotlib.pyplot
In [100]:
%matplotlib inline
In [101]:
image = matplotlib.pyplot.imshow(data)
In [102]:
# Lets look at the average temperature over time
avg_temperature = numpy.mean(data, axis = 0)
In [103]:
avg_plot = matplotlib.pyplot.plot(avg_temperature)
In [ ]:
#Task: produce max and minimum plots
In [107]:
max_temp = numpy.max(data, axis = 0)
min_temp = numpy.min(data, axis = 0)
In [108]:
min_plot = matplotlib.pyplot.plot(min_temp)
In [109]:
max_plot = matplotlib.pyplot.plot(max_temp)
In [111]:
min_plot = matplotlib.pyplot.plot(min_temp)
max_plot = matplotlib.pyplot.plot(max_temp)
In [ ]: