Skip to content

Instantly share code, notes, and snippets.

View RyotaBannai's full-sized avatar
🛴
Man's soil is still rich enough to direct his own life.

Ryota Bannai RyotaBannai

🛴
Man's soil is still rich enough to direct his own life.
View GitHub Profile
from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
face_data_raw = faces.data
average_face = face_data_raw.mean(axis=0)
face_data = face_data_raw - average_face
pca = PCA(100).fit(face_data)
PC = pca.transform(face_data)
truncated = faces.data[:500] # 500 *2914 Matrix
average_face = truncted.mean(axis=0)
face_data = truncted - average_face
U, s, Vt = LA.svd(face_data, full_matrices=False)
S = np.diag(s)
#U: 500 *500, S: 500 *500, Vt:500 *2914
dim = 100
US = U[:, 0:dim].dot(S[0:dim, 0:dim])
X = US.dot(Vt[:dim,:])
X = X + average_face
fig, ax = plt.subplots(2, 5, figsize=(5, 2.5),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(5, 10):
ax[0, i-5].imshow(faces.data[i].reshape(62,47), cmap='binary_r')
ax[1, i-5].imshow(X[i].reshape(62,47), cmap='binary_r')
N=5
fig, ax = plt.subplots(1, N, figsize=(5, 2.5),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(1, N+1):
ax[i-1].imshow(Vt[i].reshape(62, 47), cmap='binary_r')
$heroku pg:backups:capture
//Make the latest backup file on Poshgres on heroku
$heroku pg:backups:download
//Download the latest backup file to the localhost(the name is latest.dump)
//Reference is below
$pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
db=# \t //1st command
db=# \a //2nd command
db=# \o file.json //3rd command
db=# SELECT json_agg(json_build_object( //4thcommand
'pk', t.id,'model','meryota.question', 'fields',
json_build_object('question_text',t.question_text,
'pub_date',t.pub_date))) from meryota_question as t;
[
{
"model": "myapp.person",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Lennon"
}
},
{