4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の「行列計算メニュー」を、numpyとpandasでやってみた。

Last updated at Posted at 2025-12-15

paizaラーニングレベルアップ問題集の行列計算メニューを、numpyとpandasでやってみました。


ベクトル

ベクトルの足し算
問題

numpy
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
B = np.array([int(b) for b in input().split()])
C = A + B
print(*C)
pandas
import pandas as pd
N = int(input())
A = pd.Series([int(a) for a in input().split()])
B = pd.Series([int(b) for b in input().split()])
C = A + B
print(*C)

ベクトルの引き算
問題

numpy
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
B = np.array([int(b) for b in input().split()])
C = A - B
print(*C)
pandas
import pandas as pd
N = int(input())
A = pd.Series([int(a) for a in input().split()])
B = pd.Series([int(b) for b in input().split()])
C = A - B
print(*C)

ベクトルの内積
問題

numpy
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
B = np.array([int(b) for b in input().split()])
C = A @ B
print(C)
pandas
import pandas as pd
N = int(input())
A = pd.Series([int(a) for a in input().split()])
B = pd.Series([int(b) for b in input().split()])
C = A @ B
print(C)

ベクトルの応用1(ユークリッド距離)
問題

numpy
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
B = np.array([int(b) for b in input().split()])
C = A - B
print(C @ C)
pandas
import pandas as pd
N = int(input())
A = pd.Series([int(a) for a in input().split()])
B = pd.Series([int(b) for b in input().split()])
C = A - B
print(C @ C)

ベクトルの応用2(線分の直交判定)
問題

numpy
import numpy as np
A = np.array([int(a) for a in input().split()])
B = np.array([int(b) for b in input().split()])
print("No" if A @ B else "Yes")
pandas
import pandas as pd
A = pd.Series([int(a) for a in input().split()])
B = pd.Series([int(b) for b in input().split()])
print("No" if A @ B else "Yes")

行列

行列の足し算
問題

numpy
import numpy as np
H, W = map(int, input().split())
A = np.array([[int(a) for a in input().split()] for _ in range(H)])
B = np.array([[int(b) for b in input().split()] for _ in range(H)])
C = A + B
for row in C:
	print(*row)
pandas
import pandas as pd
H, W = map(int, input().split())
A = pd.DataFrame([[int(a) for a in input().split()] for _ in range(H)])
B = pd.DataFrame([[int(b) for b in input().split()] for _ in range(H)])
C = A + B
for row in C.iterrows():
	print(*row[1])

行列の引き算
問題

numpy
import numpy as np
H, W = map(int, input().split())
A = np.array([[int(a) for a in input().split()] for _ in range(H)])
B = np.array([[int(b) for b in input().split()] for _ in range(H)])
C = A - B
for row in C:
	print(*row)
pandas
import pandas as pd
H, W = map(int, input().split())
A = pd.DataFrame([[int(a) for a in input().split()] for _ in range(H)])
B = pd.DataFrame([[int(b) for b in input().split()] for _ in range(H)])
C = A - B
for row in C.iterrows():
	print(*row[1])

行列の掛け算
問題

numpy
import numpy as np
Ha, Wa, Hb, Wb = map(int, input().split())
A = np.array([[int(a) for a in input().split()] for _ in range(Ha)])
B = np.array([[int(b) for b in input().split()] for _ in range(Hb)])
C = A @ B
for row in C:
	print(*row)
pandas
import pandas as pd
Ha, Wa, Hb, Wb = map(int, input().split())
A = pd.DataFrame([[int(a) for a in input().split()] for _ in range(Ha)])
B = pd.DataFrame([[int(b) for b in input().split()] for _ in range(Hb)])
C = A @ B
for row in C.iterrows():
	print(*row[1])

回転行列
問題

numpy
import numpy as np
R = {
	0 : np.array([[1, 0], [0, 1]]),
	90 : np.array([[0, -1], [1, 0]]),
	180 : np.array([[-1, 0], [0, -1]]),
	270 : np.array([[0, 1], [-1, 0]]),
}
M = int(input())
P = np.array([int(input()) for _ in range(2)])
Q = R[M] @ P
for q in Q:
	print(q)
