m0_72041809 2022-07-09 20:37 采纳率: 50%
浏览 42
已结题

线性回归数组行数不对应

问题遇到的现象

线性回归中遇到数组行数不对应的情况

问题相关代码,请勿粘贴截图
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 change40.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400

z40=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z40.shape[0],30):
    z1=z40[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)


list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label40=y_array
z40=np.vstack((x0,x1,x2,x3,x4,x5)).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 50.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)


list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z50=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z50.shape[0],30):
    z1=z50[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label50=y_array
z50=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T




 
img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 55.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z55=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z55.shape[0],30):
    z1=z55[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label55=y_array
z55=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 -60.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)


list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z60=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z60.shape[0],30):
    z1=z60[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label60=y_array
z60=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 65.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z65=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z65.shape[0],30):
    z1=z65[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label65=y_array
z65=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T



img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 70.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z70=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z70.shape[0],30):
    z1=z70[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label70=y_array
z70=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T


rescombine = np.vstack((z40,z50,z55,z60,z65,z70))
labels= np.hstack((label40,label50,label55,label60,label65,label70)).T
labels=labels.reshape(-1, 1)

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(rescombine,labels,test_size=0.25)

from sklearn.preprocessing import MinMaxScaler
mm = MinMaxScaler()
x_train = mm.fit_transform(x_train)
y_train = mm.fit_transform(y_train)
y_max = y_train.max(axis=0)
y_min = y_train.min(axis=0)

data=x_train
labels=y_train

class LinearRegression:
    def __init__(self,data,labels):
        
        self.data = data
        self.labels = labels
        num_features = len(data[1])
        self.theta = np.zeros((num_features,1))
        


    def train(self,alpha,num_iterations = 500):
       
        cost_history = self.gradient_descent(alpha,num_iterations)
        return self.theta,cost_history
        
    def gradient_descent(self,alpha,num_iterations):
        cost_history= []
        for _ in range(num_iterations):
            self.gradient_step(alpha)
            cost_history.append(self.cost_function(self.data,self.labels))
        return cost_history
        
        
    def gradient_step(self,alpha):    
        num_examples = data.shape[0]
        prediction = LinearRegression.hypothesis(self.data,self.theta)
        delta = prediction - self.labels   ##有问题...
        theta = self.theta
        theta = theta - alpha*(1/num_examples)*(np.dot(delta.T,self.data)).T
        self.theta = theta
        
        
    def cost_function(self,data,labels):
        self.m = len(labels) 
        delta = LinearRegression.hypothesis(data,self.theta) - labels
        cost = (1/2)*np.dot(delta.T,delta)/self.m
        return cost[0][0]
        
    
    def hypothesis(data,theta):   
        predictions = np.dot(data,theta)
        return predictions
        

x_train = rescombine

y_train = labels

num_iterations = 500
learning_rate = 0.01  


linear_regression = LinearRegression(x_train, y_train)

(theta, cost_history) = linear_regression.train(learning_rate, num_iterations)



print (theta, cost_history)
print(len( cost_history))

运行结果及报错内容
发生异常: ValueError
operands could not be broadcast together with shapes (207,1) (155,1) 
  File "C:\Users\Xpc\Desktop\LinearRegression\linear_regression.py", line 380, in gradient_step
    delta = prediction - self.labels   ##有问题...
  File "C:\Users\Xpc\Desktop\LinearRegression\linear_regression.py", line 372, in gradient_descent
    self.gradient_step(alpha)
  File "C:\Users\Xpc\Desktop\LinearRegression\linear_regression.py", line 366, in train
    cost_history = self.gradient_descent(alpha,num_iterations)
  File "C:\Users\Xpc\Desktop\LinearRegression\linear_regression.py", line 408, in <module>
    (theta, cost_history) = linear_regression.train(learning_rate, num_iterations)

我的解答思路和尝试过的方法

我提取了一些数据

import numpy as np

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 change40.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400

z40=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z40.shape[0],30):
    z1=z40[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)


list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label40=y_array
z40=np.vstack((x0,x1,x2,x3,x4,x5)).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 50.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)


list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z50=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z50.shape[0],30):
    z1=z50[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label50=y_array
z50=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T




 
img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 55.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z55=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z55.shape[0],30):
    z1=z55[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label55=y_array
z55=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 -60.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)


list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z60=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z60.shape[0],30):
    z1=z60[i,:]
    z0.append(z1)
z0=np.array(z0)

x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label60=y_array
z60=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T





