0

https://www.kaggle.com/paree24/development-index i am trying to plot my linear regression model but i just cant seem to make in work.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from scipy.stats import shapiro
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import normalize
from math import sqrt
from sklearn.model_selection import cross_val_score

dataset = pd.read_csv("Development.csv")

x = np.array(dataset.drop(columns= ["Area (sq. mi.)", "Pop. Density ", "Development Index", "Infant mortality "]))
y = np.array(dataset["Infant mortality "])

plot1 = dataset.plot(x= "GDP ($ per capita)", y='Infant mortality ', style='o')
plot2 = dataset.plot(x= "Literacy (%)", y='Infant mortality ', style='o')
plot3 = dataset.plot(x= "Population", y='Infant mortality ', style='o')
plt.tight_layout()
plt.show()

stat, p = shapiro(y)
print(f"показатель {p}")
print(f"статистика {stat}")

#разделение модели для модели
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=10)
regressor = LinearRegression()
regressor.fit(x_train, y_train)
print(f"regressor.intercept_ {regressor.intercept_}")
print(f"regressor.coef_{regressor.coef_}")

scores = cross_val_score(regressor, x, y, cv=5)
print(scores)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))

y_pred = regressor.predict(x_test)
df = pd.DataFrame({'Actual': y_test.flatten(), 'Predicted': y_pred.flatten()})

and this is the plot i am trying to make in 3d

plt.scatter(x_test[0:,1], y_test)
plt.plot(x_test, y_pred, color='orange', linewidth=1 )
plt.show()

and this is what it shows

enter image description here

I have wasted 6 hours on this and i still cant figure out how to plot this, this is the closest i could come

plt3d = plt.figure().gca(projection='3d')
plt3d.view_init(azim=135)
plt3d.plot_trisurf(x_test[0:,1], y_pred[0:,1], alpha=0.7, antialiased=True)

and it gives me an error: IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

any ideas?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I'm unable to see your figure but I've noticed that you forgot to initialize 3d axis. You need to add `from mpl_toolkits.mplot3d import Axes3D` before you load matplotlib. See for example: https://matplotlib.org/2.0.2/examples/mplot3d/text3d_demo.html – ATony Sep 28 '21 at 21:44
  • Is [1](https://i.stack.imgur.com/0QPMd.png) and [2](https://i.stack.imgur.com/HX6d2.png) what you want? I'm not posting an answer, because I'm not sure if the line is correct. – Trenton McKinney Sep 28 '21 at 22:07
  • @ATony this is exactely what i wanted, but i thought it wasnt supposed to be a line but a 2d plane? also ax.scatter(x[:,0], x[:,1]....... what does the [:,0,1,2] mean? – Alexander Vasiliev Sep 29 '21 at 07:13
  • @TrentonMcKinney McKinney is exactely what i wanted, but i thought it wasnt supposed to be a line but a 2d plane? also ax.scatter(x[:,0], x[:,1]....... what does the [:,0,1,2] mean? – Alexander Vasiliev Sep 29 '21 at 09:36
  • `line` is an array, so `line[:, 0]` is a slice of the array. _i thought it wasnt supposed to be a line but a 2d plane_: I don't know, which is why I didn't post as an answer. – Trenton McKinney Sep 29 '21 at 11:51
  • 1
    Trenton McKinney Thank you very much for your help! – Alexander Vasiliev Sep 29 '21 at 12:17
  • You're welcome. Should the question be considered answered? The code used to produce the line, came from an answer to https://stackoverflow.com/q/65888553/7758804, so this question would be closed as a duplicate. – Trenton McKinney Sep 29 '21 at 18:38

0 Answers0