Python Matplotlib正在绘制一个';之字形';尝试绘制多项式时的直线
matplotlib中的线条是正确的形状,但是,它是由之字形线条组成的 我尝试重新启动并在desmos上绘制相同的方程式。desmos上的方程看起来正是我想要的。我认为这是一个matplotlib问题Python Matplotlib正在绘制一个';之字形';尝试绘制多项式时的直线,python,matplotlib,graphing,Python,Matplotlib,Graphing,matplotlib中的线条是正确的形状,但是,它是由之字形线条组成的 我尝试重新启动并在desmos上绘制相同的方程式。desmos上的方程看起来正是我想要的。我认为这是一个matplotlib问题 #imports import numpy as np import pandas as pd import seaborn as sns; sns.set() # just makes your plots look prettier run 'pip install seaborn' impo
#imports
import numpy as np
import pandas as pd
import seaborn as sns; sns.set() # just makes your plots look prettier run 'pip install seaborn'
import matplotlib.pyplot as plt
from IPython.core.pylabtools import figsize
figsize(15, 7)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)
noise = np.random.randn(100)
x = np.linspace(-2,2, 100)
y = x + noise + np.random.randn()*2 + x**2
plt.scatter(x, y); plt.show()
#pre processing
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
#initializing m and b variables
current_z_val = 0.1
current_m_val = 0.1
current_b_val = 0.1
#setting # of iterations
iterations = 5
#calculating length of examples for functions used below
n = len(x_train)
#learning rate
learning_rate = 0.01
#plot the data and estimates
plt.scatter(x_train,y_train)
plt.title("Example data and hypothesis lines")
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
cost_history = []
#main graident descent loop
for i in range(iterations):
#creating the hypothesis using y=z^2 + mx+b form
y_hypothesis = (current_z_val * (x_train**2)) + (current_m_val * x_train) + current_b_val
#calculating the derivatives from the image embedded above in code
z_deriv = -(2/n)*sum(y_train-y_hypothesis)
m_deriv = -(2/n)*sum(x_train*(y_train-y_hypothesis))
b_deriv = -(2/n)*sum(y_train-y_hypothesis)
#updating m and b values
current_z_val = current_z_val - (learning_rate * z_deriv)
current_m_val = current_m_val - (learning_rate * m_deriv)
current_b_val = current_b_val - (learning_rate * b_deriv)
#calculate the cost (error) of the model
cost = (1/n)*sum(y_train-y_hypothesis)**2
cost_history.append(cost)
#print the m and b values
#print("iteration {}, cost {}, m {}, b {}".format(i,cost,current_m_val,current_b_val))
plt.plot(x_train,y_hypothesis)
plt.show()
#plot the final graph
plt.plot(range(1,len(cost_history)+1),cost_history)
plt.title("Cost at each iteration")
plt.xlabel('Iterations')
plt.ylabel('MSE')
plt.show()
是我的图上的图形。它应该是这样的。
matplotlib
按照点在列表中的顺序绘制点,而不是根据点的大小绘制点的“自然”顺序
我认为你应该在计算y\u假设之前对x\u序列进行排序
,以获得你期望的函数
请注意,这在plt.scatter()
和plt.plot()
中都会发生,但您只能在后者中看到,因为在将点与plt.plot()
连接时,您实际看到的是序列。matplotlib
按照点在列表中的顺序绘制点,而不是它们的“自然”由它们的大小给出的顺序
我认为你应该在计算y\u假设之前对x\u序列进行排序
,以获得你期望的函数
请注意,这在plt.scatter()
和plt.plot()
中都会发生,但您只能在后者中看到,因为在将点与plt.plot()
连接时,您实际上看到了序列。函数训练测试分割
将随机选择xtrain
和xtest
,因此,您的x
将被洗牌。如果x
不正常,Matplotlib将无法绘制线
在下一行中使用shuffle=False
。这应该是正确的
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, shuffle=False)
功能train\u test\u split
将随机选择xtrain
和xtest
,因此您的x
将被洗牌。如果x
不正常,Matplotlib将无法绘制线
在下一行中使用shuffle=False
。这应该是正确的
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, shuffle=False)