Pworkspace

week10 - numpy(쉅안온날)

haerangssa 2024. 5. 28. 12:46
'''import numpy as np
data = [1,2,3]
a=np.array(data)
print(a)
a_rb = list(a)
print(a_rb)

data = [[1,2,3],[4,5,6],[7,8,9]]
a=np.array(data)
print(a)
print(a[0:2])
print(a[0,2])
print(a[0][2])

print("--------------------")
print(a[0:3:2,0:3:2])

print("--------------------")
a_1 = a[0:3:2,0:3:2]
print(a_1)

print("--------------------")
print(a_1.shape)
print(a.shape)

print("--------------------")
print(np.sum(a, axis=0))
print(np.sum(a, axis=1))

print("--------------------")

import numpy as np
data = [[[1,2],[3,4]],[[5,6],[7,8]]] # 3차원 배열 (텐서)
a=np.array(data)
print(a)
print(np.sum(a))
print(a.shape)


print("--------------------")
import numpy as np
array = np.array([[[1, 0, 1],[0, 0, 1]],
[[1, 1, 1],[1, 0, 1]],[[1, 0, 1],[0, 1, 1]]])
print(array.shape)
print(array)

print("--------------------")
print(array.ndim)
print(array.sum(axis = 0))
print(array.sum(axis = 0).shape)
print(array.sum(axis = 1))
print(array.sum(axis = 1).shape)
print(array.sum(axis = 2))
print(array.sum(axis = 2).shape)

print("--------------------")
print(np.arange(5))
print(np.arange(5.0))
print(np.arange(2,8))
print(np.arange(2,10,2))
'''
import numpy as np
X=np.linspace(1.0, 100.0, num=100)
print(X)
#배열에 균일한 간격으로 1부터 100까지 10개의 숫자를 채워줌
print(X.shape)
print("-----------")
X_4_25=np.reshape(X,(4, 25))
print(X_4_25.shape)
print(X_4_25)
X_4_5_5=X_4_25.reshape(4,5,5, order='C')
print(X_4_5_5.shape)
X_new=X.reshape(-1,25, order='C')
print(X_new.shape)
print(X_new)

print("-----------")
array= np.arange(15, 0, -1).reshape(3, 5)
print(array)
print(array.shape)
print(array.ndim)
print(array.sum(axis=0))
print(array.sum(axis=1))

