x = 1.0 / 3.0
print(x)
y = 10**30
print(y)
print(x * y)
print('{0:f}'.format(x * y))
0.3333333333333333 1000000000000000000000000000000 3.333333333333333e+29 333333333333333316505293553664.000000
import numpy as np
x = np.array([1,2,3,4])
x
array([1, 2, 3, 4])
x = np.array([[1,2,3,4], [5,6,7,8]])
x
array([[1, 2, 3, 4], [5, 6, 7, 8]])
x = np.ones([2,3])
x
array([[1., 1., 1.], [1., 1., 1.]])
x = np.zeros([2,3])
x
array([[0., 0., 0.], [0., 0., 0.]])
x = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([[5.0, 6.0], [7.0, 8.0]])
print(x)
print(y)
[[1. 2.] [3. 4.]] [[5. 6.] [7. 8.]]
print(x + y)
[[ 6. 8.] [10. 12.]]
print(x - y)
[[-4. -4.] [-4. -4.]]
print(x * y)
[[ 5. 12.] [21. 32.]]
print(np.matmul(x, y))
[[19. 22.] [43. 50.]]
print(x**2)
[[ 1. 4.] [ 9. 16.]]
x = np.array([[1, 2], [3, 4]])
x.dtype
dtype('int32')
x = np.array([[1.0, 2.0], [3.0, 4.0]])
x.dtype
dtype('float64')
x = np.array([[1, 2], [3.0, 4.0]])
x.dtype
dtype('float64')
x = np.array([[True, False],[False, True]])
print(x)
x.dtype
[[ True False] [False True]]
dtype('bool')
x.astype(int)
array([[1, 0], [0, 1]])
x = np.arange(0.0, 10.0, 0.1)
x
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 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. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9])
x.shape
(100,)
x = x.reshape([10, 10])
x
array([[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], [1. , 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. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9], [8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9], [9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9]])
x[0]
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
x[:,0]
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
x[2,3]
2.3000000000000003
x[4:8,1:4]
array([[4.1, 4.2, 4.3], [5.1, 5.2, 5.3], [6.1, 6.2, 6.3], [7.1, 7.2, 7.3]])
x = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([[5.0, 6.0], [7.0, 8.0]])
print(x)
print(y)
[[1. 2.] [3. 4.]] [[5. 6.] [7. 8.]]
np.vstack([x, y])
array([[1., 2.], [3., 4.], [5., 6.], [7., 8.]])
np.hstack([x, y])
array([[1., 2., 5., 6.], [3., 4., 7., 8.]])
x = np.array([1.0, 2.0])
y = np.array([[5.0, 6.0], [7.0, 8.0]])
print(x)
print(y)
[1. 2.] [[5. 6.] [7. 8.]]
np.vstack([x,y])
array([[1., 2.], [5., 6.], [7., 8.]])
np.hstack([x,y])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-82-65c95db36c10> in <module>() ----> 1 np.hstack([x,y]) ~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in hstack(tup) 284 # As a special case, dimension 0 of 1-dimensional arrays is "horizontal" 285 if arrs and arrs[0].ndim == 1: --> 286 return _nx.concatenate(arrs, 0) 287 else: 288 return _nx.concatenate(arrs, 1) ValueError: all the input arrays must have same number of dimensions
import numpy as np
import matplotlib.pyplot as pl
def mandelbrot(h, w, maxit=20):
"""Returns an image of the Mandelbrot fractal of size (h,w)."""
y, x = np.ogrid[-1.4:1.4:h*1j, -2:0.8:w*1j]
c = x + y * 1j
z = c
divtime = maxit + np.zeros(z.shape, dtype=int)
for i in range(maxit):
z = z**2 + c
diverge = z * np.conj(z) > 2**2 # who is diverging
div_now = diverge & (divtime==maxit) # who is diverging now
divtime[div_now] = i # note when
z[diverge] = 2 # avoid diverging too much
return divtime
pl.imshow(mandelbrot(400,400))
<matplotlib.image.AxesImage at 0x20ab1a9e048>