-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek6-LanguageDet
70 lines (46 loc) · 2.01 KB
/
Week6-LanguageDet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#sol. python
import numpy as np
import pandas as pd
def computeLanguageError(freq):
# Computes the squared error between the letter frequencies given
# as input and the letter frequencies of 15 languages stored in the
# file letter_frequencies.csv
# Open the file letter_frequencies.
data = pd.read_csv("letter_frequencies.csv")
# Extract a matrix containing the letter frequencies for the 15
# languages and transpose it to yield a size 15 x 26 matrix
languageFrequencies = np.array(data.iloc[0:26, 1:16]).T
# Compute the squared errors. Subtracting a vector from a matrix will
# "recycle" the vector, so it is subtracted from each row.
E = np.sum((languageFrequencies - freq)**2, axis=1)
return E
#sol. matlab
function E = computeLanguageError(freq)
% Computes the squared error between the letter frequencies given
% as input and the letter frequencies of 15 languages stored in the
% file letter_frequencies.csv
% Open the file letter_frequencies.
data = readtable('letter_frequencies.csv');
% Extract a 26 x 15 matrix containing the letter frequencies for
% the 15 languages
languageFrequencies = data{1:26, 2:16};
% Create a 26 x 15 matrix where each column is the letter
% frequencies in the text
textFrequencies = repmat(freq(:), 1, 15);
% Compute the squared errors
E = sum((languageFrequencies - textFrequencies).^2);
#sol. R
computeLanguageError <- function(freq) {
# Computes the squared error between the letter frequencies given
# as input and the letter frequencies of 15 languages stored in the
# file letter_frequencies.csv
# Open the file letter_frequencies.
data = read.csv("letter_frequencies.csv")
# Extract a 26 x 15 matrix containing the letter frequencies for
# the 15 languages
languageFrequencies = data[1:26, 2:16]
# Compute the squared errors. Subtracting a vector from a matrix will
# "recycle" the vector, so it is subtracted from each column.
E = apply((languageFrequencies - freq)^2, 2, sum)
return(E)
}