Flower in my dev/Python

<PYTHON>[Numpy_01 배열 및 자료형]

꽃선생 2017. 11. 13. 20:17

[Numpy_01 배열 및 자료형]

 

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
import numpy as np
 
= [1,2,3,4,5]
arr1 = np.array(a) # 배열 생성
print(arr1)
 
= [[1,2,3,4,5],[6,7,8,9,0]]
arr2 = np.array(b) # 2차원 배열 생성
print(arr2)
 
print(arr1.ndim)
print(arr2.ndim)
 
print(arr1.shape)
print(arr2.shape)
 
 
= [1.1,2.2,3.3]
arr3 = np.array(c) # 부동소수점 배열 생성
print(arr3)
 
print(arr1.dtype)
print(arr3.dtype)
 
arr4 = np.zeros(5) # 0으로 채워진 배열 생성
arr5 = np.ones(5)  # 1으로 채워진 배열 생성
 
print(arr4)
print(arr5)
 
arr6 = np.zeros((2,3))
arr7 = np.zeros((2,3,4))
print(arr6)
print(arr7)
 
arr8 = np.arange(10) # 순차값 배열 생성
print(arr8)

 

# 타입을 지정하여 배열 생성 

arr1 = np.array([1,2,3], dtype=np.int32)
arr2 = np.array([1,2,3], dtype=np.float64)
 
print(arr1.dtype)
print(arr2.dtype)

 

# 대상배열의 타입으로 타입 변경 

arr3 = arr2.astype(np.int64)
print(arr3.dtype)
 
arr4 = np.arange(10)
arr5 = np.array([.11, .22], dtype=np.float64)
 
print(arr4.dtype)
print(arr5.dtype)
 
arr4.astype(arr5.dtype)
print(arr4.dtype)
cs

 

- 테스트