data = np.loadtxt("data/artifical_lin.txt") X = data[:, :-1] y = data[:, -1] print X[:10, :] print y[:10] from sklearn.utils import shuffle X, y = shuffle(X, y, random_state=1) print X.shape print y.shape train_set_size = X.shape[0] / 2 print train_set_size X_train = X[:train_set_size, :] # selects first train_set_size rows (examples) for train set X_test = X[train_set_size:, :] # selects from row train_set_size until the last one for test set print(X_train.shape) print(X_test.shape) y_train = y[:train_set_size] # selects first 15 rows (targets) for train set y_test = y[train_set_size:] # selects from row 250 until the last one for test set print(y_train.shape) print(y_test.shape) from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) ax.scatter3D(X_train[:500, 0], X_train[:500, 1], y_train[:500]) ax.view_init(6,-20) plt.show() from sklearn import linear_model regr = linear_model.LinearRegression() regr.fit(X_train, y_train); print(regr.coef_) print(regr.intercept_) # The mean square error print("Training error: ", np.mean((regr.predict(X_train) - y_train) ** 2)) print("Test error: ", np.mean((regr.predict(X_test) - y_test) ** 2)) from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) ax.scatter3D(X_train[:500, 0], X_train[:500, 1], y_train[:500]) # plots 3d points, 500 is number of points which are visualized # here we create plane which we want to plot, using the train data and predictions (you don't need to understand it) range_x = np.linspace(X_train[:, 0].min(), X_train[:, 0].max(), num=10) range_y = np.linspace(X_train[:, 1].min(), X_train[:, 1].max(), num=10) xx, yy = np.meshgrid(range_x, range_y) zz = np.vstack([xx.ravel(), yy.ravel()]).T pred = regr.predict(zz) pred = pred.reshape(10, 10) ax.plot_surface(xx, yy, pred, alpha=.1) # plots the plane ax.view_init(6,-20) plt.show() from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) ax.scatter3D(X_test[:500, 0], X_test[:500, 1], y_test[:500]) # plots 3d points 500 is number of points which are visualized # here we create plane which we want to plot, using the train data and predictions (you don't need to understand it) range_x = np.linspace(X_test[:, 0].min(), X_test[:, 0].max(), num=10) range_y = np.linspace(X_test[:, 1].min(), X_test[:, 1].max(), num=10) xx, yy = np.meshgrid(range_x, range_y) zz = np.vstack([xx.ravel(), yy.ravel()]).T pred = regr.predict(zz) pred = pred.reshape(10, 10) ax.plot_surface(xx, yy, pred, alpha=.1) # plots the plane ax.view_init(6,-20) plt.show()