달력

52024  이전 다음

  • 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

다차원 배열의 자료형

NumPY의 다차원배열의 원소는 동일한 데이터 타입을 가져야 한다. 다양한 자료형을 하나의 배열에 표현하고 싶을 경우 Pandas의 데이타프레임을 이용하면 된다.

 

직접 값을 입력해 다차원 배열을 만드는 방법

  • np.array() 메서드를 이용

 

bool형 ndarray 만들기

bool형 ndarray 생성

 

결과

 

https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html?highlight=array#numpy.array

 

numpy.array — NumPy v1.16 Manual

Parameters: object : array_like An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence. dtype : data-type, optional The desired data-type for the array. If not given, then the type wil

docs.scipy.org

https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html?highlight=dtype#numpy.dtype

 

numpy.dtype — NumPy v1.16 Manual

Attributes: alignment The required alignment (bytes) of this data-type according to the compiler. More information is available in the C-API section of the manual. base byteorder A character indicating the byte-order of this data-type object. One of: ‘=’ n

docs.scipy.org

 

 

정수형 ndarray 만들기

정수형 다차원배열은 정수형, 부호없는 정수형, 실수형, 복소수형이 있다. 정수형의 default data type는 'int64' 이다.

  • 정수형

 

  • 부호없는 정수형

 

  • 실수형

 

  • 복소수형

 

  • python list에서

 

  • 3차원 ndarray 만들기

 

 

파일에서 데이터를 입력 받아 다차원 배열 생성하기

np.genfromtxt()을 이용하여 파일에 저장된 데이터를 입력받아 다차원 배열을 생성할 수 있다.

  • ndarray는 동일한 자료형만 가질 수 있다.( C의 배열이기 때문에 )
  • 파일에는 정수, 실수, 문자열이 섞여 있기 때문에 사용상 어려움이 있다.
  • 파일에서 데이터를 읽어올 때는 genfromtxt보다는 Pandas의 read_csv(), read_excel()을 주로 사용한다.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

 

numpy.genfromtxt — NumPy v1.16 Manual

Parameters: fname : file, str, pathlib.Path, list of str, generator File, filename, list, or generator to read. If the filename extension is gz or bz2, the file is first decompressed. Note that generators must return byte strings in Python 3k. The strings

docs.scipy.org

 

 

 

NumPY의 메서드를 이용해 다차원 배열을 만드는 방법

NumPY의 array 함수를 이용해서 다차원 배열을 만들었다. 이제 array가 아닌 다른 메서드를 이용해서  다차원 배열을 만드는 방법을 알아보자

 

https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html

 

Array creation routines — NumPy v1.16 Manual

Note numpy.char is the preferred alias for numpy.core.defchararray.

docs.scipy.org

 

다차원 Array를 만드는 다른 방법들.

 

 

 

 

 

 

 

 

 

10 * np.ones((2,2))

 

 

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html#numpy.linspace

 

numpy.linspace — NumPy v1.16 Manual

Parameters: start : array_like The starting value of the sequence. stop : array_like The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is e

docs.scipy.org

 

Posted by 생짜
|