问题遇到的现象
线性回归中遇到数组行数不对应的情况
问题相关代码,请勿粘贴截图
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