ML Resources and Notes: Multiple Regression
Fist we identify different steps then we write code for specified steps for clarity of codes: ---------------------------------- For Multiple Linear Regression: Importing the libraries Importing the dataset Encoding categorical data Splitting the dataset into the Training set and Test set Training the Multiple Linear Regression model on the Training set Predicting the Test set results ----------------------------------- Import Libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd Import data dataset = pd.read_csv('50_Startups.csv') X = dataset.iloc[:, :-1].values y = dataset.iloc[:, -1].values Encoding Categorical Data from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])], remainder='passthrough') X = np.array(ct.fit_transform(X)) Splitting Training and Test Data from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0) Training multiple linear regression model on Training set from sklearn.linear_model import LinearRegression regressor = LinearRegression() regressor.fit(X_train, y_train) Predicting the test result y_pred = regressor.predict(X_test) np.set_printoptions(precision=2) print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1)) Making prediction for a single set of data: 1, 0, 0, 160000, 130000, 300000 print(regressor.predict([[1, 0, 0, 160000, 130000, 300000]])) Getting final linear regression eqation: print(regressor.coef_) print(regressor.intercept_)