Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 2 - Regression/Multiple Linear Regression/multiple_linear_regression.py
1009 views
1
# Multiple Linear Regression
2
3
# Importing the libraries
4
import numpy as np
5
import matplotlib.pyplot as plt
6
import pandas as pd
7
8
# Importing the dataset
9
dataset = pd.read_csv('50_Startups.csv')
10
X = dataset.iloc[:, :-1].values
11
y = dataset.iloc[:, 4].values
12
13
# Encoding categorical data
14
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
15
labelencoder = LabelEncoder()
16
X[:, 3] = labelencoder.fit_transform(X[:, 3])
17
onehotencoder = OneHotEncoder(categorical_features = [3])
18
X = onehotencoder.fit_transform(X).toarray()
19
20
# Avoiding the Dummy Variable Trap
21
X = X[:, 1:]
22
23
# Splitting the dataset into the Training set and Test set
24
from sklearn.cross_validation import train_test_split
25
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
26
27
# Feature Scaling
28
"""from sklearn.preprocessing import StandardScaler
29
sc_X = StandardScaler()
30
X_train = sc_X.fit_transform(X_train)
31
X_test = sc_X.transform(X_test)
32
sc_y = StandardScaler()
33
y_train = sc_y.fit_transform(y_train)"""
34
35
# Fitting Multiple Linear Regression to the Training set
36
from sklearn.linear_model import LinearRegression
37
regressor = LinearRegression()
38
regressor.fit(X_train, y_train)
39
40
# Predicting the Test set results
41
y_pred = regressor.predict(X_test)
42