Numpyデータ分析入門(2)-データ型、再定義
Numpyのデータ型
Numpyには、さまざまな値を格納して表す複数のデータ型が用意されています.
Pythonが提供するデータ型と互換性があり、代表的なデータ型は以下のとおりです.
np.int 64:(64ビット)整数
np.float 64:(64ビット)実数
np.複数
np.ブール:ブール値(True,False)ブール値
np.object:Pythonオブジェクト
np.str:文字列は、 endarrayに格納されたデータを所定のdtypeに変換することによってデータを返す. Parameters
dtype:変更する配列のデータ型 出力値のタイプと演算を変換し、実際の値を変換しません. <実習コード>
Ndarray形状の変更はしばしば発生する.
shapeを変更する様々な例を表示し、一般的なreforme関数を理解します.
例1)深度学習に画像データを適用する場合,2次元を1次元に展開する必要がある.
例2)マシンラーニングアルゴリズムを適用するには、特定の独立変数列を選択して、1次元を2次元に変更することもできます.(10,)->(10,1)は、既存のデータを保持しながら次元と形状のみを変更する を返します. Parameter
Array:再構築するカレンダー
Newshape:形状値 、変更用コール方式(結果は同じ)
np.reshape(array, newshape)
array.reshape(newshape) 再構成アレイに格納されているすべてのデータが失われないように、再構成前後のアレイサイズは同じである必要があります.
形状を変更するとともに、既存データ を保持する.形状は、所与のN自動推定により に変更する.-1の位置は可変 である.
<実習コード>
Numpyには、さまざまな値を格納して表す複数のデータ型が用意されています.
Pythonが提供するデータ型と互換性があり、代表的なデータ型は以下のとおりです.
np.int 64:(64ビット)整数
np.float 64:(64ビット)実数
np.複数
np.ブール:ブール値(True,False)ブール値
np.object:Pythonオブジェクト
np.str:文字列
ndarray.astype(dtype)
関数dtype:変更する配列のデータ型
import numpy as np
'''
dtype 인자 지정을 통한 데이터 타입 변경
'''
data = [1, 2, 3]
cpx_arr = np.array(data, dtype=np.complex)
str_arr = np.array(data, dtype=np.str)
print(cpx_arr)
print(str_arr)
'''
샘플 데이터 생성
'''
origin = np.arange(1, 2, 0.2)
print('\nOriginal data')
print(origin)
print('dtype : ', origin.dtype)
'''
astype(dtype) 을 활용한 데이터 타입 변경
'''
result = origin.astype(int)
print('\nResult of astype(int)')
print(result)
print('\nOriginal data')
print(origin)
<実行結果>[1.+0.j 2.+0.j 3.+0.j]
['1' '2' '3']
Original data
[1. 1.2 1.4 1.6 1.8]
dtype : float64
Result of astype(int)
[1 1 1 1 1]
Original data
[1. 1.2 1.4 1.6 1.8]
Ndarrayの再成形Ndarray形状の変更はしばしば発生する.
shapeを変更する様々な例を表示し、一般的なreforme関数を理解します.
例1)深度学習に画像データを適用する場合,2次元を1次元に展開する必要がある.
例2)マシンラーニングアルゴリズムを適用するには、特定の独立変数列を選択して、1次元を2次元に変更することもできます.(10,)->(10,1)
np.reshape(array, newshape)
Array:再構築するカレンダー
Newshape:形状値
np.reshape(array, newshape)
array.reshape(newshape)
reshape(-1, N)
<実習コード>
import numpy as np
# ndarray 속성 출력 함수
def printinfo(arr):
print(arr)
print("shape: {}, size: {}, dtype: {}, dimension: {}"
.format(arr.shape, arr.size, arr.dtype, arr.ndim))
# 샘플 데이터 생성
origin_arr = np.arange(15)
print('\nOriginal ndarray')
printinfo(origin_arr)
'''
reshape() 을 활용한 데이터 형상 변경
'''
reshape_arr = np.reshape(origin_arr, (3,5))
print('\nReshape ndarray')
printinfo(reshape_arr)
'''
origin_arr에 저장된 1차원 배열을 -1을 활용하여 2차원으로 형상 변경
'''
reshape_arr = reshape_arr.reshape(-1,1)
print('\nReshape(-1, N) to 2D array')
printinfo(reshape_arr)
<実行結果>Original ndarray
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
shape: (15,), size: 15, dtype: int64, dimension: 1
Reshape ndarray
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
shape: (3, 5), size: 15, dtype: int64, dimension: 2
Reshape(-1, N) to 2D array
[[ 0]
[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]]
shape: (15, 1), size: 15, dtype: int64, dimension: 2
Reference
この問題について(Numpyデータ分析入門(2)-データ型、再定義), 我々は、より多くの情報をここで見つけました https://velog.io/@supremo7/NumPy-활용한-데이터-분석-입문2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol