必威体育Betway必威体育官网
当前位置:首页 > IT技术

多元线性回归

时间:2019-10-02 08:13:22来源:IT技术作者:seo实验室小编阅读:85次「手机版」
 

多元线性回归

# Multiple Linear regression

# Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset = pd.read_csv('50_Startups.csv')

X = dataset.iloc[:, :-1].values

y = dataset.iloc[:, 4].values

# Encoding categorical data

# Encoding the Independent Variable

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

labelencoder_X = LabelEncoder()

X[:, 3] = labelencoder_X.fit_transform(X[:, 3])

onehotencoder = OneHotEncoder(categorical_features = [3])

X = onehotencoder.fit_transform(X).toarray()

# Avoiding the dummy Variable Trap

X = X[:, 1:]

# Splitting the dataset into the Training set and Test set

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)

# Feature Scaling

"""from sklearn.preprocessing import Standardscaler

sc_X = StandardScaler()

X_train = sc_X.fit_transform(X_train)

X_test = sc_X.transform(X_test)

sc_y = StandardScaler()

y_train = sc_y.fit_transform(y_train)"""

# Fitting Multiple Linear Regression to the Training set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X_train, y_train)

# Predicting the Test set results

y_pred = regressor.predict(X_test)

# building the optimal model using Backward Elimination

import statsmodels.formula.api as sm

X_train = np.APPend(arr = np.ones((40, 1)).astype(int), values = X_train, axis = 1)

X_opt = X_train [:, [0, 1, 2, 3, 4, 5]]

regressor_OLS = sm.OLS(endog = y_train, exog = X_opt).fit()

regressor_OLS.summary()

X_opt = X_train [:, [0, 1, 3, 4, 5]]

regressor_OLS = sm.OLS(endog = y_train, exog = X_opt).fit()

regressor_OLS.summary()

X_opt = X_train [:, [0, 3, 4, 5]]

regressor_OLS = sm.OLS(endog = y_train, exog = X_opt).fit()

regressor_OLS.summary()

X_opt = X_train [:, [0, 3, 5]]

regressor_OLS = sm.OLS(endog = y_train, exog = X_opt).fit()

regressor_OLS.summary()

X_opt = X_train [:, [0, 3]]

regressor_OLS = sm.OLS(endog = y_train, exog = X_opt).fit()

regressor_OLS.summary()

相关阅读

滴滴顺风车要回归?这次可能是“众望所归”

文/东方亦落昨天下午,滴滴顺风车业务负责人张瑞通过官微发表了《滴滴顺风车致大家的一封信》。内容包括在顺风车下线这段时间所做

多项式回归(polynomial regression)转换为线性回归(linea

一、介绍一元m次多项式回归方程:二元二次多项式回归方程:多元多次的多项式回归方程较复杂,加之实际生产生活中一元m次多项式归回就已

回归算法和决策树

一、回归算法 1.回归分析的概念 回归分析(regression analysis)是确定两种或两种以上变量间相互依赖的定量关系的一种统计分析

回归分析详解及matlab实现

回归分析方法想要资源的请关注公众号: 在一起的足球自动获取资源和数十种经典算法,帮助各位提升自己之前留的是自己的qq号 感觉好多

什么是回归分析法

“回归分析”是解析“注目变量”和“因于变量”并明确两者关系的统计方法。以下是seo实验室小编

分享到:

栏目导航

推荐阅读

热门阅读