100% found this document useful (1 vote)
79 views

Numpy Test: Import As

This document contains examples demonstrating various operations in NumPy including importing NumPy, creating arrays, finding maximum values, axis operations, slicing, reshaping, and basic array manipulation. NumPy is used to create arrays of data and perform common mathematical operations on that data such as finding maximum values, adding and slicing arrays.

Uploaded by

Haing p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
79 views

Numpy Test: Import As

This document contains examples demonstrating various operations in NumPy including importing NumPy, creating arrays, finding maximum values, axis operations, slicing, reshaping, and basic array manipulation. NumPy is used to create arrays of data and perform common mathematical operations on that data such as finding maximum values, adding and slicing arrays.

Uploaded by

Haing p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Numpy Test

March 22, 2021

In [1]: import numpy as np

In [2]: CURVE_CENTER = 80
grades = np.array([72, 35, 64, 88, 51, 90, 74, 12])

In [3]: def curve(grades):


average = grades.mean()
change = CURVE_CENTER - average
new_grades = grades + change
return np.clip(new_grades, grades, 100)

In [4]: curve(grades)

Out[4]: array([ 91.25, 54.25, 83.25, 100. , 70.25, 100. , 93.25, 31.25])

In [6]: temperature = np.array([29.3, 42.1, 18.8, 16.1, 38.0, 12.5, 12.6, 49.9, 38.6, 31.3, 9.2

In [7]: temperature

Out[7]: array([[[29.3, 42.1, 18.8],


[16.1, 38. , 12.5]],

[[12.6, 49.9, 38.6],


[31.3, 9.2, 22.2]]])

In [14]: temperature.max()

Out[14]: 49.9

In [8]: np.swapaxes(temperature, 1, 2)

Out[8]: array([[[29.3, 16.1],


[42.1, 38. ],
[18.8, 12.5]],

[[12.6, 31.3],
[49.9, 9.2],
[38.6, 22.2]]])

1
In [9]: table = np.array([
[5, 3, 7, 1],
[2, 6, 7 ,9],
[1, 1, 1, 1],
[4, 3, 2, 0],
])

In [10]: table.max()

Out[10]: 9

In [11]: table.max(axis = 0)

Out[11]: array([5, 6, 7, 9])

In [12]: table.max(axis = 1)

Out[12]: array([7, 9, 1, 4])

In [ ]:

In [18]: A = np.arange(32).reshape(4, 1, 8)

In [19]: A

Out[19]: array([[[ 0, 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]]])

In [20]: B = np.arange(48).reshape(1, 6, 8)

In [21]: B

Out[21]: array([[[ 0, 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]]])

In [24]: A + B

Out[24]: array([[[ 0, 2, 4, 6, 8, 10, 12, 14],


[ 8, 10, 12, 14, 16, 18, 20, 22],
[16, 18, 20, 22, 24, 26, 28, 30],
[24, 26, 28, 30, 32, 34, 36, 38],

2
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54]],

[[ 8, 10, 12, 14, 16, 18, 20, 22],


[16, 18, 20, 22, 24, 26, 28, 30],
[24, 26, 28, 30, 32, 34, 36, 38],
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62]],

[[16, 18, 20, 22, 24, 26, 28, 30],


[24, 26, 28, 30, 32, 34, 36, 38],
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62],
[56, 58, 60, 62, 64, 66, 68, 70]],

[[24, 26, 28, 30, 32, 34, 36, 38],


[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62],
[56, 58, 60, 62, 64, 66, 68, 70],
[64, 66, 68, 70, 72, 74, 76, 78]]])

In [25]: a = np.zeros((2,2))

In [29]: b = np.ones((2,2))

In [31]: c = np.full((2,2), 7)

In [33]: d = np.eye(5)

In [86]: a = np.array([[1,2], [3, 4], [5, 6]])

In [36]: a

Out[36]: array([[1, 2],


[3, 4],
[5, 6]])

In [78]: b = a[[0,1], [0]]


b

Out[78]: array([1, 3])

In [75]: c = a[:2, [0]]


c

Out[75]: array([[1],
[3]])

3
In [84]: d = a[:2, 0:1]
d

Out[84]: array([[1],
[3]])

In [80]: b[1] = 4
a

Out[80]: array([[1, 2],


[3, 4],
[5, 6]])

In [83]: c[1, 0] = 4
a

Out[83]: array([[1, 2],


[3, 4],
[5, 6]])

In [96]: d[1, 0] = 4
a

Out[96]: array([[100, 2],


[ 3, 4],
[ 5, 6]])

In [109]: a = np.array([[1,2], [3, 4], [5, 6]])


a

Out[109]: array([[1, 2],


[3, 4],
[5, 6]])

In [110]: b = a[(1, 2), :]


b

Out[110]: array([[3, 4],


[5, 6]])

In [111]: b[0][0] = 100


a

Out[111]: array([[1, 2],


[3, 4],
[5, 6]])

In [124]: y = np.arange(35).reshape(5,7)
y

4
Out[124]: array([[ 0, 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]])

In [133]: x = y[np.array([0]), 1:3]


x.shape

Out[133]: (1, 2)

In [132]: x[0, 1] = 100


y

Out[132]: array([[ 0, 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]])

You might also like