Skip to content

Instantly share code, notes, and snippets.

Avatar

k5.trismegistus k5trismegistus

View GitHub Profile
View pca.py
import matplotlib.pyplot as plt
import numpy
def pca(datas, dim):
cov_matrix = cov(transposition(datas))
evs = eigen_vectors(cov_matrix)
return [tuple(sum([a*b for a, b in zip(v,d)]) for v in evs[:dim]) for d in datas]
View multiple_regression_analysis.py
import numpy
import random
def create_datas(dimension):
datas = []
for i in range(50):
explanatory = [random.random() for i in range(dimension - 1)]
criterion = sum([d * (i + 1) for i, d in enumerate(explanatory)]) + 4
datas.append( (explanatory, criterion + random.random() * 0.3) )
return datas
View regression_analysis.py
import random
def create_datas():
datas = []
for i in range(50):
a = random.random()
datas.append((a, 3 * a + 2 + (random.random() - 0.5)))
return datas
def regression_analysis(datas):
View gist:afbc61433ac80c3f843e
def regression_analysis(datas):
# x, yの平均値を求める
x_ave = sum([d[0] for d in datas]) / len(datas)
y_ave = sum([d[1] for d in datas]) / len(datas)
# x, yの分散、xyの共分散を求める
c_xy = sum([(d[0] - x_ave) * (d[1] - y_ave) for d in datas]) / len(datas)
d_x = sum([(d[0] - x_ave) ** 2 for d in datas]) / len(datas)
d_y = sum([(d[1] - y_ave) ** 2 for d in datas]) / len(datas)
View gist:39e27f83a4670e145d8d
import random
def create_datas():
datas = []
for i in range(50):
a = random.random()
datas.append((a, 3 * a + 2 + (random.random() - 0.5)))
return datas
@k5trismegistus
k5trismegistus / neuralnetwork.py
Created Dec 25, 2015
Python標準ライブラリだけでニューラルネットワークを実装してみた
View neuralnetwork.py
import random
import itertools
import neuron
from IPython import embed
from IPython.terminal.embed import InteractiveShellEmbed
class NeuralNetwork():
def __init__(self, layers, learn_rate):
View greedy_flatten.py
test = [1, 2, 3, [4, 5], 6, 7, [8, [9, 10]], [[11, 12, [13, 14]]]]
def greedy_flatten(seq):
if not isinstance(seq, list):
return seq
seq = seq.copy()
while any([isinstance(x, list) for x in seq]):
for idx, x in enumerate(seq):
if isinstance(seq[idx], list):
View graph_maker.py
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import pygame
from pygame.locals import *
import serial
import sys
import re
import time
View googlenews.rb
require "nokogiri"
require "uri"
require 'net/http'
require "rss"
require "openssl"
keywords = ['旅順', '203高地']
news_feed_uri = URI.parse("https://news.google.com/news?output=rss&q=" + keywords.join('+'))
View multiprocess_test.py
import random
import time
import functools
from multiprocessing import Pool
def Timer(func):
@functools.wraps(func)
def Wrapper(*args, **kw):
stime = time.clock()
ret = func(*args, **kw)