深度学习入门ch2a numpy型感知器1(note)
In [59]:
import numpy as np
x=np.array([1.0, 3.0,5.0])
print(x)
print(type(x))
Output:
[1. 3. 5.]
<class 'numpy.ndarray'>
In [60]:
y=np.array([10.0,20.0,30.0])
z=x+y
print(z)
print(y/x)
print(x*y)
print(x/2.0)
Output:
[11. 23. 35.]
[10. 6.66666667 6. ]
[ 10. 60. 150.]
[0.5 1.5 2.5]
In [61]:
a=np.array([[1.2,2.4],[3.6,4.8]]) # double []
print(a)
print(a.shape)
print(a.dtype)
Output:
[[1.2 2.4]
[3.6 4.8]]
(2, 2)
float64
In [62]:
b=np.array([[5,10],[7,11]])
print(a+b)
print(a*b)
Output:
[[ 6.2 12.4]
[10.6 15.8]]
[[ 6. 24. ]
[25.2 52.8]]
In [63]:
a2 = np.array([[1,2],[3,4]])
b2 = np.array([10,20])
print(a2*b2)
print(a2[0])
print(a2[1][1])
Output:
[[10 40]
[30 80]]
[1 2]
4
In [64]:
for row in a2:
print(row)
Output:
[1 2]
[3 4]
In [65]:
print(y.flatten())
print(y[1,2]) # it is wrong using index directly
Output:
[10. 20. 30.]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-65-aaf42b68dd20> in <module>()
1 print(y.flatten())
----> 2 print(y[1,2]) # it is wrong using index directly
IndexError: too many indices for array
In [70]:
print(y[np.array([0,2])]) #using [] and np ndarray
Output:
[10. 30.]
In [71]:
print(y)
y2 = y > 15
print(y>15)
print(type(y2))
Output:
[10. 20. 30.]
[False True True]
<class 'numpy.ndarray'>
In [72]:
print(y[y>15]) # if true , fetch out,取出true对应的元素
Output:
[20. 30.]
In [66]:
import matplotlib.pyplot as plt
%pylab inline
x=np.arange(0,6,0.1) # step is 0.1
y1=np.sin(x)
y2=np.cos(x)
plt.plot(x,y1,label="sin")
plt.plot(x,y2,linestyle="--",label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title('sin & cos')
plt.legend()
plt.show()
Output:
Populating the interactive namespace from numpy and matplotlib
In [69]:
import os
from matplotlib.image import imread
img = imread('C:\\Users\\Administrator\\0009.jpg')
plt.imshow(img)
Output:
Out[69]:
<matplotlib.image.AxesImage at 0xc9de908>
|