-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathanalytics.py
More file actions
89 lines (78 loc) · 2.93 KB
/
Copy pathanalytics.py
File metadata and controls
89 lines (78 loc) · 2.93 KB
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
import string
import pandas as pd
import random
import numpy as np
from sklearn.linear_model import LinearRegression
class PyFrame:
def __init__(self, cache):
self.cache = cache
@classmethod
def makefm(cls, frame):
cache = {}
cache["data"] = frame
cache["version"] = 0
cache["low_version"] = 0
cache[0] = frame
return cls(cache)
@classmethod
def makefm_csv(cls, fileName):
frame = pd.read_csv(fileName)
cache = {}
cache["data"] = frame
cache["version"] = 0
cache["low_version"] = 0
cache[0] = frame
return cls(cache)
def id_generator(self, size=6, chars=string.ascii_uppercase + string.digits):
return "".join(random.choice(chars) for _ in range(size))
def makeCopy(this):
version = 0
if version in this.cache:
if "high_version" in this.cache:
version = this.cache["high_version"]
else:
version = this.cache["version"]
version = version + 1
this.cache[version] = this.cache["data"].copy()
this.cache["version"] = version
this.cache["high_version"] = version
# falls back to the last version
def fallback(this):
version = this.cache["version"]
low_version = this.cache["low_version"]
if version in this.cache and version > low_version:
this.cache["high_version"] = version
version = version - 1
this.cache["version"] = version
this.cache["data"] = this.cache[version]
else:
print("cant fall back. In the latest")
def get_correlation(this, columns="assign", inplace=True):
print(columns)
output = "to be assigned"
if columns == "assign":
output = this.cache["data"].corr().unstack().reset_index()
else:
print("in the else")
output = this.cache["data"][columns].corr().unstack().reset_index()
output.columns = ["Column_Header_X", "Column_Header_Y", "Correlation"]
return output
def get_regression(this, target, source):
model = LinearRegression(fit_intercept=True)
frame = this.cache["data"]
target = frame[target]
if len(source) == 1:
# print ('In Single Source Column')
temp = source[0]
# print('Recasting', temp)
source = frame[temp][:, np.newaxis]
else:
source = frame[source]
model.fit(source, target)
predict = list(model.predict(source))
row_id = list(range(0, len(predict)))
target = list(target)
# print(len(row_id), len(target), len(predict))
result = pd.DataFrame({"row": row_id, "actual": target, "fitted": predict})
result.columns = ["ROW_ID", "Actual", "Fitted"]
return result