This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |