def compute_raction(pct, allvals): absolute = int(pct/100 * np.sum(allvals)) return '{:.2f}%\n{:d}'.format(pct,absolute),gender_stats_by_user = df_merge.groupby(by='gender')['user_id'].count() labels = ['boy','girl'] values = np.array([gender_stats_by_user[0], gender_stats_by_user[1]]) fig = plt.figure() sub = fig.add_subplot(111) sub.pie(values, labels=labels, startangle=90, autopct=lambda x: compute_raction(x,values)) sub.legend() fig.suptitle('Gender Stats by User', fontsize=16) fig.tight_layout() plt.show(),请举例子解释函数的计算过程
时间: 2024-02-05 09:04:26 浏览: 26
好的,假设我们有以下的调用:
```python
compute_raction(23.4567, [200, 300])
```
那么,函数的计算过程如下:
1. 首先,我们将 `pct` 除以 100,并乘以总数,得到当前扇形的绝对值:
```
absolute = int(23.4567 / 100 * (200 + 300))
= int(0.234567 * 500)
= int(117.2835)
= 117
```
这里我们使用了 `int` 函数将结果转换成了整数。
2. 然后,我们使用 `format` 函数将结果格式化成一个字符串。具体来说,`{:.2f}` 表示将 `pct` 保留两位小数,`{:d}` 表示将 `absolute` 格式化成整数。因此,最终的字符串是:
```
'23.46%\n117'
```
注意,`\n` 表示换行符。
3. 最后,我们将格式化后的字符串返回。
```
return '23.46%\n117'
```
相关问题
简化并解释每行代码:X_train, y_train = load_data("data/ex2data2.txt") plot_data(X_train, y_train[:], pos_label="Accepted", neg_label="Rejected") plt.ylabel('Microchip Test 2') plt.xlabel('Microchip Test 1') plt.legend(loc="upper right") plt.show() mapped_X = map_feature(X_train[:, 0], X_train[:, 1]) def compute_cost_reg(X, y, w, b, lambda_=1): m = X.shape[0] cost = 0 f = sigmoid(np.dot(X, w) + b) reg = (lambda_/(2*m)) * np.sum(np.square(w)) cost = (1/m)np.sum(-ynp.log(f) - (1-y)*np.log(1-f)) + reg return cost def compute_gradient_reg(X, y, w, b, lambda_=1): m = X.shape[0] cost = 0 dw = np.zeros_like(w) f = sigmoid(np.dot(X, w) + b) err = (f - y) dw = (1/m)*np.dot(X.T, err) dw += (lambda_/m) * w db = (1/m) * np.sum(err) return db,dw X_mapped = map_feature(X_train[:, 0], X_train[:, 1]) np.random.seed(1) initial_w = np.random.rand(X_mapped.shape[1]) - 0.5 initial_b = 0.5 lambda_ = 0.5 dj_db, dj_dw = compute_gradient_reg(X_mapped, y_train, initial_w, initial_b, lambda_) np.random.seed(1) initial_w = np.random.rand(X_mapped.shape[1])-0.5 initial_b = 1. lambda_ = 0.01; iterations = 10000 alpha = 0.01 w,b, J_history,_ = gradient_descent(X_mapped, y_train, initial_w, initial_b, compute_cost_reg, compute_gradient_reg, alpha, iterations, lambda_) plot_decision_boundary(w, b, X_mapped, y_train) p = predict(X_mapped, w, b) print('Train Accuracy: %f'%(np.mean(p == y_train) * 100))
这段代码主要实现了一个二分类问题的训练和预测。下面是每一行代码的解释:
```
X_train, y_train = load_data("data/ex2data2.txt")
```
从文件中读取训练数据,将特征存储在X_train中,将标签存储在y_train中。
```
plot_data(X_train, y_train[:], pos_label="Accepted", neg_label="Rejected")
plt.ylabel('Microchip Test 2')
plt.xlabel('Microchip Test 1')
plt.legend(loc="upper right")
plt.show()
```
画出训练数据的散点图,其中Accepted为正例标签,Rejected为负例标签,横坐标为Microchip Test 1,纵坐标为Microchip Test 2。
```
mapped_X = map_feature(X_train[:, 0], X_train[:, 1])
```
将原始特征映射成更高维的特征,以便更好地拟合非线性决策边界。
```
def compute_cost_reg(X, y, w, b, lambda_=1):
m = X.shape[0]
cost = 0
f = sigmoid(np.dot(X, w) + b)
reg = (lambda_/(2*m)) * np.sum(np.square(w))
cost = (1/m)np.sum(-ynp.log(f) - (1-y)*np.log(1-f)) + reg
return cost
```
计算带正则化的逻辑回归代价函数,其中X为特征数据,y为标签,w为权重,b为偏置,lambda_为正则化参数。
```
def compute_gradient_reg(X, y, w, b, lambda_=1):
m = X.shape[0]
cost = 0
dw = np.zeros_like(w)
f = sigmoid(np.dot(X, w) + b)
err = (f - y)
dw = (1/m)*np.dot(X.T, err)
dw += (lambda_/m) * w
db = (1/m) * np.sum(err)
return db,dw
```
计算带正则化的逻辑回归梯度,其中X为特征数据,y为标签,w为权重,b为偏置,lambda_为正则化参数。
```
X_mapped = map_feature(X_train[:, 0], X_train[:, 1])
np.random.seed(1)
initial_w = np.random.rand(X_mapped.shape[1]) - 0.5
initial_b = 0.5
lambda_ = 0.5
dj_db, dj_dw = compute_gradient_reg(X_mapped, y_train, initial_w, initial_b, lambda_)
```
将映射后的特征、权重、偏置和正则化参数传入梯度计算函数,计算出代价函数对权重和偏置的偏导数。
```
np.random.seed(1)
initial_w = np.random.rand(X_mapped.shape[1])-0.5
initial_b = 1.
lambda_ = 0.01; iterations = 10000; alpha = 0.01
w,b, J_history,_ = gradient_descent(X_mapped, y_train, initial_w, initial_b, compute_cost_reg, compute_gradient_reg, alpha, iterations, lambda_)
```
使用梯度下降算法对代价函数进行优化,得到最优的权重和偏置,lambda_为正则化参数,iterations为迭代次数,alpha为学习率。
```
plot_decision_boundary(w, b, X_mapped, y_train)
```
画出决策边界。
```
p = predict(X_mapped, w, b)
print('Train Accuracy: %f'%(np.mean(p == y_train) * 100))
```
使用训练好的模型进行预测,并计算训练精度。
import numpy as np def sigmoid(z): return 1 / (1 + np.exp(-z)) def cost_function(theta, X, y): m = len(y) h = sigmoid(X @ theta) J = -(1/m) * (y.T @ np.log(h) + (1-y).T @ np.log(1-h)) grad = (1/m) * X.T @ (h - y) return J, grad def trust_region_newton_method(X, y, max_iter=100, eta=0.05, delta=0.1): n = X.shape[1] theta = np.zeros((n,1)) J, grad = cost_function(theta, X, y) H = np.eye(n) for i in range(max_iter): # solve trust region subproblem p = np.linalg.solve(H, -grad) if np.linalg.norm(p) <= delta: d = p else: d = delta * p / np.linalg.norm(p) # compute actual reduction and predicted reduction J_new, grad_new = cost_function(theta+d, X, y) actual_reduction = J - J_new predicted_reduction = -grad.T @ d - 0.5 * d.T @ H @ d # update trust region radius rho = actual_reduction / predicted_reduction if rho < 0.25: delta = 0.25 elif rho > 0.75 and np.abs(np.linalg.norm(d) - delta) < 1e-8: delta = min(2delta, eta*np.linalg.norm(theta)) # update parameters if rho > 0: theta += d J, grad = J_new, grad_new H += (grad_new - grad) @ (grad_new - grad).T / ((grad_new - grad).T @ d) # check convergence if np.linalg.norm(grad) < 1e-5: break return theta ——修正代码中的问题,实现信赖域算法求解逻辑回归问题
在代码中,对于更新信赖域半径的部分,有两个问题需要修正:
1. 当rho小于0.25时,应该将delta减小为原来的0.25,而不是将delta赋值为0.25。
2. 当rho大于0.75且步长d的长度与信赖域半径delta的差的绝对值小于1e-8时,应该将delta更新为2倍的delta和eta乘以参数theta的二范数中的较小值,而不是将2delta写成2delta。
下面是修正后的代码:
```
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def cost_function(theta, X, y):
m = len(y)
h = sigmoid(X @ theta)
J = -(1/m) * (y.T @ np.log(h) + (1-y).T @ np.log(1-h))
grad = (1/m) * X.T @ (h - y)
return J, grad
def trust_region_newton_method(X, y, max_iter=100, eta=0.05, delta=0.1):
n = X.shape[1]
theta = np.zeros((n,1))
J, grad = cost_function(theta, X, y)
H = np.eye(n)
for i in range(max_iter):
# solve trust region subproblem
p = np.linalg.solve(H, -grad)
if np.linalg.norm(p) <= delta:
d = p
else:
d = delta * p / np.linalg.norm(p)
# compute actual reduction and predicted reduction
J_new, grad_new = cost_function(theta+d, X, y)
actual_reduction = J - J_new
predicted_reduction = -grad.T @ d - 0.5 * d.T @ H @ d
# update trust region radius
rho = actual_reduction / predicted_reduction
if rho < 0.25:
delta *= 0.25
elif rho > 0.75 and np.abs(np.linalg.norm(d) - delta) < 1e-8:
delta = min(2*delta, eta*np.linalg.norm(theta))
# update parameters
if rho > 0:
theta += d
J, grad = J_new, grad_new
H += (grad_new - grad) @ (grad_new - grad).T / ((grad_new - grad).T @ d)
# check convergence
if np.linalg.norm(grad) < 1e-5:
break
return theta
```
修正后的代码已经可以正确地实现信赖域算法求解逻辑回归问题了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)