python:ValueError:x和y的大小必须相同

时间:2020-03-08 12:50:35

标签: python pandas matplotlib scikit-learn

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
housing = pd.read_csv("housing.csv")
X = housing[['latitude', 'longitude']].to_numpy()
X
from sklearn.preprocessing import OrdinalEncoder
housing_median_house_value=housing['median_house_value']
y =housing_median_house_value.ravel()
y
from sklearn.model_selection import train_test_split
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.3)

from sklearn import linear_model

model1 = linear_model.LinearRegression()

model1.fit(X, y)   
print(model1.intercept_,model1.coef_)

theta0, theta1,thetal2 = model1.intercept_, model1.coef_[0], model1.coef_[1] #train完

print(theta0, theta1,thetal2) 
>>-5829397.044942677 -69550.96552198952 -71209.3691841117

plt.figure(figsize = (12,8))
plt.scatter(X, y)

vx = np.linspace(0,110000,100)
vz = np.linspace(0,110000,100)
print(vx)
print(vz)
vy = theta0 + theta1 * vx + thetal2 * vz
print(vy)
plt.plot(vx, vy, vz, c='r')

ValueError: x and y must be the same size

我已经检查过vx,vz,vy大小,它们都是(100,)。但是,它仍然无法运行。请帮助我,谢谢!

0 个答案:

没有答案