Skip to content

Instantly share code, notes, and snippets.

View JLFDataScience's full-sized avatar

Jose Luis Fernández Nuevo JLFDataScience

  • FGCSIC
View GitHub Profile
@JLFDataScience
JLFDataScience / Yandex_traslate_api.py
Last active June 23, 2020 17:09
Yandex traslate Api connection
#Import libraries
import pandas as pd
import numpy as np
import requests
#Test the Yandex API connection
key='APIKEY' #Enter the Apikey supplied to the registrar in Yandex
text = 'Hola Mundo'
lang = 'en'
url_yandex ="https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&text=%s&lang=%s" % (key,text,lang)
@JLFDataScience
JLFDataScience / Yandex_api_function.py
Last active January 24, 2020 14:12
Yandex api connection function
#Function to automate translation Yandex taslate
def traslate(text, key):
lang = 'en'
url_yandex ="https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&text=%s&lang=%s" % (key,text,lang)
time.sleep(0.3)
response = requests.get(url_yandex, timeout=None)
response_data = eval(response.content.decode('utf-8'))
lb = response_data['text'][0]
return lb
@JLFDataScience
JLFDataScience / Age_dummy.py
Last active January 24, 2020 14:11
Age feature to dummy
dummie = pd.get_dummies(myopia['AGE'], prefix = 'AGE')
names = list(dummie.columns)
names.remove(names[0])
features.remove('AGE')
myopia_dummy = pd.concat([myopia[features], dummie[names]], axis = 1)
@JLFDataScience
JLFDataScience / VIF_Function.py
Last active January 24, 2020 14:11
Implementation VIF Function
from sklearn.linear_model import LinearRegression
def calculateVIF(data):
features = list(data.columns)
num_features = len(features)
model = LinearRegression()
result = pd.DataFrame(index = ['VIF'], columns = features)
result = result.fillna(0)
@JLFDataScience
JLFDataScience / SelectKBest_method.py
Last active January 24, 2020 14:10
Selection of the best candidates with SelectKBest method of Scikit-learn
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
var_sk = SelectKBest(f_classif, k = 8)
var_sk.fit_transform(vif_selection, myopia[target])
k_selection = vif_selection.loc[:, var_sk.get_support()]
k_selection.head()
@JLFDataScience
JLFDataScience / Metric_model_function.py
Last active January 24, 2020 14:10
Metric Function of predictive model
from sklearn.metrics import accuracy_score
from sklearn.metrics import auc
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import roc_curve
def metrics(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred)
@JLFDataScience
JLFDataScience / training_validation.py
Last active January 24, 2020 14:10
Creating a training and validation dataset
from sklearn.model_selection import train_test_split
features = list(k_selection.columns)
x_train, x_test, y_train, y_test = train_test_split(k_selection, myopia[target], random_state = 0)
@JLFDataScience
JLFDataScience / Logistic_regression.py
Last active January 24, 2020 14:09
Logistic Regression
from sklearn.linear_model.logistic import LogisticRegression
model = LogisticRegression().fit(x_train, y_train)
y_pred_train = model.predict(x_train)
metrics(y_train, y_pred_train)
@JLFDataScience
JLFDataScience / Decision_trees.py
Last active January 24, 2020 14:09
Decision trees model
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(criterion = 'entropy',
max_depth = 2,
random_state = 0)
tree.fit(x_train, y_train)
y_pred_train = tree.predict(x_train)
metrics(y_train, y_pred_train)
@JLFDataScience
JLFDataScience / Random_forest.py
Last active January 24, 2020 14:09
Random Forest model
rf_classifier = RandomForestClassifier(n_estimators = 5, max_depth=4,
random_state = 1)
rf_classifier.fit(x_train, y_train)
y_pred = rf_classifier.predict(x_train)
metrics(y_train, y_pred)