img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 65.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z65=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z65.shape[0],30):
    z1=z65[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label65=y_array
z65=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T



img = cv2.imread(r'C:\Users\Xpc\Desktop\weixin2222 - 70.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 221])
high_hsv = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)

list_y = []
list_x = []

for i in range(len(mask)):
    xmax = []
    for j in range(len(mask[i])):
        if mask[i][j] == 0:
            list_x.append(j)
            list_y.append(len(mask)-i)

plt.plot(list_x, list_y, 'o', color='r')
plt.show()

x_array=np.array(list_x)
x_array=x_array/400
y_array=np.array(list_y)
y_array=y_array*0.2/400
z70=np.stack((x_array,y_array),axis=0).T
z0=[]
for i in range(0,z70.shape[0],30):
    z1=z70[i,:]
    z0.append(z1)
z0=np.array(z0)
x_array=z0[:,0]
y_array=z0[:,1]
print(x_array)
print(y_array)

list_x1=[]
for i in range(0,len(x_array)):
    list_x1.append(40)
x1_array=np.array(list_x1)

list_x0=[]
for i in range(0,len(x_array)):
    list_x0.append(1)
x0=np.array(list_x0)
x1=x_array
x2=x1_array
x3=x1*x2
x4=x1*x1
x5=x2*x2
label70=y_array
z70=np.stack((x0,x1,x2,x3,x4,x5),axis=0).T


rescombine = np.vstack((z40,z50,z55,z60,z65,z70))
labels= np.hstack((label40,label50,label55,label60,label65,label70)).T
labels=labels.reshape(-1, 1)

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(rescombine,labels,test_size=0.25)

from sklearn.preprocessing import MinMaxScaler
mm = MinMaxScaler()
x_train = mm.fit_transform(x_train)
y_train = mm.fit_transform(y_train)
y_max = y_train.max(axis=0)
y_min = y_train.min(axis=0)

data=x_train
labels=y_train

num_features = len(data[1])
theta = np.zeros((num_features,1))
predictions = np.dot(data,theta)
print(len(data[1]))#列数
print(len(data))#行数
print(len(theta[1]))
print(len(theta))
print(len(predictions [1]))
print(len(predictions))
print(len(labels))
print(len(labels[1]))

运行结果显示prediction并没有出错

6
155
1
6
1
155
155
1

我想要达到的结果 找出原程序里的错误原因
  • 写回答

1条回答 默认 最新

  • 溪风沐雪 2022-07-10 10:35
    关注

    我没太看明白你取得那6个特征,x0、2、5不都是常量吗x1和x3是一次函数,x4是二次函数,这个是否用3个特征就可以了,你最后想要的效果是要一个二次函数去拟合你原本图片中的那条线吗?
    我用你其中一张图片研究了一下,目前的效果是这样的:

    import numpy as np
    import matplotlib.pyplot as plt
    import cv2
    
    class LinearRegression:
        def __init__(self,data,labels):
            self.data = data
            self.labels = labels
            self.features = np.zeros((1,self.data.shape[1]))
        
        def train(self,learning_rate, num_iterations):
            for i in range(num_iterations):
                self.step_gradient(learning_rate)
                # loss = self.loss_fuction()
                # print(f'第{i}轮loss={loss}, features={self.features}')
            return self.features
        
        def step_gradient(self, learning_rate):
            N = float(len(self.labels))
            err_current = np.sum(self.features*self.data,axis=1) - self.labels
            features_gradient = np.array([sum([x**i*err for x,err in zip(self.data[:,i],err_current)])*(2/N) for i in range(self.features.shape[1])])
            self.features = self.features - (learning_rate* features_gradient)
        
        def loss_fuction(self):
            totalError = sum([(y-(np.sum(self.features*x,axis=1)))**2 for x,y in zip(self.data,self.labels)])
            return totalError / float(len(self.labels))
        
        def draw(self,num_iterations):
            plt.rcParams['font.sans-serif'] = ['SimHei']
            plt.rcParams['axes.unicode_minus'] = False
            x = self.data[:,1]
            sl = [s[0] for s in sorted(enumerate(x), key=lambda a:a[1])]
            y = [sum(x*self.features[0]) for x in self.data]
            sort_y = [y[i] for i in sl]
            sort_labels = [self.labels[i] for i in sl]
            x.sort()
            plt.scatter(x,sort_labels,label='source')
            plt.plot(x, sort_y,color='r',label='predict')
            plt.xlabel('X')
            plt.ylabel('Y')
            plt.title(str(num_iterations)+'轮')
            plt.legend()
            plt.show()
     
    if __name__ == '__main__':
        img = cv2.imread('line.jpg')
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        low_hsv = np.array([0, 0, 221])
        high_hsv = np.array([180, 30, 255])
        mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)
        list_y = []
        list_x = []
        
        for i in range(0,len(mask),5):
            xmax = []
            for j in range(0,len(mask[i]),5):
                if mask[i][j] == 0:
                    list_x.append(j)
                    list_y.append(i)
        list_x = [x/100 for x in list_x]
        list_y = [y/100 for y in list_y]
        list_x = [[1,x,x**2] for x in list_x]
        learning_rate = 0.001
        num_iterations = 1000
        lr = LinearRegression(np.array(list_x),np.array(list_y))
        lr.train(learning_rate,num_iterations)
        lr.draw(num_iterations)
    

    img


    img


    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月18日
  • 已采纳回答 7月10日
  • 创建了问题 7月9日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