Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - MLR visualizing error ValueError Matplotlib Scatter

I am trying to do multiple linear regression. I splited the data and put into training model. But i have a problem when i am trying to visualize results.

    x = data.iloc[:,:-2].values # independent
    y = data.iloc[:,-1].values # dependent profit
    print(y)
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3, random_state = 0)

    regressor = LinearRegression()
    regressor.fit(x_train, y_train)
    
    y_pred = regressor.predict(x_test)
    x_pred = regressor.predict(x_train)

    plt.scatter(x_train, y_train, color = "green")
    plt.plot(x_train, x_pred, color = "red")
    plt.title("Companies Profit Prediction")
    plt.xlabel("Independent Variables")
    plt.ylabel("Profit")
    plt.show()

I have companies.csv which have columns rdspend, administration, marketing spend and profit. So my dependent variable is profit and independent variables are others(rdspend, admi...). I get the ValueError for plt.scatter saying x,y must be same size. x_train is multidimensional array while y_train is normal array. I want to see training set results. Somehow i need to change either x_train or y_train i guess. How can i solve that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...