forked from geojames/Dart_EnvGIS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWeek4-1_Numpy2.py
265 lines (168 loc) · 6.08 KB
/
Week4-1_Numpy2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
__author__ = 'James T. Dietrich'
__contact__ = '[email protected]'
__copyright__ = '(c) James Dietrich 2016'
__license__ = 'MIT'
__date__ = 'Wed Nov 16 11:33:39 2016'
__version__ = '1.0'
__status__ = "initial release"
__url__ = "https://github.com/geojames/..."
"""
Name: Week4-1_Numpy2.py
Compatibility: Python 3.5
Description: This program does stuff
URL: https://github.com/geojames/...
Requires: libraries
Dev ToDo:
AUTHOR: James T. Dietrich
ORGANIZATION: Dartmouth College
Contact: [email protected]
Copyright: (c) James Dietrich 2016
"""
#------------------------------------------------------------------------------
import numpy as np
import timeit
#%% ARRAY MATH
#
# Doing math on entire arrays is much faster than using lists and FOR loops
a = np.arange(10000)
%timeit a + 1
b = list(range(10000))
%timeit [i+1 for i in b]
#%% Element wise operations
import numpy as np
array = np.array([[1.,2.,3.,4.],
[5.,6.,7.,8.]])
# math operations are "element-wise", any math operation will be applied to
# each element of the array
plus_1 = array + 1
array_squ = array ** 2
array_func = 10 ** (array/2)
#%% Array to Array Math
# for equal sized arrays, the math is done on matching elements
a = np.array([[1.,2.,3.,4.],
[5.,6.,7.,8.]])
b = a * 10
c = a + b
# Can also work on array with different dimensions (as long as one dimension is
# is same, same number of row or columns) - sometimes called broadcasting
# The smaller array will be "broadcast" to the other rows/columns
a = np.array([[1.,2.,3.,4.],
[5.,6.,7.,8.]])
b = np.array([10,11,12,13])
c = np.array([[1.],
[20.]])
col_wise = a + b
row_wise = a + c
# combining operations
array = a + c**2
#%% ARRAY CONCATENATION
# Combining arrays is easy, but beware of the direction and size of your inputs
# the number of elements that you sticking on must be the same dimensions
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
# to concat B onto A (rows)
np.concatenate((a, b), axis=0)
# to add B as a column you need to transpose it
np.concatenate((a, b.T), axis=1)
# vstack and hstack
v_stack = np.vstack((a,b))
h_stack = np.hstack((a,b.T))
np.row_stack((a,b))
np.column_stack((a,b.T))
#%% ARRAY REDUCTION
# most array reductions are methods availible to an existing array
# other reductions are numpy functions
# SUMS
a = np.array([[1.,2.],
[3.,4.]])
# sum of all elements
a_sum = a.sum()
# sums of rows (axis=0) or columns (axis=1)
# output is an array
# ax1
# a 1 2
# x 3 4
# 0
a_sum_rows = a.sum(axis=0)
a_sum_cols = a.sum(axis=1)
# MIN / MAX
# can also do by axis (like sums)
a_max = a.max() # a_row_max = a.max(axis=0)
a_min = a.min()
# BASIC STATS
# !! for data with nan values use the nan stats functions:
# np.nanmean(array) or np.nanmean(a, axis=)...
a_xbar = a.mean() # a_col_mean = a.mean(axis=1)
a_median = np.median(a) # a_col_median = np.median(a, axis=1)
a_std = a.std() # a_row_std = a.mean(axis=0)
#%% UNIQUE VALUES
years = np.random.randint(1950,2016,100)
unique_list = np.unique(years)
uniq, counts = np.unique(years, return_counts=True)
u_counts = np.vstack((uniq, counts)).T
#%% SORTING
# sorting in basic numby arrays is tricky...the default sort method sorts
# everything based on the axis you choose (0 or 1)
array = np.arange(1,9)
rand_data = np.random.randint(1,100,(8))
a_data = np.vstack((array,rand_data)).T
a_srt = np.sort(a_data,axis = 0) # independantly sorts all columns low-high
a_srtR = np.sort(a_data,axis = 1) # independantly sorts all rows low-high
# usually you want to sort a matrix by a specific row or column, so that your
# data stays together. For this we use a fancy indexing method with np.argsort
np.random.shuffle(a_data)
a_data[np.argsort(a_data[:,0])]
a_data[np.argsort(a_data[:,1])]
sort = a_data[a_data[:,1].argsort()]
#%% MASKING or BOOLEAN INDEXING / EXTRACTION
# you can "select" elements of your array based on a boolean test
#
a = np.random.random_integers(-20, 20, (10,10))
mask = (a <= 0)
# Extracted elements will be in a 1-D array
a_extract = a[mask]
# inverse masking
a_inverse = a[not mask]
# short cut, ~ = not
a_inverse = a[~mask]
# direct masking, putting the boolean inside the index
a_dir_mask = a[a >= 0]
# masks can also be used to select elements in other arrays of the same size
b = np.arange(0,10)
b_mask = b[mask]
# doing math with masks (reasigning)
a[mask] = a[mask] * 100
#%% ASSIGNING NAN
a = np.random.rand(10)
mask = (a >= 0.5)
a[mask] = np.nan
#%% STRUCTURED ARRAYS
# arrays that allow you to manipulate the data based on named fields
# also allow for different data types to be stored in the same array
# - Pandas Data Frame to do mostly same thing
# ** you're better off using Pandas for this type of data **
# DTYPES
# + Common dtypes:
# - 'b' boolean
# - 'i' (signed) integer
# - 'f' floating-point
# - 'S', 'a' (byte-)string
# - 'O' (Python) objects (i.e. datatime objects)
# Contructing structured arrays
#
a = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
'''
Lab 3 Grading Rubric
• Does the program calculate the distance correctly (i.e. does it agree with
the website provided)? 0-5 pts.
• Does the program get user inputs from the console and successfully screen
them for errors? 0-5 pts.
• Does the program print the required outputs to the console and are they
explained in a manner that the user knows what they are getting? 0-5 pts.
• Do the comments embedded in the code sufficiently explain the work flow and
the commands that are being executed (i.e. can I understand what your code is
doing without having to interpret commands from their actual syntax)? 0-5 pts.
• Extra Credit parts – Same criteria apply, 3 pts. available