如何找到我的图形python的梯度



如何找到图的梯度,我使用了一个实验的外部文件中的数据。我尝试过各种不同的方法,我认为问题来自于我从外部文件中获取数据时,但我不确定是否有人能帮忙?

x的数据为2640828956和31508,y的数据为4.97,0.09和0

import matpoltlib.pyplot as plt
%matplotlib inline
f = open("Large C 100ohms","r")
lines = f.readlines()[1:]
x = [line.split()[0] for line in lines]
y = [line.split()[1] for line in lines]
x_1 = np.array(x)
#print(x_1)
y_1 = np.array(y)
#print(y_1)
plt.plot(x, y,'bo', linestyle='-')
plt.gca().invert_yaxis()
plt.xlabel('Time(ms)')
plt.ylabel('Voltage(V)')
plt.title('100 ohms')
#gradient
plt.show()
f.close()

您可以使用tensorflow:

x=[26408,28956]
y=[4.97, 0.09 ]
!pip install tensorflow
import tensorflow as tf 
model=tf.keras.Sequential([tf.keras.layers.Dense(units=1,input_shape=[1])])
model.compile(optimizer="sgd",loss="mean_squared_error")
model.fit(x,y,epochs=3)
print(model.predict([31508]))

预测的输出=[[-6.44407e+25]](非常接近0(另外,如果你不想使用这样的嵌入式模型,请告诉我。我的意思是,如果你只想通过纯粹的数学来做。

最新更新