-
Notifications
You must be signed in to change notification settings - Fork 7
/
nuclearcli.py
179 lines (139 loc) · 4.3 KB
/
nuclearcli.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
"""
Nuclear Powered Cli
* GPU parallization
* Machine Learning/Clustering
* JIT
* Multi-threading
* Colored output
"""
import click
from numba import (cuda, vectorize)
import numba
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import KMeans
from functools import wraps
from time import time
@click.group()
def cli():
pass
def timing(f):
@wraps(f)
def wrap(*args, **kw):
ts = time()
result = f(*args, **kw)
te = time()
print(f"fun: {f.__name__}, args: [{args}, {kw}] took: {te-ts} sec")
return result
return wrap
def real_estate_df():
"""30 Years of Housing Prices"""
df = pd.read_csv("https://raw.githubusercontent.com/noahgift/real_estate_ml/master/data/Zip_Zhvi_SingleFamilyResidence.csv")
df.rename(columns={"RegionName":"ZipCode"}, inplace=True)
df["ZipCode"]=df["ZipCode"].map(lambda x: "{:.0f}".format(x))
df["RegionID"]=df["RegionID"].map(lambda x: "{:.0f}".format(x))
return df
def numerical_real_estate_array(df):
"""Converts df to numpy numerical array"""
columns_to_drop = ['RegionID', 'ZipCode', 'City', 'State', 'Metro', 'CountyName']
df_numerical = df.dropna()
df_numerical = df_numerical.drop(columns_to_drop, axis=1)
return df_numerical.values
def real_estate_array():
"""Returns Real Estate Array"""
df = real_estate_df()
rea = numerical_real_estate_array(df)
return np.float32(rea)
def kmeans_cluster_housing(clusters=3):
"""Kmeans cluster a dataframe"""
val_housing_win_df =\
pd.read_csv("https://raw.githubusercontent.com/noahgift/socialpowernba/master/data/nba_2017_att_val_elo_win_housing.csv")
numerical_df =\
val_housing_win_df.loc[:,["TOTAL_ATTENDANCE_MILLIONS", "ELO",
"VALUE_MILLIONS", "MEDIAN_HOME_PRICE_COUNTY_MILLIONS"]]
#scale data
scaler = MinMaxScaler()
scaler.fit(numerical_df)
scaler.transform(numerical_df)
#cluster data
k_means = KMeans(n_clusters=clusters)
kmeans = k_means.fit(scaler.transform(numerical_df))
val_housing_win_df['cluster'] = kmeans.labels_
return val_housing_win_df
@timing
def expmean(rea):
"""Regular Function"""
val = rea.mean() ** 2
return val
@timing
@numba.jit(nopython=True)
def expmean_jit(rea):
"""Perform multiple mean calculations"""
val = rea.mean() ** 2
return val
@vectorize(['float32(float32, float32)'], target='cuda')
def add_ufunc(x, y):
return x + y
@timing
@numba.jit(parallel=True)
def add_sum_threaded(rea):
"""Use all the cores"""
x,_ = rea.shape
total = 0
for _ in numba.prange(x):
total += rea.sum()
print(total)
@timing
def add_sum(rea):
"""traditional for loop"""
x,_ = rea.shape
total = 0
for _ in numba.prange(x):
total += rea.sum()
print(total)
@cli.command()
@click.option('--threads/--no-jit', default=False)
def thread_test(threads):
rea = real_estate_array()
if threads:
click.echo(click.style('Running with multicore threads', fg='green'))
add_sum_threaded(rea)
else:
click.echo(click.style('Running NO THREADS', fg='red'))
add_sum(rea)
@cli.command()
def cuda_operation():
"""Performs Vectorized Operations on GPU"""
x = real_estate_array()
y = real_estate_array()
print("Moving calculations to GPU memory")
x_device = cuda.to_device(x)
y_device = cuda.to_device(y)
out_device = cuda.device_array(
shape=(x_device.shape[0],x_device.shape[1]), dtype=np.float32)
print(x_device)
print(x_device.shape)
print(x_device.dtype)
print("Calculating on GPU")
add_ufunc(x_device,y_device, out=out_device)
out_host = out_device.copy_to_host()
print(f"Calculations from GPU {out_host}")
@cli.command()
@click.option('--jit/--no-jit', default=False)
def jit_test(jit):
rea = real_estate_array()
if jit:
click.echo(click.style('Running with JIT', fg='green'))
expmean_jit(rea)
else:
click.echo(click.style('Running NO JIT', fg='red'))
expmean(rea)
@cli.command()
@click.option("--num", default=3, help="number of clusters")
def cluster(num):
df = kmeans_cluster_housing(clusters=num)
click.echo("Clustered DataFrame")
click.echo(df.head())
if __name__ == "__main__":
cli()