1、图像获取
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imshow('image',img)
cv2.waitKey(10000)
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('image1.jpg',img)
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('image3.png',img,[cv2.IMWRITE_PNG_COMPRESSION,0])
2、numpy
1、创建矩阵(普通矩阵、零矩阵、单位矩阵)
2、矩阵维度、改查、基本运算
3、矩阵加乘
import numpy as np
data1 = np.array([[1,2,3],[4,5,6]])
data5 = np.array([[1,2],[4,5],[1,2]])
data2 = np.zeros([2,3])
data3 = np.ones([2,3])
data4 = np.array([[1,2,3],[4,5,6]])
print(data1)
print(data2)
print(data3)
print(data1.shape)
print(data1*2)
print(data1+2)
print(data1/2)
print(data1*data4)
print(np.matmul(data1,data5))
3、tensorflow
常量变量定义
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")
data1 = tf.constant(2,dtype=tf.int32)
data2 = tf.Variable(10,name='hh')
print(data1,data2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(data1))
print(sess.run(data2))```
```python
import tensorflow as tf
data1 = tf.constant(3,dtype=tf.int32)
data2 = tf.constant(2,dtype=tf.int32)
dataadd = tf.add(data1,data2)
datasub = tf.subtract(data1,data2)
datamul = tf.multiply(data1,data2)
datadiv = tf.divide(data1,data2)
with tf.Session() as sess:
print(sess.run(dataadd))
print(sess.run(datasub))
print(sess.run(datamul))
print(sess.run(datadiv))
import tensorflow as tf
data1 = tf.Variable(3,name='a')
data2 = tf.Variable(2,name='b')
dataadd = tf.add(data1,data2)
datacopy = tf.assign(data2,dataadd)
datasub = tf.subtract(data1,data2)
datamul = tf.multiply(data1,data2)
datadiv = tf.divide(data1,data2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(dataadd))
print(sess.run(datasub))
print(sess.run(datamul))
print(sess.run(datadiv))
print('datacopy1:',sess.run(datacopy))
print('datacopy.eval',datacopy.eval())
print('tf.get_default_session()',tf.get_default_session().run(datacopy))
tensorflow=tensor(张量)+计算图
4、mat
import tensorflow as tf
data1 = tf.placeholder(tf.float32)
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
print(sess.run(dataAdd,feed_dict={data1:3,data2:5}))
print('end!')```
```python
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 = tf.constant([[1,2],
[3,4]])
data3 = tf.constant([[1],
[2]])
print(data2.shape)
with tf.Session() as sess:
print(sess.run(data2))
print(sess.run(data2[0]))
print(sess.run(data2[:,0]))
print(sess.run(data2[0,0]))
```python
import tensorflow as tf
data1 = tf.constant([[1,2,3],[4,5,6]])
data2 = tf.constant([[1,2],[3,4],[5,6]])
data3 = tf.constant([[1,2],[3,4],[5,6]])
matAdd = tf.add(data3,data2)
matMul = tf.matmul(data1,data2)
with tf.Session() as sess:
print(sess.run(matAdd))
print(sess.run(matMul))```
```python
import tensorflow as tf
mat0 = tf.zeros([2,3])
mat1 = tf.ones([2,3])
mat2 = tf.fill([2,3],15)
with tf.Session() as sess:
print(sess.run(mat0))
print(sess.run(mat1))
print(sess.run(mat2))```
```python
import tensorflow as tf
mat1 = tf.constant([[1],[2],[3]])
mat2 = tf.zeros_like(mat1)
mat3 = tf.linspace(0.0,2.0,11)
mat4 = tf.random_uniform([2,3],-1,2)
with tf.Session() as sess:
print(sess.run(mat2))
print(sess.run(mat3))
print(sess.run(mat4))```
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7])
y = np.array([23,44,2,5,54,3,4])
plt.plot(x,y,'r',lw=10)
x = np.array([1,2,3,4,5,6,7])
y = np.array([2,4,23,54,4,33,43])
plt.bar(x,y,alpha=0.5,color='b')
plt.show()
6、股票预测
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date = np.linspace(1,15,15)
openPrice = np.array([22,44,42,53,12,53,63,5,51,36,67,35,42,23,45])
closePrice = np.array([3,23,45,56,3,34,67,9,90,45,23,45,63,2,46])
plt.figure('股市预测',facecolor='blue')
plt.ylabel('价格')
plt.xlabel('日期')
for i in range(0,15):
dateone = np.zeros([2])
dateone[0] = i
dateone[1] = i
priceone = np.zeros([2])
priceone[0] = openPrice[i]
priceone[1] = closePrice[i]
if openPrice[i]>closePrice[i]:
plt.plot(dateone,priceone,'r',lw=8)
else:
plt.plot(dateone,priceone,'g',lw=8)
dateNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range(0,15):
dateNormal = i/14
priceNormal = closePrice[i]/70
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
w1 = tf.Variable(tf.random.uniform([1,10],0,1))
b1 = tf.Variable(tf.zeros([1,10]))
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1)