reshapeの使い方 (numpy)



Python
Pythonとは
基本的な使い方
IDLE

Jupyter Notebook

Markdown

コマンドプロンプトで実行

仮想環境の構築

仮想環境でIDLEを実行

ライブラリのインストール
pipの使い方

numpy , matplotlib等

graphviz

pytorch

Mecab

Pythonの関数:一覧
共通関数
append , extend

class

copy

csv.reader

csv.writer

def , return

dict , defaultdict

enumerate

exit

for

if

import

in

input

lambda

len

list

min/max

OrderedDict

open/close

os

pickle

print

range

re.split

read/readline

round/floor/ceil

split

sys.argv

time

while

write

zip

・特殊メソッド
 ・__name__

 ・__iter__ , __next__

正規表現、メタ文字

データの型の種類

四則演算 (+ , - , * , /)

コメントアウト (# , ''')

numpy
append

arange

argmax/argmin

array

asfarray

astype , dtype

digitize

dot

hstack/vstack

linalg.solve

linspace

max,min

mean

meshgrid

mgrid

ndim

ndmin

pad

poly1d

polyfit

prod

random

reshape

savetxt/loadtxt

shape

std

transpose

where

zeros/zeros_like

scipy
expit

imread

interpolate

signal.square, sawtooth

matplotlib
imshow

figure

pcolormesh

plot

quiver

scatter

scikit-learn
GaussianNB

KMeans

KNeighborsClassifier

SVC

tree

chaospy

keras

chainer

chainerrl

pytorch
BCELoss , MSELoss

Embedding

device

Sequential

Dataset, Dataloader

RNN, LSTM

OpenAI gym
Blackjack-v0

CartPole-v0

tkinter
frame, grid

画像表示

画像を切り取り表示

画像を保存

目的別
ステップ関数

矩形波, 三角波

1 of K 符号化法

線形補間

配列に番号をつける

ベクトル場を描く

線形回帰, 多項式回帰


公開日:2018/7/15 , 最終更新日:2019/1/20         

■説明
配列の大きさと形状を変換する。

■具体例
以下はnumpyを使用する際に必須の記載です。

>> import numpy as np


以下は各例で使う共通の記述になります。

>> a=np.array([1,2,3,4,5,6,7,8,9,10,11,12])


例①

>> a.reshape(2,6)

  array([[ 1, 2, 3, 4, 5, 6],
     [ 7, 8, 9, 10, 11, 12]])


例②
以下の様にすると、1次元の様に見えますが2次元です。カッコの数が違います。

>> a.reshape(1,12)

  array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])


例③

>>a.reshape(2,2,3) #2つ作る、2行3列を

 array([[[ 1, 2, 3],
     [ 4, 5, 6]],

     [[ 7, 8, 9],
      [10, 11, 12]]])


例④

>> a.reshape(2,2,3,1) #4次元にする

  array([[[[ 1],
      [ 2],
      [ 3]],

      [[ 4],
      [ 5],
      [ 6]]],


     [[[ 7],
      [ 8],
      [ 9]],

     [[10],
      [11],
      [12]]]])


例⑤
多次元化した配列をを再び1次元に戻すときは以下の様にします。

>> a.reshape(-1)

   array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


例⑥
-1を使うと任意の配列に整形されます。

>> a.reshape(4,-1) #4行にする。

   array([[ 1, 2, 3],
      [ 4, 5, 6],
      [ 7, 8, 9],
      [10, 11, 12]])

>> a.reshape(4,-1).shape

   (4, 3) #配列は当然ながら4x3


例⑦
配列をnumpyのarrayで定義していない場合、以下の様に記載することも可能です。

>> a=[1,2,3,4,5,6,7,8,9,10,11,12]
>> a=np.reshape(a,(2, 2, 3))
>> a

 array([[[ 1, 2, 3],
     [ 4, 5, 6]],

     [[ 7, 8, 9],
      [10, 11, 12]]])










サブチャンネルあります。⇒ 何かのお役に立てればと

関連記事一覧



Python
Pythonとは
基本的な使い方
IDLE

Jupyter Notebook

Markdown

コマンドプロンプトで実行

仮想環境の構築

仮想環境でIDLEを実行

ライブラリのインストール
pipの使い方

numpy , matplotlib等

graphviz

pytorch

Mecab

Pythonの関数:一覧
共通関数
append , extend

class

copy

csv.reader

csv.writer

def , return

dict , defaultdict

enumerate

exit

for

if

import

in

input

lambda

len

list

min/max

OrderedDict

open/close

os

pickle

print

range

re.split

read/readline

round/floor/ceil

split

sys.argv

time

while

write

zip

・特殊メソッド
 ・__name__

 ・__iter__ , __next__

正規表現、メタ文字

データの型の種類

四則演算 (+ , - , * , /)

コメントアウト (# , ''')

numpy
append

arange

argmax/argmin

array

asfarray

astype , dtype

digitize

dot

hstack/vstack

linalg.solve

linspace

max,min

mean

meshgrid

mgrid

ndim

ndmin

pad

poly1d

polyfit

prod

random

reshape

savetxt/loadtxt

shape

std

transpose

where

zeros/zeros_like

scipy
expit

imread

interpolate

signal.square, sawtooth

matplotlib
imshow

figure

pcolormesh

plot

quiver

scatter

scikit-learn
GaussianNB

KMeans

KNeighborsClassifier

SVC

tree

chaospy

keras

chainer

chainerrl

pytorch
BCELoss , MSELoss

Embedding

device

Sequential

Dataset, Dataloader

RNN, LSTM

OpenAI gym
Blackjack-v0

CartPole-v0

tkinter
frame, grid

画像表示

画像を切り取り表示

画像を保存

目的別
ステップ関数

矩形波, 三角波

1 of K 符号化法

線形補間

配列に番号をつける

ベクトル場を描く

線形回帰, 多項式回帰