Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import numpy as np
- from scipy import spatial as sp
- amount_of_words = 0
- amount_of_sentences = 0
- freq = {}
- sentences = []
- text = open("2.3.1.txt", 'r')
- for line in text:
- amount_of_sentences += 1
- current = re.split('[^a-z]', line[: -2].lower())
- result = []
- for cur in current:
- if cur != '':
- result.append(cur)
- if cur not in freq.values():
- freq[amount_of_words] = cur
- amount_of_words += 1
- sentences.append(result)
- text.close()
- matrix_freq = np.zeros((amount_of_sentences, amount_of_words))
- i = 0
- for i in range(amount_of_sentences):
- for j in range(amount_of_words):
- matrix_freq[i][j] = sentences[i].count(freq[j])
- results = []
- comp = matrix_freq[0]
- min1 = 1
- min2 = 1
- min1_index = 0
- min2_index = 0
- for i in range(1, amount_of_sentences):
- x = sp.distance.cosine(comp, matrix_freq[i])
- results.insert(i, x)
- if x <= min1:
- min2 = min1
- min2_index = min1_index
- min1 = x
- min1_index = i
- elif x <= min2:
- min2 = x
- min2_index = i
- # for i in matrix_freq:
- # print(i)
- print(results)
- results.sort()
- print(results)
- print(min1, min2)
- print(min1_index, min2_index)
- res = open('2.3.1.result.txt', 'w')
- res.write(str(min1_index) + ' ' + str(min2_index))
- res.close()
- -----------------------------------------------------------------------------
- import re
- import numpy as np
- from math import pi, sin, exp
- def f(x):
- return sin(x / 5) * exp(x / 10) + 5 * exp(-1 * x / 2)
- f_1 = f(1)
- f_4 = f(4)
- f_8 = f(8)
- f_10 = f(10)
- f_15 = f(15)
- matrix_1 = np.array([[1, 1],
- [1, 15]])
- b_1 = np.array([f_1, f_15])
- x_1 = np.linalg.solve(matrix_1, b_1)
- # .....
- matrix_3 = np.array([[1, 1, 1, 1],
- [1, 4, 16, 64],
- [1, 10, 100, 1000],
- [1, 15, 225, 3375]])
- b_3 = np.array([f_1, f_4, f_10, f_15])
- x_3 = np.linalg.solve(matrix_3, b_3)
- x = list(map(str, x_3))
- print(type(x[0]))
- print(x)
- result = open("2.3.2.result.txt", 'w')
- result.write(x[0] + ' ' + x[1] + ' ' + x[2] + ' ' + x[3])
- result.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement