codes

from sklearn import datasets
from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# Load dataset and pick up the needed
iris = datasets.load_iris()
X = iris.data[:, :2]
Y = iris.target
B = iris.feature_names[:2]

# Initialize the classifier
logreg = linear_model.LogisticRegression(C=1e5)
logreg.fit(X, Y)
coef = logreg.coef_
intercept = logreg.intercept_

# Matrix build
res = .05
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, res), np.arange(y_min, y_max, res))
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])
print(logreg.score(X, Y))
Z = Z.reshape(xx.shape)

# Visualization
plt.figure(1, figsize=(8, 6))
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xlabel(B[0])
plt.ylabel(B[1])
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Set1)
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Set1)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import linear_model
from mpl_toolkits.mplot3d import Axes3D

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def linear_com(data, c, i):
    return data[0] * c[0] + data[1] * c[1] + i

# Load dataset and pick up the needed
iris = datasets.load_iris()
X = iris.data[:, :2]
Y = iris.target
B = iris.feature_names[:2]

# Initialize the classifier
logreg = linear_model.LogisticRegression(C=1e5)
logreg.fit(X, Y)
coef = logreg.coef_
intercept = logreg.intercept_

# Matrix build
res = .1
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, res), np.arange(y_min, y_max, res))
xi = xx.ravel()
yi = yy.ravel()
tot = len(xi)
val = []
for i in range(3):
    tmp = []
    for p in range(tot):
        tmp.append(sigmoid(linear_com([xi[p], yi[p]], coef[i], intercept[i])))
    tmp = np.array(tmp)
    tmp = tmp.reshape(xx.shape)
    val.append(tmp)
val = np.array(val)

# Visualization
fig = plt.figure(1, figsize=(15, 4))
for i in range(3):
    ax = fig.add_subplot(1, 3, i + 1, projection='3d')
    ax.plot_surface(xx, yy, val[i], cmap=plt.cm.coolwarm, linewidth=0, antialiased=False)
    ax.set_xlabel(B[0])
    ax.set_ylabel(B[1])
    ax.set_zlabel('possibility of type ' + str(i))
plt.show()

import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# Synthesize the normal distributed data with Gaussian noise
n_samples = 30
np.random.seed(0)
X = np.random.normal(size=n_samples)
X[X > 0] *= 4
X += .3 * np.random.normal(size=n_samples)
'''
The author put the classification before noise added.
What's the difference and why?
'''
Y = (X > 0).astype(np.float)
X_test = np.linspace(-5, 10, 300)

# Initialize the classifier
X = X[:, np.newaxis]
logreg = linear_model.LogisticRegression(C=1e5)
logreg.fit(X, Y)

# Visualization
plt.figure(1, figsize=(8, 6))
plt.ylabel('Class')
plt.xlabel('X')
plt.xticks(range(-5, 10))
plt.yticks([0, 0.5, 1])
plt.ylim(-.25, 1.25)
plt.xlim(-4, 10)
plt.scatter(X.ravel(), Y, c='k')
coef = logreg.coef_.ravel()[0]
intercept = logreg.intercept_.ravel()[0]
plt.plot(X_test, sigmoid(coef * X_test + intercept), label='Regression Curve')
plt.axhline(.5, c='.5', linestyle='--', label='Decision Boundary')
font = {
    'family' : 'serif',
    'color' : 'k',
    'weight' : 'normal',
    'size' : 12
}
plt.text(5, 0.25,
         'Reg(x) = Sigmoid(%.2f * x + %.2f)'%(coef, intercept),
         horizontalalignment='center', fontdict=font)
plt.legend()
plt.show()

对不起,由于这是一个文本环境,我无法直接查看附件或者运行Jupyter notebook脚本。不过,我可以帮你理解如何在Python中使用scikit-learn库来进行逻辑回归,如果你需要手动计算梯度下降法求解最优参数θ(theta),通常会按照以下步骤: 首先,你需要读取数据并将其分为特征和目标变量。假设`ex2data1.txt`是一个CSV格式的数据,你可以使用pandas库加载它: ```python import pandas as pd # 读取数据 data = pd.read_csv('ex2data1.txt', header=None) X = data.iloc[:, :-1] # 特征矩阵 y = data.iloc[:, -1] # 目标变量 ``` 然后,你需要初始化θ,设置学习率α,以及迭代次数。接下来,你可以创建逻辑回归模型,并通过梯度下降算法优化θ: ```python from sklearn import datasets from sklearn.linear_model import LogisticRegression # 初始化θ和参数 n_samples, n_features = X.shape initial_theta = np.zeros(n_features + 1) # 假设只有一个截距项 alpha = 0.01 # 学习率 max_iters = 1500 # 最大迭代次数 def compute_cost(X, y, theta): h = sigmoid(np.dot(X, theta)) m = len(y) J = (-1/m) * (np.sum(y * np.log(h)) + np.sum((1 - y) * np.log(1 - h))) return J def gradient_descent(X, y, initial_theta, alpha, max_iters): m = len(y) thetas = [initial_theta] for _ in range(max_iters): cost = compute_cost(X, y, thetas[-1]) grad = (1/m) * np.dot(X.T, (sigmoid(np.dot(X, thetas[-1])) - y)) thetas.append(thetas[-1] - alpha * grad) return thetas[-1], cost best_theta, cost = gradient_descent(X.values, y.values, initial_theta, alpha, max_iters) ``` 这里的`sigmoid`函数用于将线性预测转换成概率形式。 最后,`best_theta`就是你想要的最优参数θ。 注意,如果实际目的是完成ex2的练习,Scikit-learn有一个内置的逻辑回归模型,可以直接拟合数据而无需手动计算θ。使用它会更简单,如下所示: ```python logreg = LogisticRegression() logreg.fit(X, y) best_theta_logreg = logreg.coef_.reshape(-1) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值