'''
print("-----------")

import numpy as np
# Indexing and Slicing 1D array
array1 = np.arange(10) # subset of 1D array
print(array1)
print(array1[5])
print(array1[0:5])

import numpy as np
# Indexing and Slicing 2D array
array2=np.arange(20).reshape(4, 5) # subset of 2D array
print(array2)
print(array2[1])
print(array2[1:3])
print(array2[0, 4])
print(array2[0:3, 1:3])
print(array2[1])
print(array2[1:3])
print(array2[0, 4])
print(array2[0:3, 1:3])

import numpy as np
# Indexing and Slicing 3D array
array3=np.arange(24).reshape(2, 3, 4) # subset of 3D array
print(array3)
print(array3[1])
print(array3[1, 0])
print(array3[1, 1, 0:3])
print(array3[1, :, 1:3])

print("------------------")

import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.hstack([a, b]))
print(np.hstack([a, b]).shape)
print(np.vstack([a, b]))
print(np.vstack([a, b]).shape)
print(np.stack([a, b], axis=0))
print(np.stack([a, b], axis=0).shape)
print(np.stack([a, b], axis=1))
print(np.stack([a, b], axis=1).shape)
print(np.dstack([a, b]))
print(np.dstack([a, b]).shape)


print(np.linspace(4.0, 6.0, num=5))
print(np.linspace(4.0, 6.0, num=5, endpoint=False))
print(np.linspace(4.0, 6.0, num=5, retstep=True))



print("----------------")
a = np.ones((2,5))
print(a)
a = np.full((2,5), 3)
b = np.array(range(20)).reshape((2,10))
print(np.ndim(b))
print(np.shape(b))
print(np.dtype)



print("----------------")
data = [[1,2,3],[4,5,6],[7,8,9]]
array_3=np.array(data)
print(array_3)
array_C = np.reshape(array_3,(1, -1), order='C')
print(array_C)
array_F = np.reshape(array_3, (1, -1), order='F')
print(array_F)

import numpy as np
array = np.arange(10)
print(array)
array_new = np.reshape(array, (2, -1))
print(array_new)
array_C = np.reshape(array, (2, -1), order='C')
print(array_C)

print("--------------")

data=np.array([1,2,3])
print(data+data)
print(data**2)
print(data*20)
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = a + b
print(c)
c_other=np.add(a,b)
print(c_other)
d = a - b
print(d)
f = a*b
print(f )
g = a/b
print(g)

import numpy as np

# 2차원 배열
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# 전체 합 구하기
sum_val1 = np.sum(a)
print("전체 합:", sum_val1)

# 축별 합 구하기
sum_val2 = np.sum(a, axis=0)
print("열별 합:", sum_val2)

sum_val3 = np.sum(a, axis=1)
print("행별 합:", sum_val3)

# 전체 곱 구하기
prod_val = np.prod(a)
print("전체 곱:", prod_val)

# 1차원 배열
x = np.array([1, 2], float)
y = np.array([3, 4], float)

print("x:", x)
print("y:", y)
print("x와 y의 내적:", np.dot(x, y))
print("x와 y의 내적 (inner):", np.inner(x, y))
print("x와 y의 원소별 곱:", x * y)

# 2차원 배열의 행렬 내적과 내적
print("a와 b의 행렬 내적 (dot):", np.dot(a, b))
print("a와 b의 내적 (inner):", np.inner(a, b))

# 1차원 배열의 외적 및 교차 곱
print("x와 y의 외적 (outer):", np.outer(x, y))

# 2차원 벡터를 3차원 벡터로 확장하여 교차 곱 계산
x_3d = np.array([x[0], x[1], 0])
y_3d = np.array([y[0], y[1], 0])
print("x와 y의 교차 곱 (cross):", np.cross(x_3d, y_3d))

# 2차원 배열을 1차원으로 변환하여 외적 계산
print("a와 b의 외적 (outer):", np.outer(a.ravel(), b.ravel()))

#ㅇㅣ거 교슈님 코드 안 돌아가서 챗쥐피티 돌림 ㅋㅋ아


print("-----------------------")
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 2., 0.1)
plt.figure(figsize=(7,5))
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Drawing graph")
plt.plot(x, x, 'r--', x, x**2, 'bs-.', x, x**3, 'g^--')
plt.show()


import numpy as np
import matplotlib.pyplot as plt
def h(t):
   return np.exp(-t)
def f(t):
   return np.exp(-t) * np.cos(2*np.pi*t)
def g(t):
   return np.sin(np.pi*t)
t1 = np.arange(0.0, 5.0, 0.01)
t2 = np.arange(0.0, 5.0, 0.01)
t3 = np.arange(0.0, 5.0, 0.01)
plt.plot(t1, f(t1), t2, g(t2),'r-', t3,h(t3),'g--')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Drawing graph")
plt.grid()
plt.show()

arr = np.array([('jin', 25, 67), ('suho', 18, 77)],
               dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
print(arr)
print(arr['name'])
print(arr['age'])

print(arr[1])
arr['age'][0] = 20
print(arr)
arr_d = arr.dtype
print(arr_d.names)
print(arr_d.fields)

a = np.array([(1, 2, 3), (4, 5, 6)], dtype='i8, f4, f8')
a[1] = (7, 8, 9)
print(a)

a = np.zeros(2, dtype='i8, f4, ?, S1')
print(a)
a[:] = 7
print(a)
a[:] = np.arange(2)
print(a)

a = np.zeros(3, dtype=[('a', 'i8'), ('b', 'f4'), ('c', 'S3')])
b = np.ones(3, dtype=[('x', 'f4'), ('y', 'S3'), ('z', 'O')])
b[:] = a
print(b.dtype)
print(a.dtype)
a[:] = 1
print(b)
'''
#output
/usr/local/bin/python3.9 /Users/haerangssa/Desktop/advancedpy/week10/week10.py 
[1 2 3]
[1, 2, 3]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1 2 3]
 [4 5 6]]
3
3
--------------------
[[1 3]
 [7 9]]
--------------------
[[1 3]
 [7 9]]
--------------------
(2, 2)
(3, 3)
--------------------
[12 15 18]
[ 6 15 24]
--------------------
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
36
(2, 2, 2)
--------------------
(3, 2, 3)
[[[1 0 1]
  [0 0 1]]

 [[1 1 1]
  [1 0 1]]

 [[1 0 1]
  [0 1 1]]]
--------------------
3
[[3 1 3]
 [1 1 3]]
(2, 3)
[[1 0 2]
 [2 1 2]
 [1 1 2]]
(3, 3)
[[2 1]
 [3 2]
 [2 2]]
(3, 2)
--------------------
[0 1 2 3 4]
[0. 1. 2. 3. 4.]
[2 3 4 5 6 7]
[2 4 6 8]
[  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.  70.
  71.  72.  73.  74.  75.  76.  77.  78.  79.  80.  81.  82.  83.  84.
  85.  86.  87.  88.  89.  90.  91.  92.  93.  94.  95.  96.  97.  98.
  99. 100.]
(100,)
-----------
(4, 25)
[[  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.  70.  71.  72.  73.  74.  75.]
 [ 76.  77.  78.  79.  80.  81.  82.  83.  84.  85.  86.  87.  88.  89.
   90.  91.  92.  93.  94.  95.  96.  97.  98.  99. 100.]]
(4, 5, 5)
(4, 25)
[[  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.  70.  71.  72.  73.  74.  75.]
 [ 76.  77.  78.  79.  80.  81.  82.  83.  84.  85.  86.  87.  88.  89.
   90.  91.  92.  93.  94.  95.  96.  97.  98.  99. 100.]]
-----------
[[15 14 13 12 11]
 [10  9  8  7  6]
 [ 5  4  3  2  1]]
(3, 5)
2
[30 27 24 21 18]
[65 40 15]

Process finished with exit code 0