Vous êtes sur la page 1sur 7

Numpy Class – Chapter # 4

1- from numpy import empty


a = empty([3,3])
print(a)
Create empty truple

output
[[0.00000000e+000 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 6.73905541e-321]
[8.90060779e-307 0.00000000e+000 0.00000000e+000]]

2- print(a.shape)
print(a.dtype)

output
(3, 3)
float64

3- # create zero array


from numpy import zeros
a = zeros([3,5])
print(a)

create empty truple

ouput
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]

float64

4- a.dtype=int

For integer output

5- # create one array


from numpy import ones
a = ones([5])
print(a)

create one row with 1

output
[1. 1. 1. 1. 1.]
6- 9 row 10 column, data type= int, value =125
a=np.zeros(9,5, dtype=int) +125
a

7 - # create array with vstack


from numpy import array
from numpy import vstack
# create first array
a1 = array([1,2,3])
print(a1)
# create second array
a2 = array([4,5,6])
print(a2)
# vertical stack
a3 = vstack((a1, a2))
print(a3)
print(a3.shape)

output

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

Combine two array vertically

8 - # create array with hstack


from numpy import array
from numpy import hstack
# create first array
a1 = array([1,2,3])
print(a1)
# create second array
a2 = array([4,5,6])
print(a2)
# create horizontal stack
a3 = hstack((a1, a2))
print(a3)
print(a3.shape)

output
[1 2 3]
[4 5 6]
[1 2 3 4 5 6]
(6,)

Combine two array Horizontal


Chapter # 5

Index, Slice and Reshape NumPy Arrays


Index must start with 0

1- # create one-dimensional array


from numpy import array
# list of data
data = [11, 22, 33, 44, 55]
# array of data
data = array(data)
print(data)
print(type(data))

Output
[11 22 33 44 55]
<class 'numpy.ndarray'>

5.2.2 Two-Dimensional List of Lists to Array


# create two-dimensional array
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data)
print(type(data))

[[11 22]
[33 44]
[55 66]]
<class 'numpy.ndarray'>

5.3.1 One-Dimensional Indexing


# index a one-dimensional array
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
# index data
print(data[0])
print(data[4])

output
11
55

5.3.2 Two-Dimensional Indexing


# index two-dimensional array
from numpy import array
# define array
data = array([
[11, 22],
[33, 44],
[55, 66]])
# index data
print(data[0,0])

11

# index row of two-dimensional array


from numpy import array
# define array
data = array([
[11, 22],
[33, 44],
[55, 66]])
# index data
print(data[0,])

[11 22]

5.4.1 One-Dimensional Slicing


# slice a one-dimensional array
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[:])

[11 22 33 44 55]

# slice a subset of a one-dimensional array


from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[0:1])

11

# negative slicing of a one-dimensional array


from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[-2:])

[44 55]

# negative slicing of a one-dimensional array


from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[-2::-1])
[44 33 22 11]

Reverse stack counting

# split input and output data


from numpy import array
# define array
data = array([
[11, 22, 33],
[44, 55, 66],
[77, 88, 99]])
# separate data
X, y = data[:, :-1], data[:, -1]
print(X)
print(y)

[[11 22]
[44 55]
[77 88]]
[33 66 99]

Split Train and Test Rows


# split train and test data
from numpy import array
# define array
data = array([
[11, 22, 33],
[44, 55, 66],
[77, 88, 99]])
# separate data
split = 2
train,test = data[:split,:],data[split:,:]
print(train)
print(test)

[[11 22 33]
[44 55 66]]
[[77 88 99]]

[33, 44],
[55, 66]]
# array of data
data = array(data)
print('Rows: %d' % data.shape[0])
print('Cols: %d' % data.shape[1])

Rows: 3
Cols: 2

# reshape 1D array to 2D
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data.shape)
# reshape
data = data.reshape((data.shape[0], 1))
print(data.shape)

(5,)
(5, 1)

array([[11],
[22],
[33],
[44],
[55]])

Transfer column to rows

# reshape 2D array to 3D
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data.shape)
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data.shape)

array([[[11],
[22]],

[[33],
[44]],

[[55],
[66]]])

import numpy as np
a=np.arange(75).reshape((3,5,5))
a[1, 0:, 1:-1]
z, x, y

start:stop:step

Home work
all column slice one row and one row miss, and print descending row
array
Start, end, step
Home work
Chase Game
1-64