Usefull functions Flashcards
1
Q
1) write number to file(file_name)
2) write array to file(file_name)
A
1)
def write_answer_(num,file_name):
with open(file_name,”w”) as fout:
fout.write(str(num))
2)
def write_answer_arr(scores_,file_name):
with open(file_name, “w”) as fout:
fout.write(“ “.join([str(num) for num in scores_]))
2
Q
map preds array of (0, 1, 2)s into mapped_preds array of (2, 0, 1)s
A
mapping = {0 : 2, 1: 0, 2: 1} mapped_preds = [mapping[pred] for pred in preds]
3
Q
count number of lines in file
A
1) num_lines = sum(1 for line in open(‘myfile.txt’))
2) num_lines = len(list(open(‘myfile.txt’)))
4
Q
change package in conda
A
conda install scikit-learn=0.17
5
Q
Create bootstrap sample
A
def get_bootstrap_samples(data, n_samples): indices = np.random.randint(0, len(data), (n_samples, len(data))) samples = data[indices] return samples