Skip to content

Instantly share code, notes, and snippets.

@vinimonteiro
vinimonteiro / cos_sim.py
Created October 13, 2021 14:18
cos similarity example
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd
basetext = """
Quantum computers encode information in 0s and 1s at the same time, until you "measure" it
"""
text1 = """
A qubit stores "0 and 1 at the same time" in the same way how a car travelling north-west travels north and west at the same time
"""
text2 = """
@vinimonteiro
vinimonteiro / linear_regression.py
Created August 4, 2021 19:17
Linear regression algorithm from Pragramatic programmers
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def predict(X, w, b):
return X * w + b
def loss(X, Y, w, b):
return np.average((predict(X, w, b) - Y) ** 2)
@vinimonteiro
vinimonteiro / generator_large_file.py
Created April 16, 2021 15:30
generator_large_file
def gen_file_reader(file_path):
for row in open(file_path, "r"):
yield row
def gen_print_row_count():
count = 0
for row in gen_file_reader("large_file"):
count += 1
print(f"Total count is {count}")
@vinimonteiro
vinimonteiro / run_function_large_file.py
Created April 16, 2021 15:27
run function large file
$ python large_file_example.py
Total count is 72456321
@vinimonteiro
vinimonteiro / function_large_file.py
Created April 16, 2021 15:25
Row count large file with function
def file_reader(file_path):
rows = []
for row in open(file_path, "r"):
rows.append(row)
return rows
def print_row_count():
count = 0
for row in file_reader("large_file"):
count += 1
@vinimonteiro
vinimonteiro / hello_world_from_file.py
Created April 14, 2021 19:45
Python Hello World From File
$ python /Users/viniciusmonteiro/hello_world.py
Hello World
$
@vinimonteiro
vinimonteiro / hello_world.py
Created April 14, 2021 19:33
Hello World in Python
$ python
>>> print('Hello World')
Hello World
>>>
@vinimonteiro
vinimonteiro / int_python3
Created February 7, 2021 08:52
int in Python 3
>>> print(923125123133123123163123193133193122123923123177 + 5)
923125123133123123163123193133193122123923123182
@vinimonteiro
vinimonteiro / dict_comprehension
Created February 6, 2021 17:57
dict comprehension
>>> squares = {i:i*i for i in range(6)}
>>> squares
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
@vinimonteiro
vinimonteiro / set_comprehensions
Created February 6, 2021 17:56
set comprehensions
>>> squares = {i*i for i in range(6)}
>>> squares
{0, 1, 4, 9, 16, 25}