Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Topological_Machine_Learning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Colbry, Dirk
Topological_Machine_Learning
Commits
07138663
Commit
07138663
authored
5 years ago
by
shawk masboob
Browse files
Options
Downloads
Patches
Plain Diff
added new functions
parent
818a46ad
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Topological_ML/TDA_Prediction.py
+47
-17
47 additions, 17 deletions
Topological_ML/TDA_Prediction.py
with
47 additions
and
17 deletions
Topological_ML/TDA_Prediction.py
+
47
−
17
View file @
07138663
from
Topological_ML
import
TDA_Prediction
as
tdap
from
sklearn.datasets
import
fetch_california_housing
from
sklearn.model_selection
import
train_test_split
from
sklearn.linear_model
import
LinearRegression
import
kmapper
as
km
import
pandas
as
pd
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
sklearn
from
sklearn
import
ensemble
def
numpy_to_pandas
(
sklearn_data
):
"""
Converts scikit-learn numpy data into pandas dataframe.
...
...
@@ -8,16 +19,17 @@ def numpy_to_pandas(sklearn_data):
df
[
'
response
'
]
=
pd
.
Series
(
sklearn_data
.
target
)
return
df
def
d
escriptive_statistic
(
df
,
n
):
def
d
ata_summary
(
df
,
n
):
"""
Provides brief descriptive statistics on dataset.
Input: df = dataframe
n = the first n rows of the dataframe
Output: shape, head, and descriptive statistics of dataframe
Input: name of dataframe
Output: dictionary
"""
print
(
"
Shape :
"
,
df
.
shape
)
print
(
"
Head --
\n
"
,
df
.
head
(
n
))
print
(
"
Describe :
"
,
df
.
describe
())
d
=
dict
()
d
[
'
head
'
]
=
df
.
head
(
n
)
d
[
'
shape
'
]
=
df
.
shape
#d['missing values'] = df.isna().sum()
return
d
def
model_selection
(
df
):
"""
...
...
@@ -38,29 +50,47 @@ def MSE_fit(fit):
MSE
=
None
return
MSE
def
accuracy_metrics
(
fit
,
MSE
):
def
accuracy_metrics
(
fit
,
MSE
,
n
,
k
):
"""
This function is used for model validation. It returns a dictionary
of several regression model accuracy metrics. Its inputs are a fitted model
and the MSE of the fitted model.
"""
d
=
dict
()
sumObj
=
None
SSE
=
None
y_hat
=
model
.
predict
(
X
)
resid
=
y
-
y_hat
SSE
=
sum
(
resid
**
2
)
n
=
None
p
=
None
pr
=
None
d
[
'
R2
'
]
=
None
d
[
'
R2ad
'
]
=
None
d
[
'
AIC
'
]
=
None
d
[
'
BIC
'
]
=
None
d
[
'
AIC
'
]
=
2
*
k
-
2
*
ln
(
SSE
)
d
[
'
BIC
'
]
=
n
*
ln
(
SSE
/
n
)
+
k
*
ln
(
n
)
d
[
'
PRESS
'
]
=
None
d
[
'
Cp
'
]
=
None
return
None
def
linear_regression
(
x
,
y
):
"""
Ordinary least squares Linear Regression.
input: x = independent variables
y = dependent variable
output: R^2
"""
model
=
LinearRegression
()
model
.
fit
(
x
,
y
)
return
model
.
score
(
x
,
y
)
def
mysqrt
(
n
):
if
n
<
0
:
n
=
1.5
*
abs
(
n
)
sqrt1
=
n
**
(
1
/
2
)
return
sqrt1
\ No newline at end of file
def
lens_1d
(
X
,
rs
,
v
):
"""
input:
output:
"""
model
=
sklearn
.
ensemble
.
IsolationForest
(
random_state
=
rs
)
model
.
fit
(
X
)
lens1
=
model
.
decision_function
(
X
).
reshape
((
X
.
shape
[
0
],
1
))
mapper
=
km
.
KeplerMapper
(
verbose
=
v
)
lens2
=
mapper
.
fit_transform
(
X
,
projection
=
"
l2norm
"
)
lens
=
np
.
c_
[
lens1
,
lens2
]
return
lens
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment