import numpy as np
A = np.linspace(start=0, stop=np.pi, num=10)
B = np.empty(10)
np.sin(A, out=B)
array([0.00000000e+00, 3.42020143e-01, 6.42787610e-01, 8.66025404e-01, 9.84807753e-01, 9.84807753e-01, 8.66025404e-01, 6.42787610e-01, 3.42020143e-01, 1.22464680e-16])
B
array([0.00000000e+00, 3.42020143e-01, 6.42787610e-01, 8.66025404e-01, 9.84807753e-01, 9.84807753e-01, 8.66025404e-01, 6.42787610e-01, 3.42020143e-01, 1.22464680e-16])
import numpy as np
A = np.linspace(0, np.pi, 10)
B = np.sin(A)
B
array([0.00000000e+00, 3.42020143e-01, 6.42787610e-01, 8.66025404e-01, 9.84807753e-01, 9.84807753e-01, 8.66025404e-01, 6.42787610e-01, 3.42020143e-01, 1.22464680e-16])
import numpy as np
np.info(np.sin)
sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) Trigonometric sine, element-wise. Parameters ---------- x : array_like Angle, in radians (:math:`2 \pi` rad equals 360 degrees). out : ndarray, None, or tuple of ndarray and None, optional A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs. where : array_like, optional This condition is broadcast over the input. At locations where the condition is True, the `out` array will be set to the ufunc result. Elsewhere, the `out` array will retain its original value. Note that if an uninitialized `out` array is created via the default ``out=None``, locations within it where the condition is False will remain uninitialized. **kwargs For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`. Returns ------- y : array_like The sine of each element of x. This is a scalar if `x` is a scalar. See Also -------- arcsin, sinh, cos Notes ----- The sine is one of the fundamental functions of trigonometry (the mathematical study of triangles). Consider a circle of radius 1 centered on the origin. A ray comes in from the :math:`+x` axis, makes an angle at the origin (measured counter-clockwise from that axis), and departs from the origin. The :math:`y` coordinate of the outgoing ray's intersection with the unit circle is the sine of that angle. It ranges from -1 for :math:`x=3\pi / 2` to +1 for :math:`\pi / 2.` The function has zeroes where the angle is a multiple of :math:`\pi`. Sines of angles between :math:`\pi` and :math:`2\pi` are negative. The numerous properties of the sine and related functions are included in any standard trigonometry text. Examples -------- Print sine of one angle: >>> np.sin(np.pi/2.) 1.0 Print sines of an array of angles given in degrees: >>> np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. ) array([ 0. , 0.5 , 0.70710678, 0.8660254 , 1. ]) Plot the sine function: >>> import matplotlib.pylab as plt >>> x = np.linspace(-np.pi, np.pi, 201) >>> plt.plot(x, np.sin(x)) >>> plt.xlabel('Angle [rad]') >>> plt.ylabel('sin(x)') >>> plt.axis('tight') >>> plt.show()
import numpy as np
A = np.arange(6)
print(A)
print(np.power(A, 2))
[0 1 2 3 4 5] [ 0 1 4 9 16 25]
A = np.linspace(0, np.pi, 10)
B = np.sin(A, dtype='float16')
print(B)
[ 0.000 0.342 0.643 0.866 0.985 0.985 0.866 0.643 0.342 0.001]
B = np.sin(A)
print(' '.join(format(x, '5.3f') for x in B))
0.000 0.342 0.643 0.866 0.985 0.985 0.866 0.643 0.342 0.000
B = np.sin(A)
C = (format(x, '5.3f') for x in B)
print(list(C))
['0.000', '0.342', '0.643', '0.866', '0.985', '0.985', '0.866', '0.643', '0.342', '0.000']
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
A = np.linspace(0, np.pi, 10)
B = np.sin(A, dtype='float16')
print(B)
[ 0.000 0.342 0.643 0.866 0.985 0.985 0.866 0.643 0.342 0.001]
pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
A = np.linspace(0, 2 * np.pi)
print(len(A))
50
A = np.linspace(0, 2 * np.pi)
plt.plot(A, np.sin(A))
plt.show()
A = np.linspace(0, 2 * np.pi)
plt.plot(A, np.cos(A))
plt.show()
A = np.linspace(0.1, 10)
print(len(A))
A
50
array([ 0.100, 0.302, 0.504, 0.706, 0.908, 1.110, 1.312, 1.514, 1.716, 1.918, 2.120, 2.322, 2.524, 2.727, 2.929, 3.131, 3.333, 3.535, 3.737, 3.939, 4.141, 4.343, 4.545, 4.747, 4.949, 5.151, 5.353, 5.555, 5.757, 5.959, 6.161, 6.363, 6.565, 6.767, 6.969, 7.171, 7.373, 7.576, 7.778, 7.980, 8.182, 8.384, 8.586, 8.788, 8.990, 9.192, 9.394, 9.596, 9.798, 10.000])
plt.plot(A, 1/A)
plt.show()
plt.plot([0, 1, 2, 3, 4], [1, 2, 4, 5, 3])
[<matplotlib.lines.Line2D at 0x7fe1107c77f0>]
plt.plot([1, 2, 4, 5, 3])
[<matplotlib.lines.Line2D at 0x7fe0e03e3e80>]
plt.plot([3, 4, 1, 5, 2, 3], [4, 1, 3, 3, 1, 4])
[<matplotlib.lines.Line2D at 0x7fe0f081cac8>]
A = np.linspace(-15, 20)
plt.plot(A, A ** 3 - (15 * A ** 2) + 25)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
A = np.linspace(0, 2 * np.pi)
plt.plot(A, np.sin(A))
plt.plot(A, np.cos(A))
plt.show()
plt.plot(A, np.sin(A), A, np.cos(A))
[<matplotlib.lines.Line2D at 0x7fa615ba1340>, <matplotlib.lines.Line2D at 0x7fa615ba1370>]
plt.plot(A, np.sin(A), A, np.cos(A), 'o')
plt.show()
plt.plot(A, np.sin(A))
plt.plot(A, np.cos(A), 'o')
plt.show()
plt.plot(A, np.sin(A), '^', A, np.cos(A), 'o')
plt.title('Sine and Cosine')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
plt.plot(A, np.sin(A), 'r^', A, np.cos(A), 'ko')
plt.title('Sine and Cosine')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
A = np.linspace(1, 60, 60)
A # 년단위 기간으로 가정
array([ 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., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60.])
2 * A # 매년 2달러 증가 공식 ==> 신규 배열 생성
array([ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98., 100., 102., 104., 106., 108., 110., 112., 114., 116., 118., 120.])
1.1 ** A # 복리계산법: 매년 10% 증가 공식
array([ 1.1 , 1.21 , 1.331 , 1.4641 , 1.61051 , 1.771561 , 1.9487171 , 2.14358881, 2.35794769, 2.59374246, 2.85311671, 3.13842838, 3.45227121, 3.79749834, 4.17724817, 4.59497299, 5.05447028, 5.55991731, 6.11590904, 6.72749995, 7.40024994, 8.14027494, 8.95430243, 9.84973268, 10.83470594, 11.91817654, 13.10999419, 14.42099361, 15.86309297, 17.44940227, 19.1943425 , 21.11377675, 23.22515442, 25.54766986, 28.10243685, 30.91268053, 34.00394859, 37.40434344, 41.14477779, 45.25925557, 49.78518112, 54.76369924, 60.24006916, 66.26407608, 72.89048369, 80.17953205, 88.19748526, 97.01723378, 106.71895716, 117.39085288, 129.12993817, 142.04293198, 156.24722518, 171.8719477 , 189.05914247, 207.96505672, 228.76156239, 251.63771863, 276.80149049, 304.48163954])
1.0 + 0.1 * A # 단리계산법
array([1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7. ])
plt.plot(A, 2 * A, 'o') # 매년 2 달러씩 증가 (원으로 그림)
plt.plot(A, 1.0 + 0.1 * A, '^') # 단리 10%
plt.plot(A, 1.1 ** A) # 복리 10%
[<matplotlib.lines.Line2D at 0x7fa61715e550>]
plt.plot(A, 2 * A, 'o') # 매년 2 달러씩 증가 (원으로 그림)
plt.plot(A, 1.0 + 0.1 * A, '^') # 단리 10%
plt.plot(A, 1.1 ** A) # 복리 10%
plt.title('Linear vs. Simple Interest vs. Compounded Interest')
plt.xlabel('Years')
plt.ylabel('Value of Funds')
plt.show()
IQ_list = [
91, 110, 105, 107, 135, 127, 92, 111, 105,
106, 130, 145, 145, 128, 109, 108, 98, 129, 100,
108, 114, 119, 99, 137, 142, 145, 112, 113
]
import numpy as np
import matplotlib.pyplot as plt
IQ_A = np.array(IQ_list)
plt.hist(IQ_A)
plt.show()
plt.hist(IQ_A, bins=20)
plt.show()
plt.hist(IQ_A)
plt.title('IQ Distribution of Development Team.')
plt.show()
A = np.random.standard_normal(200_000)
A = A * 10 + 100
plt.hist(A, bins=80, color='g')
plt.title('Normal Distribution in 80 Bins')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
A = np.random.standard_normal(2_000_000)
A = A * 10 + 100
B = np.histogram(A, 50)
print(len(B), len(B[0]), len(B[1]))
plt.plot(B[0])
plt.show()
2 50 51
B[0]
array([ 1, 3, 7, 13, 33, 70, 147, 256, 561, 1095, 2025, 3517, 5868, 9708, 15015, 22825, 33386, 46136, 61511, 79069, 97872, 116781, 133158, 146067, 156063, 157782, 154382, 145311, 131452, 113754, 95431, 77081, 59819, 43841, 31689, 21920, 14216, 9182, 5627, 3349, 1889, 1058, 556, 264, 111, 53, 31, 8, 4, 3])
B[1]
array([ 49.56421013, 51.54663769, 53.52906525, 55.51149281, 57.49392037, 59.47634793, 61.45877549, 63.44120306, 65.42363062, 67.40605818, 69.38848574, 71.3709133 , 73.35334086, 75.33576842, 77.31819598, 79.30062354, 81.2830511 , 83.26547866, 85.24790622, 87.23033378, 89.21276134, 91.1951889 , 93.17761646, 95.16004402, 97.14247158, 99.12489914, 101.10732671, 103.08975427, 105.07218183, 107.05460939, 109.03703695, 111.01946451, 113.00189207, 114.98431963, 116.96674719, 118.94917475, 120.93160231, 122.91402987, 124.89645743, 126.87888499, 128.86131255, 130.84374011, 132.82616767, 134.80859523, 136.7910228 , 138.77345036, 140.75587792, 142.73830548, 144.72073304, 146.7031606 , 148.68558816])
import numpy as np
import matplotlib.pyplot as plt
A = np.random.standard_normal(2_000_000)
A = A * 10 + 100
B, X = np.histogram(A, 50)
X = (X[1:] + X[:-1])/2 # 경계 대신 통의 중간 값을 사용한다.
plt.plot(X, B) # 통 숫자 대신 값으로
plt.show() # 그래프를 표현한다.
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2 * np.pi, 1000)
plt.plot(np.cos(theta), np.sin(theta))
plt.show()
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2 * np.pi, 1000)
plt.plot(np.cos(theta), np.sin(theta))
plt.axis('equal')
plt.show()
A_data = [3.7, 2.5, 1.9, 0.5]
A_labels = ['Poker', 'Chess', 'Comic Books', 'Exercise']
A_colors = ['k', 'g', 'r', 'c']
import numpy as np
import matplotlib.pyplot as plt
plt.pie(A_data, labels=A_labels, colors=A_colors)
plt.title('Relative Hobbies of Dev. Team')
plt.axis('equal')
plt.show()
import numpy as np
A = np.ones(5)
B = np.arange(5, dtype="float32")
print(A, B)
[1. 1. 1. 1. 1.] [0. 1. 2. 3. 4.]
np.dot(A, A)
5.0
np.dot(A, B)
10.0
np.dot(B, B)
30.0
A = np.arange(6).reshape(2,3)
B = np.arange(6).reshape(3,2)
C = np.dot(A, B)
print(A, B, sep='\n\n')
print('\nDot product:\n', C)
[[0 1 2] [3 4 5]] [[0 1] [2 3] [4 5]] Dot product: [[10 13] [28 40]]
print(np.dot([10, 15, 30], B))
[150 205]