使用 Python 编写一个机器学习模型例如线性回归或逻辑回归用于进行数据预处理?

使用 Python 编写一个机器学习模型例如线性回归或逻辑回归用于进行数据预处理?

import pandas as pd
import numpy as np

# Load the data
data = pd.read_csv('data.csv')

# Preprocess the data
# Linear Regression
X_train = data.iloc[:, 1:].values.reshape(-1, 1)
y_train = data.iloc[:, 0].values

# Logistic Regression
X_train_logistic = data.iloc[:, 1:].values.reshape(-1, 1)
y_train_logistic = data.iloc[:, 1:].values

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=42)

# Train the linear regression model
model_linear = LinearRegression()
model_linear.fit(X_train, y_train)

# Train the logistic regression model
model_logistic = LogisticRegression()
model_logistic.fit(X_train_logistic, y_train_logistic)

# Evaluate the models on the testing set
score_linear = model_linear.score(X_test, y_test)
score_logistic = model_logistic.score(X_test, y_test)

# Print the scores
print('Linear Regression Score:', score_linear)
print('Logistic Regression Score:', score_logistic)

data.csv 是一个包含线性回归或逻辑回归模型训练所需的数据的 CSV 文件。

注意:

  • 你需要根据你的数据而调整线性回归或逻辑回归的模型参数。
  • 你可以使用不同的测试集大小,以评估模型的性能。
  • 你可以使用不同的机器学习库来实现线性回归和逻辑回归。
相似内容
更多>