Path: blob/master/Part 2 - Regression/Multiple Linear Regression/multiple_linear_regression.py
1009 views
# Multiple Linear Regression12# Importing the libraries3import numpy as np4import matplotlib.pyplot as plt5import pandas as pd67# Importing the dataset8dataset = pd.read_csv('50_Startups.csv')9X = dataset.iloc[:, :-1].values10y = dataset.iloc[:, 4].values1112# Encoding categorical data13from sklearn.preprocessing import LabelEncoder, OneHotEncoder14labelencoder = LabelEncoder()15X[:, 3] = labelencoder.fit_transform(X[:, 3])16onehotencoder = OneHotEncoder(categorical_features = [3])17X = onehotencoder.fit_transform(X).toarray()1819# Avoiding the Dummy Variable Trap20X = X[:, 1:]2122# Splitting the dataset into the Training set and Test set23from sklearn.cross_validation import train_test_split24X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)2526# Feature Scaling27"""from sklearn.preprocessing import StandardScaler28sc_X = StandardScaler()29X_train = sc_X.fit_transform(X_train)30X_test = sc_X.transform(X_test)31sc_y = StandardScaler()32y_train = sc_y.fit_transform(y_train)"""3334# Fitting Multiple Linear Regression to the Training set35from sklearn.linear_model import LinearRegression36regressor = LinearRegression()37regressor.fit(X_train, y_train)3839# Predicting the Test set results40y_pred = regressor.predict(X_test)4142