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

本文是100天学习机器学习的Day3内容,主要探讨了多值线性回归预测模型。通过github上Avik-Jain的资源,学习了如何获取数据、进行预处理、编码、分割数据集,并使用Python的pandas和sklearn库创建、训练和测试线性回归模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跟着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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值