-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_linear_regression.py
52 lines (40 loc) · 1.4 KB
/
multiple_linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Multiple Linear Regression
## Importing the Libraries
import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
## Importing the Dataset
dataset = pd.read_csv('50_Startups.csv')
## Visualizing data
cols = dataset.columns
sb.pairplot(dataset[cols], size=2.5)
plt.tight_layout()
plt.show()
## Creating Correlation Matrix
colm= dataset.iloc[:,[0,1,2,4]]
cm = np.corrcoef(colm.values.T)
sb.set(font_scale=2.5)
hm = sb.heatmap(cm)
plt.show()
## Dataset to Independent(X) and Dependent Variables(Y)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
## Handling 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))
## Spitting Dataset into Train Test
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)
## Creating and Training Regression Model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
## Making the Predictions
y_pred = regressor.predict(X_test)
print(y_pred)
print(y_test)
#check the results of y_pred and y_test for how accurate your model is