Jupyter
在此处摁下“m"键就会编程markdown模式
摁”b“键是在下方创建代码
%matplotlib inline
作用:%matplotlib inline这一句是IPython的魔法函数,可以在IPython编译器里直接使用,作用是内嵌画图,省略掉plt.show()这一步,直接显示图像。
如果不加这一句的话,我们在画图结束之后需要加上plt.show()才可以显示图像。不然就只有<matplotlib.collections.PathCollection at 0x2aa7c1ae490>这行话
一定导入的是matplotlib.pyplot 不然里面没有scatter方法,
scatter()方法的参数含义参考:https://blog.csdn.net/m0_37393514/article/details/81298503
import pandas as pd
data = pd.read_csv("C:\\Users\\94823\\Desktop\\aaa.csv")
data
aaaIncome | Education | Income | |
---|---|---|---|
0 | 1 | 10.000000 | 26.658839 |
1 | 2 | 10.401338 | 27.306435 |
2 | 3 | 10.842809 | 22.132410 |
3 | 4 | 11.244147 | 21.169841 |
4 | 5 | 11.645449 | 15.192634 |
5 | 6 | 12.086957 | 26.398951 |
6 | 7 | 12.048829 | 17.435307 |
7 | 8 | 12.889632 | 25.507885 |
8 | 9 | 13.290970 | 36.884595 |
9 | 10 | 13.732441 | 39.666109 |
10 | 11 | 14.133779 | 34.396281 |
11 | 12 | 14.635117 | 41.497994 |
12 | 13 | 14.978589 | 44.981575 |
13 | 14 | 15.377926 | 47.039595 |
14 | 15 | 15.779264 | 48.252578 |
15 | 16 | 16.220736 | 57.034251 |
16 | 17 | 16.622074 | 51.490919 |
17 | 18 | 17.023411 | 51.336621 |
18 | 19 | 17.464883 | 57.681998 |
19 | 20 | 17.866221 | 68.553714 |
20 | 21 | 18.267559 | 64.310925 |
21 | 22 | 18.709030 | 68.959009 |
22 | 23 | 19.110368 | 74.614639 |
23 | 24 | 19.511706 | 71.867195 |
24 | 25 | 19.913043 | 76.098135 |
25 | 26 | 20.354515 | 75.775216 |
26 | 27 | 20.755853 | 72.486055 |
27 | 28 | 21.167191 | 77.355021 |
28 | 29 | 21.598662 | 72.118790 |
29 | 30 | 22.000000 | 80.260571 |
import matplotlib.pyplot as plt %matplotlib inline
plt.scatter(data.Education,data.Income)
<matplotlib.collections.PathCollection at 0x2aa7c1ae490>
x = data.Education # 定义横纵坐标 y = data.Income
import tensorflow as tf
model = tf.keras.Sequential() # 建立一个输入一个输出的模型。比如图中的线性模型y=ax+b,x为一个输入,y为一个输出
model.add(tf.keras.layers.Dense(1,input_shape=(1,))) # 前面的1对应的是units这个参数,表示该层有多少个神经单元。 # 同时也是与上一层连接的W的列数。 # input_shape的第一维一般表示一个batch的大小, # 即有多少个样本。后面表示每个样本应该有多少特征,也是多少个列。如果不写会根据输入的X自动推断。 # 具体参考https://blog.csdn.net/weixin_42055967/article/details/104420985
model.summary() # ax+b param中的两个参数就是a与b
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 1) 2 ================================================================= Total params: 2 Trainable params: 2 Non-trainable params: 0 _________________________________________________________________
model.compile( optimizer = 'adam', loss = 'mse' ) # 减少误差优化的方法是梯度下降法:简写‘adam’,优化的损失函数是均方差‘mse’, # 通过这种方法编译
history = model.fit(x, y, epochs=5000) # 把所有数据训练5000次
Epoch 4992/5000 1/1 [==============================] - 0s 3ms/step - loss: 287.1974 Epoch 4993/5000 1/1 [==============================] - 0s 2ms/step - loss: 286.9698 Epoch 4994/5000 1/1 [==============================] - 0s 3ms/step - loss: 286.7423 Epoch 4995/5000 1/1 [==============================] - 0s 3ms/step - loss: 286.5152 Epoch 4996/5000 1/1 [==============================] - 0s 2ms/step - loss: 286.2881 Epoch 4997/5000 1/1 [==============================] - 0s 2ms/step - loss: 286.0614 Epoch 4998/5000 1/1 [==============================] - 0s 3ms/step - loss: 285.8348 Epoch 4999/5000 1/1 [==============================] - 0s 3ms/step - loss: 285.6085 Epoch 5000/5000 1/1 [==============================] - 0s 3ms/step - loss: 285.3824
model.predict(x) # 通过x预测
array([[25.934877], [26.824005], [27.802044], [28.691174], [29.58022 ], [30.558342], [30.473873], [32.336597], [33.225723], [34.203762], [35.092888], [36.20356 ], [36.96449 ], [37.849186], [38.73831 ], [39.716354], [40.60548 ], [41.494606], [42.47265 ], [43.361774], [44.250904], [45.228943], [46.118073], [47.007195], [47.89632 ], [48.874363], [49.763493], [50.674774], [51.630657], [52.519787]], dtype=float32)
model.predict(pd.Series([20]))
array([[48.088966]], dtype=float32)