Kaggle competition: SERPRO - Iris
<SOS> metric: categorizationaccuracy Kaggle data source: serpro-iris<load_from_csv>
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.model_selection import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
d = pd.read_csv('.. /input/train.csv')
 Y_train = d['target']
X_train = d['question_text']
d_test = pd.read_csv('.. /input/test.csv')
 X_test = d_test['question_text']<categorify>
file = '.. /input/iris-train.csv'
df = pd.read_csv(file, delimiter = ',', index_col='Id')
 X = df[['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']]
Y = df['Species']
def encode_digit(x):

 x = re.sub('[0-9]{5,}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
return x
def clean_digits(string_vec):

 cleaned_string = [encode_digit(s)for s in string_vec]
return pd.Series(cleaned_string)
 def clean_ws(string_vec):

 cleaned_string = [re.sub('\s+', ' ', s ).strip() for s in string_vec]
return pd.Series(cleaned_string)
 def clean_word(char, punct):

 for p in punct:
char = char.replace(p, f' {p} ')
 return(char)
 def clean_punct(string_vec, punct):

 cleaned_string = []
for char in tqdm(string_vec):
char = [clean_word(x, punct)for x in char.split() ]
cleaned_string.append(' '.join(char))
return pd.Series(cleaned_string)
 def tokenize_text(string_vec, tokenizer, max_len):

 token = tokenizer.texts_to_sequences(string_vec)
 token = pad_sequences(token, maxlen = max_len)
 return token<load_pretrained>
models = []
models.append(( 'Support Vector Machines - linear', svm.SVC(kernel='linear', random_state=0, gamma=.10, C=2.0)))
 models.append(( 'Logistic Regression - lbfgs', LogisticRegression(solver='lbfgs', multi_class='auto', max_iter=1000)))
 models.append(( 'KNeighborsClassifier', KNeighborsClassifier()))
models.append(( 'DecisionTreeClassifier', DecisionTreeClassifier()))
models.append(( 'RandomForestClassifier', RandomForestClassifier(n_estimators=100)))
 models.append(( 'Gaussian Naïve Bayes', GaussianNB()))
def load_from_text(path):

 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(path, errors='ignore'))
return embeddings_index
def read_embedding(path, reading_type, binary = False):

 if(reading_type == 'text'):
model_embed = load_from_text(path)
 if(reading_type == 'word2vec'):
model_embed = KeyedVectors.load_word2vec_format(path, binary = binary)
 return model_embed
def create_embedding_matrix(model_embed, tokenizer, max_features, embed_size):

 embedding_matrix = np.zeros(( max_features, embed_size))
for word, index in tokenizer.word_index.items() :
if index > max_features - 1:
break
else:
try:
embedding_matrix[index] = model_embed[word]
except:
continue
return embedding_matrix 
<compute_test_metric>
from sklearn.model_selection import cross_val_score
<compute_test_metric><EOS> 
model = models[0][1]
filename = models[0][0]
X_train = df.drop(['Species'], axis=1)
 Y_train = df['Species']
model.fit(X_train, Y_train)
 dfPredict = pd.read_csv('.. /input/iris-test.csv', delimiter=',', index_col='Id')
 xPredict = dfPredict[['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']]
yPredict = model.predict(xPredict)
 submission = pd.DataFrame({
'Id': xPredict.index,
'Species': yPredict
})
 submission.set_index('Id', inplace=True)
 submission.to_csv(filename+'.csv' )