import numpy as np
np.sin(3)
0.1411200080598672
from numpy import cos
cos(3)
-0.9899924966004454
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
np.pi
3.141592653589793
plt.plot([1,2,3,4,5,6])
[<matplotlib.lines.Line2D at 0x112691b10>]
plt.plot(np.random.randn(100))
[<matplotlib.lines.Line2D at 0x113dc4ad0>]
L=[100.12,200.23,300.45,400.56,500.78]
TWD = 30
for i in L:
print(TWD*i)
3003.6000000000004
6006.9
9013.5
12016.8
15023.4
result =[]
for i in L:
result.append(TWD*i)
result
[3003.6000000000004, 6006.9, 9013.5, 12016.8, 15023.4]
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
USD = [100.12,200.23,300.34,400.45,500.56]
Rate = 30
price = np.array(USD)
price
array([100.12, 200.23, 300.34, 400.45, 500.56])
TWD = price*Rate
TWD
array([ 3003.6, 6006.9, 9010.2, 12013.5, 15016.8])
定義兩個向量$A(x1, y1, z1)$與$B(x2, y2, z2)$的內積運算為: $ AB = (x1, y1, z1) (x2, y2, z2) = x1x2 + y1y2 + z1z2 $
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
grades =np.array([90,80,70])
weight =np.array([0.54,0.29,0.27])
gw = grades*weight
gw
array([48.6, 23.2, 18.9])
gw.sum()
90.7
快速運算dot
np.dot(grades,weight)
90.7
A = np.random.randn(100)
A
array([ 0.21633101, -0.01134162, 0.12971999, -0.38508203, -0.85082543,
-0.66996532, -1.01282311, 0.28391263, -1.08646607, -1.5594304 ,
-1.31724837, 1.27836016, -1.37651606, -1.2068564 , -0.1018424 ,
-0.18165236, 2.14084848, -1.01243429, 0.2549101 , -0.6550546 ,
1.6795229 , 0.84659938, 0.3116358 , 1.0601329 , 1.25613617,
0.1455921 , 1.54468457, -1.77308826, 0.46632822, 0.22631494,
0.36247298, 1.37591752, -0.14180817, -0.73213724, -0.21023857,
1.20817597, 0.19063736, -0.33985522, 1.1856165 , 0.0419306 ,
-0.3344185 , 2.07579031, -1.44048427, -0.25861774, -0.27197172,
0.42475009, -2.08839912, -0.81327181, -1.09120127, -0.91332792,
0.37183663, 0.38667231, -0.57274663, -0.73343432, -0.22737345,
-0.77678973, 0.76387937, 0.98440307, -0.71073374, -1.04552871,
-0.75147211, 0.82540751, -0.52923114, 0.50644201, 0.08139759,
1.89417254, 0.69389315, 0.93863085, -0.62753562, 0.58683208,
0.45189772, 0.20812309, 0.04809831, 1.04700102, 0.68133258,
1.33596765, -0.48539365, -0.45992005, 0.63734106, -0.94633078,
0.31989718, 1.21664125, 1.07954262, -0.4081131 , -0.63309861,
-0.79970931, 1.70041275, 0.97044351, 1.47684148, -0.68811853,
1.90541068, 0.49488636, 0.98260398, 1.19738644, -0.85599576,
0.78446224, -1.31428705, 1.15872042, 0.98784683, -0.10769273])
A = 10*A+100
A
array([102.16331014, 99.88658381, 101.29719995, 96.14917971,
91.49174571, 93.3003468 , 89.87176892, 102.8391263 ,
89.13533934, 84.40569604, 86.82751631, 112.7836016 ,
86.23483941, 87.931436 , 98.98157597, 98.18347639,
121.40848483, 89.8756571 , 102.54910096, 93.449454 ,
116.79522901, 108.46599378, 103.11635795, 110.60132905,
112.56136173, 101.45592101, 115.44684573, 82.26911735,
104.66328223, 102.26314938, 103.62472977, 113.75917525,
98.58191825, 92.67862757, 97.89761435, 112.08175971,
101.90637359, 96.60144783, 111.85616502, 100.41930602,
96.655815 , 120.75790314, 85.59515731, 97.41382257,
97.28028278, 104.24750093, 79.11600878, 91.86728188,
89.08798734, 90.86672078, 103.71836626, 103.86672311,
94.2725337 , 92.66565679, 97.72626554, 92.23210266,
107.63879371, 109.84403074, 92.89266261, 89.54471292,
92.48527894, 108.25407513, 94.70768864, 105.06442012,
100.81397592, 118.94172544, 106.93893155, 109.38630851,
93.72464376, 105.86832076, 104.51897718, 102.08123086,
100.48098315, 110.47001022, 106.81332583, 113.35967649,
95.14606354, 95.40079953, 106.37341062, 90.53669217,
103.19897175, 112.16641249, 110.79542616, 95.91886901,
93.66901389, 92.00290692, 117.00412751, 109.70443509,
114.76841482, 93.11881474, 119.05410676, 104.94886365,
109.82603978, 111.97386443, 91.44004235, 107.84462239,
86.85712953, 111.58720415, 109.87846834, 98.92307274])
A.shape
(100,)
A.shape = (50,2)
A
array([[102.16331014, 99.88658381],
[101.29719995, 96.14917971],
[ 91.49174571, 93.3003468 ],
[ 89.87176892, 102.8391263 ],
[ 89.13533934, 84.40569604],
[ 86.82751631, 112.7836016 ],
[ 86.23483941, 87.931436 ],
[ 98.98157597, 98.18347639],
[121.40848483, 89.8756571 ],
[102.54910096, 93.449454 ],
[116.79522901, 108.46599378],
[103.11635795, 110.60132905],
[112.56136173, 101.45592101],
[115.44684573, 82.26911735],
[104.66328223, 102.26314938],
[103.62472977, 113.75917525],
[ 98.58191825, 92.67862757],
[ 97.89761435, 112.08175971],
[101.90637359, 96.60144783],
[111.85616502, 100.41930602],
[ 96.655815 , 120.75790314],
[ 85.59515731, 97.41382257],
[ 97.28028278, 104.24750093],
[ 79.11600878, 91.86728188],
[ 89.08798734, 90.86672078],
[103.71836626, 103.86672311],
[ 94.2725337 , 92.66565679],
[ 97.72626554, 92.23210266],
[107.63879371, 109.84403074],
[ 92.89266261, 89.54471292],
[ 92.48527894, 108.25407513],
[ 94.70768864, 105.06442012],
[100.81397592, 118.94172544],
[106.93893155, 109.38630851],
[ 93.72464376, 105.86832076],
[104.51897718, 102.08123086],
[100.48098315, 110.47001022],
[106.81332583, 113.35967649],
[ 95.14606354, 95.40079953],
[106.37341062, 90.53669217],
[103.19897175, 112.16641249],
[110.79542616, 95.91886901],
[ 93.66901389, 92.00290692],
[117.00412751, 109.70443509],
[114.76841482, 93.11881474],
[119.05410676, 104.94886365],
[109.82603978, 111.97386443],
[ 91.44004235, 107.84462239],
[ 86.85712953, 111.58720415],
[109.87846834, 98.92307274]])
A = A.reshape(50,2)
A
array([[102.16331014, 99.88658381],
[101.29719995, 96.14917971],
[ 91.49174571, 93.3003468 ],
[ 89.87176892, 102.8391263 ],
[ 89.13533934, 84.40569604],
[ 86.82751631, 112.7836016 ],
[ 86.23483941, 87.931436 ],
[ 98.98157597, 98.18347639],
[121.40848483, 89.8756571 ],
[102.54910096, 93.449454 ],
[116.79522901, 108.46599378],
[103.11635795, 110.60132905],
[112.56136173, 101.45592101],
[115.44684573, 82.26911735],
[104.66328223, 102.26314938],
[103.62472977, 113.75917525],
[ 98.58191825, 92.67862757],
[ 97.89761435, 112.08175971],
[101.90637359, 96.60144783],
[111.85616502, 100.41930602],
[ 96.655815 , 120.75790314],
[ 85.59515731, 97.41382257],
[ 97.28028278, 104.24750093],
[ 79.11600878, 91.86728188],
[ 89.08798734, 90.86672078],
[103.71836626, 103.86672311],
[ 94.2725337 , 92.66565679],
[ 97.72626554, 92.23210266],
[107.63879371, 109.84403074],
[ 92.89266261, 89.54471292],
[ 92.48527894, 108.25407513],
[ 94.70768864, 105.06442012],
[100.81397592, 118.94172544],
[106.93893155, 109.38630851],
[ 93.72464376, 105.86832076],
[104.51897718, 102.08123086],
[100.48098315, 110.47001022],
[106.81332583, 113.35967649],
[ 95.14606354, 95.40079953],
[106.37341062, 90.53669217],
[103.19897175, 112.16641249],
[110.79542616, 95.91886901],
[ 93.66901389, 92.00290692],
[117.00412751, 109.70443509],
[114.76841482, 93.11881474],
[119.05410676, 104.94886365],
[109.82603978, 111.97386443],
[ 91.44004235, 107.84462239],
[ 86.85712953, 111.58720415],
[109.87846834, 98.92307274]])
xy = [[x,y] for x in range(3) for y in range(3)]
xy
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones((3,2))
array([[1., 1.],
[1., 1.],
[1., 1.]])
np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
y = np.random.rand(50)
y
array([0.76611942, 0.21542947, 0.14257835, 0.85567334, 0.60962157,
0.74585904, 0.55901273, 0.51219678, 0.34876318, 0.71764435,
0.57119776, 0.29450495, 0.46986025, 0.33005093, 0.00188089,
0.95188522, 0.76643439, 0.85561159, 0.17058909, 0.29649429,
0.1850956 , 0.43674388, 0.09736271, 0.95428783, 0.17986021,
0.65175198, 0.15201405, 0.48544736, 0.67376696, 0.90953095,
0.20936296, 0.51079728, 0.81469851, 0.22469013, 0.70101652,
0.3302545 , 0.12395894, 0.46918266, 0.76759166, 0.75087635,
0.39531747, 0.47697309, 0.10670272, 0.13818557, 0.56077703,
0.0985195 , 0.2010733 , 0.31520234, 0.61064279, 0.25949173])
plt.plot(y)
[<matplotlib.lines.Line2D at 0x10d45aa90>]
np.linspace(起始值,結束值,多少點)
x = np.linspace(1,10,50)
plt.plot(x,np.sin(x)+x)
plt.plot(x,np.cos(x)+x)
plt.plot(x,np.sin(x)*np.cos(x)+x)
[<matplotlib.lines.Line2D at 0x110b42ad0>]
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
L= np.array([1,10,20,30,40,50,60])
L <20
array([ True, True, False, False, False, False, False])
L[L<20]
array([ 1, 10])
x = np.linspace(-5,5,100)
y = np.sinc(x)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11283a150>]
plt.plot(x,y)
plt.plot(x[x>0],y[x>0],"o")
[<matplotlib.lines.Line2D at 0x11251db10>]