Skip to content

Commit 46c2dde

Browse files
committed
KM
1 parent 119f66d commit 46c2dde

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

toolkit/quickregression.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from sklearn.datasets import load_boston
2+
from sklearn.model_selection import train_test_split
3+
import numpy as np
4+
5+
boston = load_boston()
6+
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=17)
7+
8+
def quickregression(name):
9+
from sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error
10+
"""
11+
Function to save time when doing Machine Learning models.
12+
It only asks the name of the model to train and returns the scoring.
13+
14+
Parameters
15+
----------
16+
name = Name of the ML model.
17+
Input Example = LinearRegression
18+
19+
Returns
20+
----------
21+
MAE, MAPE, MSE, RMSE and R2 Scores.
22+
"""
23+
24+
# Fit of the model in the previously split X_train, y_train
25+
model = name()
26+
model.fit(X_train, y_train)
27+
# Predict of the model with X_test
28+
modpred = model.predict(X_test)
29+
# Scores of the model with y_test and the predict values.
30+
print("MAE test:", mean_absolute_error(y_test, modpred))
31+
print("MAPE test:", mean_absolute_percentage_error(y_test, modpred))
32+
print("MSE test:", mean_squared_error(y_test, modpred))
33+
print("RMSE test:", np.sqrt(mean_squared_error(y_test, modpred)))
34+
return(model.score(X_train, y_train))

toolkit/wordcloudviz.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def wordcloudviz(column):
2+
import matplotlib.pyplot as plt
3+
from wordcloud import WordCloud
4+
"""
5+
Function to create a quick visualization of wordclouds in a given column of a dataframe called df.
6+
7+
Parameters
8+
----------
9+
column = name of the column of the dataframe.
10+
Input example: df['column_name']
11+
12+
Return
13+
---------
14+
A wordcloud visualization of the words in the column.
15+
"""
16+
# First, it concatenates the text in a "single" text.
17+
text = " ".join(comment for comment in column)
18+
19+
# Creates a wordcloud visualization
20+
wordcloud = WordCloud(width=800, height=800, background_color='white').generate(text)
21+
22+
plt.figure(figsize=(8, 8), facecolor=None)
23+
plt.imshow(wordcloud)
24+
plt.axis("off")
25+
plt.tight_layout(pad=0)
26+
plt.show()

0 commit comments

Comments
 (0)