-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tasks #2
base: master
Are you sure you want to change the base?
Tasks #2
Changes from all commits
848aad6
5dd3ff3
2a41252
43a7177
ffcd196
ded44bc
b8a2e69
9112e68
a434425
fe92dac
fa4debc
b1ceac7
546d8b6
4f59c18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import numpy as np | ||
import scipy as sc | ||
import matplotlib.pyplot as pl | ||
from sklearn.utils import shuffle | ||
from sklearn import linear_model | ||
|
||
# Generate data | ||
x = np.linspace(10, 30, 1000) | ||
y = 0.75 * x + 1.35 | ||
|
||
# Add noise | ||
noise = np.random.normal(0,0.5, y.size) | ||
y_noise = y + noise * 5 | ||
|
||
# Make plot with noise | ||
pl.plot(x, y_noise, ',') | ||
pl.plot(x, y, 'k--', label ='Input') | ||
pl.legend(loc='best') | ||
|
||
|
||
# Train and test sets | ||
x = x.reshape((x.shape[0],-1)) | ||
x, y_noise = shuffle(x, y_noise, random_state=1) | ||
x_train = x[:600] | ||
x_test = x[600:] | ||
y_train = y_noise[:600] | ||
y_test = y_noise[600:] | ||
|
||
print x_test.shape | ||
|
||
# Linear regression | ||
regr = linear_model.LinearRegression() | ||
regr.fit(x_train, y_train) | ||
|
||
print 'Coefficients: ', regr.coef_, regr.intercept_ | ||
print "Residual sum of squares, train: ", np.mean((regr.predict(x_train) - y_train) ** 2) | ||
print "Residual sum of squares, test: ", np.mean((regr.predict(x_test) - y_test) ** 2) | ||
print 'R^2:', regr.score(x,y_noise) | ||
|
||
|
||
# Plot linear regression | ||
pl.scatter(x_train, y_train, color='grey', label='Train') | ||
pl.scatter(x_test, y_test, color='red', label='Test') | ||
pl.plot(x_test, regr.predict(x_test),'b-', label='Predict') | ||
y = 0.75 * x + 1.35 | ||
pl.plot(x, y, 'k--', label='Input') | ||
pl.xlabel('X') | ||
pl.ylabel('Y') | ||
pl.legend(loc='best') | ||
|
||
|
||
# Second plot (to be continued...) | ||
pl.scatter(x, y_noise, color='grey') | ||
pl.plot(x_test, regr.predict(x_test),'b-', label='Predict') | ||
pl.legend(loc='best') | ||
|
||
|
||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as pl | ||
from sklearn.utils import shuffle | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from sklearn.ensemble import RandomForestClassifier | ||
|
||
#Import dataset | ||
red = np.loadtxt("./red.txt") | ||
blue = np.loadtxt("./blue.txt") | ||
|
||
#Plot data | ||
pl.prism() | ||
plt.xlim(-1.2, 1.2) | ||
plt.ylim(-1.2, 1.2) | ||
pl.scatter(red[:, 0], red[:, 1], c='red') | ||
pl.scatter(blue[:, 0], blue[:, 1], c='blue') | ||
|
||
#Prepare data for analysis | ||
reds = np.hstack ((red, [[1]] * len (red) )) | ||
blues = np.hstack ((blue, [[0]] * len (blue) )) | ||
dots = np.concatenate((reds, blues), axis=0) | ||
x = dots[:, :-1] | ||
y = dots[:, 2] | ||
|
||
#Train and test sets | ||
x, y = shuffle(x, y, random_state=1) | ||
size=dots.shape[0] * 0.8 | ||
x_train = x[:size] | ||
y_train = y[:size] | ||
x_test = x[size:] | ||
y_test = y[size:] | ||
|
||
#Build clussifier (KNN) | ||
knn = KNeighborsClassifier(n_neighbors=5) | ||
knn.fit(x_train, y_train) | ||
print 'Accuracy of KNN train set:', knn.score(x_train, y_train) | ||
print 'Accuracy of KNN test set:', knn.score(x_test, y_test) | ||
|
||
#Plot KNN | ||
y_pred_test_KNN = knn.predict(x_test) | ||
plt.xlim(-1.2, 1.2) | ||
plt.ylim(-1.2, 1.2) | ||
plt.scatter(x_test[:, 0], x_test[:, 1], c=y_pred_test_KNN, marker='^') | ||
plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train) | ||
|
||
#Build clussifier (Random forest) | ||
rf = RandomForestClassifier(n_estimators=10) | ||
rf.fit(x_train, y_train) | ||
print 'Accuracy of Random Forest train set:', rf.score(x_train, y_train) | ||
print 'Accuracy of Random Forest test set:', rf.score(x_test, y_test) | ||
|
||
#Plot RF | ||
y_pred_test_RF = rf.predict(x_test) | ||
plt.xlim(-1.2, 1.2) | ||
plt.ylim(-1.2, 1.2) | ||
plt.scatter(x_test[:, 0], x_test[:, 1], c=y_pred_test+RF, marker='^') | ||
plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train) | ||
|
||
#Predict color | ||
def predict_KNN(a,b): | ||
if knn.predict([a, b]) == 0: | ||
return 'blue' | ||
else: | ||
return 'red' | ||
|
||
def predict_RF(a,b): | ||
if rf.predict([a, b]) == 0: | ||
return 'blue' | ||
else: | ||
return 'red' | ||
|
||
a = raw_input() | ||
b = raw_input() | ||
print 'Predict color KNN:', predict_KNN(a,b) | ||
print 'Predict color RF:', predict_RF(a,b) | ||
|
||
print 'Best model is:' | ||
if rf.score(x_test, y_test) >= knn.score(x_test, y_test): | ||
print 'Random forest' | ||
else: | ||
print 'K-Nearest Neighbors' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
k = 3 | ||
def gen_bin(a,p): | ||
if p < k: | ||
a[p] = 0 | ||
gen_bin(a, p+1) | ||
a[p] = 1 | ||
gen_bin(a, p+1) | ||
else: | ||
print(a) | ||
|
||
gen_bin([0 for i in range (k)], 0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
infile = open('allvectors.in', 'r') | ||
outfile = open('allvectors.out', 'w') | ||
n = int(infile.readline().strip()) | ||
|
||
a=[0]*n | ||
def gen_bin(a,p): | ||
if p < n: | ||
a[p] = 0 | ||
gen_bin(a, p+1) | ||
a[p] = 1 | ||
gen_bin(a, p+1) | ||
else: | ||
outfile.write(str(''.join([str(i) for i in a])) + '\n') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
gen_bin(a,0) | ||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
infile = open('choose.in', 'r') | ||
outfile = open('choose.out', 'w') | ||
|
||
n, k = [int(i) for i in infile.readline().split()] | ||
seq=range(1,n+1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def choose(seq, k): | ||
if k == 0: | ||
yield [] | ||
else: | ||
for i, x in enumerate(seq): | ||
for c in choose(seq[i+1:], k-1): | ||
yield [x] +c | ||
|
||
for i in choose(seq, k): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А используется только тут... Хорошо объявлять переменную как можно ближе к первому использованию. И совершенно точно не стоит отделять объявление и использование функцией, у которой параметр называется также, как и переменная: очень просто запутаться, к чему именно относится идентификатор seq. |
||
outfile.write(str.join(' ', (str(j) for j in i)) + '\n') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И лучше было бы переменную |
||
|
||
infile.close() | ||
outfile.close() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
infile = open('permutations.in', 'r') | ||
outfile = open('permutations.out', 'w') | ||
|
||
n = int(infile.readline().strip()) | ||
seq=range(1,n+1) | ||
|
||
ans=[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было бы обойтись без этой глобальной перменной. В такой маленькой задаче это, наверное, не очень важно, но вообще не константная глобальная перменная это очень плохо. |
||
|
||
def permutation(seq, i): | ||
if i == len(seq)-1: | ||
ans.append(str(''.join([str(i) for i in seq]))) | ||
else: | ||
for j in range(i, len(seq)): | ||
seq[i], seq[j] = seq[j], seq[i] | ||
permutation(seq, i+1) | ||
seq[i], seq[j] = seq[j], seq[i] | ||
|
||
permutation(seq, 0) | ||
for i in sorted(ans): | ||
outfile.write(str(' '.join([str(j) for j in i])+'\n')) | ||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#permutations code from class work | ||
|
||
infile = open('permutations.in', 'r') | ||
outfile = open('permutations.out', 'w') | ||
|
||
n = int(infile.readline().strip()) | ||
def gen_perm(a, p): | ||
if p < n: | ||
for i in range(1, n+1): | ||
if i not in a: | ||
a[p] = i | ||
gen_perm(a, p+1) | ||
a[p] = 0 | ||
else: | ||
outfile.write(str.join(' ', (str(i) for i in a))+'\n') | ||
|
||
gen_perm([0 for i in range (n)], 0) | ||
|
||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import re | ||
infile = open('vectors.in', 'r') | ||
outfile = open('vectors.out', 'w') | ||
|
||
n = int(infile.readline().strip()) | ||
|
||
a = [0]*n | ||
all_bin=[] | ||
def gen_bin(a,p): | ||
if p < n: | ||
a[p] = 0 | ||
gen_bin(a, p+1) | ||
a[p] = 1 | ||
gen_bin(a, p+1) | ||
else: | ||
all_bin.append(str(''.join([str(i) for i in a]))) | ||
|
||
gen_bin(a,0) | ||
|
||
ones=[] | ||
for i in all_bin: | ||
for match in re.findall('11', i): | ||
ones.append(i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Очень сложно, если я правильно понял, как это должно работать =) Вот так вроде проще if i.find('11') > -1:
ones.append(i) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Но лучше вообще использовать set comprehension
|
||
ans=sorted(list(set(all_bin).difference(set(ones)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если это задача про последовательности без двух единичек подряд, то её можно проще и красивие решить) Там даже появятся числа Фибоначчи! |
||
|
||
outfile.write(str(len(ans))+'\n') | ||
outfile.write(str('\n'.join([str(i) for i in ans]))) | ||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from collections import deque | ||
|
||
infile = open('components.in', 'r') | ||
outfile = open('components.out', 'w') | ||
|
||
n,e = [int(i) for i in infile.readline().split()] | ||
|
||
#edgelist | ||
edges=[] | ||
for i in range(e): | ||
edges.append([int(j) for j in infile.readline().split()]) | ||
|
||
#neighbours | ||
from collections import defaultdict | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Все импорты должны располагаться в начале файла. |
||
neighbours = defaultdict(lambda: defaultdict(lambda: 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Но вообще в данном случае не очень хорошо использовать Это будет лучше с той точки зрения что, если случайно обратиться к несуществующей вершине, будет исключение, а не silent failure. Короче говоря, проще будет искать баги в коде |
||
for v1, v2 in edges: | ||
neighbours[v1][v2] += 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А зачем тут счётчики количества рёбер? Вроде бы кратность нигде не используется, и было бы достаточно True/False. Ну или вообще для каждой вершинки хранить set соседей. |
||
neighbours[v2][v1] += 1 | ||
|
||
def dfs(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется это bfs |
||
num_component ={} | ||
comp = 0 | ||
for i in range(1, n+1): | ||
if i not in num_component: | ||
comp += 1 | ||
num_component[i] = comp | ||
queue = deque([i]) | ||
while len(queue) > 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for v in neighbours[queue[0]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше сказать u = queue.popleft()
for v in neighbours[u]:
... чем сначала итерироваться по queue[0], а потом делать popleft |
||
if v not in num_component: | ||
num_component[v] = comp | ||
queue.append(v) | ||
queue.popleft() | ||
outfile.write(str(comp)+ '\n') | ||
for i in num_component.values(): | ||
outfile.write(str(i)+' ') | ||
dfs() | ||
|
||
|
||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
infile = open('pathbge1.in', 'r') | ||
outfile = open('pathbge1.out', 'w') | ||
|
||
n,e = [int(i) for i in infile.readline().split()] | ||
|
||
#edgelist | ||
edges=[] | ||
for i in range(e): | ||
edges.append([int(j) for j in infile.readline().split()]) | ||
|
||
#linking edges | ||
def link(u, v): | ||
if u not in graph: | ||
graph[u] = {} | ||
(graph[u])[v] = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем скобочки? |
||
if v not in graph: | ||
graph[v] = {} | ||
(graph[v])[u] = 1 | ||
return graph | ||
|
||
graph = {} | ||
for (x,y) in edges: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно проще vertexes = range(1, n+1)
graph = {v: set() for v in vertexes}
for u, v in edges:
graph[v].add(u)
graph[u].add(v) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Главная проблема в link -- то, что используется глобальная переменная graph. Это не очень хорошо. Лучше было бы сделать def build_graph_from_edges(edges):
graph = {}
bla-bla-bla
return graph |
||
link(x,y) | ||
dist = {} #distance | ||
|
||
def bfs (s, v): | ||
q = [s] #queue | ||
dist[s] = 0 | ||
while len(q) > 0: | ||
current = q.pop(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вроде можно сказать просто There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А нет, я соврал =) |
||
for i in graph[current].keys(): | ||
if i not in dist: | ||
dist[i] = dist[current] + 1 | ||
if i == v: | ||
return dist[v] | ||
q.append(i) | ||
return dist | ||
|
||
bfs(1,n+1) | ||
for j in dist.values(): | ||
outfile.write(str(j) + ' ') | ||
|
||
infile.close() | ||
outfile.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
infile = open('pathmgep.in', 'r') | ||
outfile = open('pathmgep.out', 'w') | ||
|
||
n, S, F = [int(i) for i in infile.readline().split()] | ||
|
||
dist = [] | ||
|
||
sum=0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. плохое имя для переменной, потому что есть buildin функция sum |
||
for line in infile.read().splitlines(): | ||
values = [int(i) for i in line.split()] | ||
dist.append(values) | ||
for i in values: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше вместо цикла записать
где первый sum это переменная, а второй -- та самая build-in функция |
||
if i>0: | ||
sum+=i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kill it with fire: http://evadeflow.com/wp-content/uploads/2011/03/TabsSpacesBoth.png Смешивать табы и пробелы в питоне очень плохо. Кстати, у вас же вроде бы третий питон? В нём смесь табов и пробелов вообще не должна работать. |
||
|
||
for i in range(n): | ||
for j in range(n): | ||
if dist[i][j] == -1: | ||
dist[i][j] = sum +1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Вообще, с учётом ограничений на задачу, можно в начале сказать inf = 10**9 и использовать inf вместо sum -- вроде должно чуть упросить код, и название переменной будет более самодокументирующиеся |
||
|
||
def floyd(dist): | ||
for k in range(n): | ||
for i in range(n): | ||
for j in range(n): | ||
dist[i][j] = min(dist[i][j], (dist[i][k] + dist[k][j])) | ||
|
||
|
||
floyd(dist) | ||
|
||
if dist[S-1][F-1]>=sum: | ||
dist[S-1][F-1]=-1 | ||
|
||
outfile.write(str(dist[S-1][F-1])) | ||
infile.close() | ||
outfile.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strip
лишний