-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_linear_regression.py
65 lines (48 loc) · 1.32 KB
/
multi_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
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 20 09:18:15 2021
@author: adgbh
"""
"""
Import Libraries
"""
import pandas as pa
import numpy as np
import matplotlib.pyplot as plt
"""
import Datasets
"""
dataset = pa.read_csv("50_Startups.csv")
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
"""
Encode catagorical 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))
"""
Seperate test and train datasets
"""
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)
"""
Train the Multiple Linear regression model wi the 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))
"""
Predicting for a value
"""
rd = float(input("Enter R&D Cost :"))
ad = float(input("Enter Administration cost : "))
mk = float(input("Enter Marketing cost : "))
print (regressor.predict([[1,0,0,rd,ad,mk]]))