使用python写一段逻辑回归的代码?
时间: 2023-03-08 09:43:13 浏览: 70
import numpy as np
from sklearn import linear_model# 训练数据
x = np.array([[1], [2], [3], [4]])
y = np.array([1, 2, 3, 4])# 创建线性回归模型
model = linear_model.LinearRegression()# 训练模型
model.fit(x, y)# 打印出参数
print('参数:', model.coef_[0])
相关问题
用python写一段,逻辑回归的代码
好的,以下是一段使用 Python 实现逻辑回归的代码:
```
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# 加载乳腺癌数据集
data = load_breast_cancer()
X = data.data
y = data.target
# 分割数据为训练数据和测试数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 训练逻辑回归模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 评估模型在测试数据上的性能
accuracy = model.score(X_test, y_test)
print("Accuracy: ", accuracy)
```
这段代码先加载了一个名为 `load_breast_cancer` 的乳腺癌数据集,然后使用 `train_test_split` 函数将数据集分为训练数据和测试数据。接着使用 `LogisticRegression` 函数训练了一个逻辑回归模型,最后评估了该模型在测试数据上的性能。
写一段逻辑回归的python代码
好的,以下是一个简单的逻辑回归模型的Python代码:
```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# 生成一个随机分类数据集
X, y = make_classification(n_samples=1000, n_features=2, n_informative=2,
n_redundant=0, n_clusters_per_class=1,
random_state=42)
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建逻辑回归模型并训练
clf = LogisticRegression(random_state=42)
clf.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)
# 可视化决策边界
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, alpha=0.8)
plt.title("Logistic Regression Decision Boundary")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
```
这段代码使用`sklearn`库中的`LogisticRegression`类来创建逻辑回归模型,并使用随机生成的分类数据集进行训练和测试。最后,使用`matplotlib`库将决策边界可视化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)