100天学习机器学习python代码计划-Day3:多值线性回归预测模型

跟着github上的Avik-Jain学习机器学习:

https://github.com/Avik-Jain/100-Days-Of-ML-Code

Day3:多值线性回归预测模型

学习任务:

获取数据,对数据预处理(编码),分割数据集

创建线性回归模型,学习并预测

1.数据处理

#imports library and read data
import pandas as pd
import numpy as np

#读取数据,并将数据分为样本和标签
data = pd.read_csv('G:/MachineLearningDailyStudy-100/100-Days-Of-ML-Code-master/datasets/50_Startups.csv')
x = data.iloc[:, :-1].values
y = data.iloc[:, 4].values

#对样本中的属性进行编码
#data preprocessing
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder = LabelEncoder()
x[:, 3] = labelencoder.fit_transform(x[:, 3])
onehotencoder = OneHotEncoder(categorical_features=[3])
x = onehotencoder.fit_transform(x).toarray()
x = x[:, 1:]

#将数据分为训练集和测试集
#spliting the dataset into test_data and train_data
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=1/4, random_state=0)

2.创建线性回归模型,训练和测试

#创建线性模型
#make the linearregression
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)

#预测结果
#predict
y_pred = regressor.predict(x_test)
print(y_test)
print(y_pred)

 

你可能感兴趣的:(机器学习)