-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.py
61 lines (47 loc) · 1.83 KB
/
Models.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
def getLogisticRegression(X_list, X_values):
"""
Function to train a logistic regression model.
Used to predict if passed in values would result in 5-star rating.
Returns formatted string with yes/no result & model accuracy.
"""
# Read in data
df = pd.read_csv('./CarRentalDataCleaned.csv')
# Train the logistic regression model
X_train, X_test, y_train, y_test = train_test_split(df[X_list], df['recommended'])
LogReg = LogisticRegression()
LogReg.fit(X_train, y_train)
# Run prediction and gets accuracy score
result = LogReg.predict(np.array([X_values]))[0]
accuracy = LogReg.score(X_test, y_test)
# Returns the result as formatted string
if result == 1:
return f"Vehicle is predicted to be recommended! The model is {(accuracy*100):.2f}% confident in it's accuracy."
else:
return f"Vehicle is predicted to be not recommended. The model is {(accuracy*100):.2f}% confident in it's accuracy."
def getKMeansGraph(x_axis, y_axis, cluster_count):
"""
Function to determin clusters using K-means
Saves created graph as an image to be used by `KMeansTab.py`
"""
# Read in data
df = pd.read_csv('./CarRentalDataCleaned.csv')
# Run K-Mean Clustering Model
kmeans = KMeans(n_clusters=cluster_count)
kmeans = kmeans.fit(df[[x_axis, y_axis]])
clusters = []
clusters = kmeans.labels_
# create graph
plot = sns.scatterplot(x=x_axis, y=y_axis, hue=clusters, data=df)
plt.savefig('output.png')
# Delete dataframe from memory.
del df
del clusters
del kmeans
del plot