pandas
import pandas as pd
R = {
	0 : pd.DataFrame([[1, 0], [0, 1]]),
	90 : pd.DataFrame([[0, -1], [1, 0]]),
	180 : pd.DataFrame([[-1, 0], [0, -1]]),
	270 : pd.DataFrame([[0, 1], [-1, 0]]),
}
M = int(input())
P = pd.Series([int(input()) for _ in range(2)])
Q = R[M] @ P
for q in Q:
	print(q)

ハミング符号1(データを符号化する)
問題

numpy
import numpy as np
G = np.array([
	[1, 0, 0, 0, 0, 1, 1],
	[0, 1, 0, 0, 1, 0, 1],
	[0, 0, 1, 0, 1, 1, 0],
	[0, 0, 0, 1, 1, 1, 1],
])
U = np.array([int(u) for u in input().split()])
V = U @ G
W = V % 2
print(*W)
pandas
import pandas as pd
G = pd.DataFrame([
	[1, 0, 0, 0, 0, 1, 1],
	[0, 1, 0, 0, 1, 0, 1],
	[0, 0, 1, 0, 1, 1, 0],
	[0, 0, 0, 1, 1, 1, 1],
])
U = pd.Series([int(u) for u in input().split()])
V = U @ G
W = V % 2
print(*W)

ハミング符号2(誤りがあるかを判定する)
問題

numpy
import numpy as np
H = np.array([
	[1, 0, 0],
	[0, 1, 0],
	[1, 1, 0],
	[0, 0, 1],
	[1, 0, 1],
	[0, 1, 1],
	[1, 1, 1],
])
E = [np.array([1 if i == j else 0 for j in range(7)]) for i in range(7)]
W = np.array([int(w) for w in input().split()])
R = W @ H
S = R % 2
print(*S)
if np.count_nonzero(S) == 0:
	print(-1)
else:
	for i in range(7):
		P = E[i] @ H
		Q = P % 2
		if np.array_equal(Q, S):
			print(i + 1)
pandas
import pandas as pd
H = pd.DataFrame([
	[1, 0, 0],
	[0, 1, 0],
	[1, 1, 0],
	[0, 0, 1],
	[1, 0, 1],
	[0, 1, 1],
	[1, 1, 1],
])
E = [pd.Series([1 if i == j else 0 for j in range(7)]) for i in range(7)]
W = pd.Series([int(w) for w in input().split()])
R = W @ H
S = R % 2
print(*S)
if S[S == 0].size == 3:
	print(-1)
else:
	for i in range(7):
		P = E[i] @ H
		Q = P % 2
		if S.equals(Q):
			print(i + 1)

ハミング符号3(誤り訂正をおこなう)
問題

numpy
import numpy as np
H = np.array([
	[1, 0, 0],
	[0, 1, 0],
	[1, 1, 0],
	[0, 0, 1],
	[1, 0, 1],
	[0, 1, 1],
	[1, 1, 1],
])
E = [np.array([1 if i == j else 0 for j in range(7)]) for i in range(7)]
W = np.array([int(w) for w in input().split()])
R = W @ H
S = R % 2
if np.count_nonzero(S) == 0:
	print(*W)
else:
	for i in range(7):
		P = E[i] @ H
		Q = P % 2
		if np.array_equal(Q, S):
			X = W + E[i]
			Y = X % 2
			print(*Y)
pandas
import pandas as pd
H = pd.DataFrame([
	[1, 0, 0],
	[0, 1, 0],
	[1, 1, 0],
	[0, 0, 1],
	[1, 0, 1],
	[0, 1, 1],
	[1, 1, 1],
])
E = [pd.Series([1 if i == j else 0 for j in range(7)]) for i in range(7)]
W = pd.Series([int(w) for w in input().split()])
R = W @ H
S = R % 2
if S[S == 0].size == 3:
	print(*W)
else:
	for i in range(7):
		P = E[i] @ H
		Q = P % 2
		if S.equals(Q):
			X = W + E[i]
			Y = X % 2
			print(*Y)
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?