贵州住房和建设厅网站网站规划有什么意义

当前位置: 首页 > news >正文

贵州住房和建设厅网站,网站规划有什么意义,wordpress插件的用法,织梦 网站参考代码 结合自己的理解#xff0c;添加注释。 代码 导入相关的库 import numpy as np import pandas as pd import matplotlib from matplotlib import pyplot as plt from sklearn import linear_model导入数据#xff0c;进行数据处理和特征工程

1.数据处理#x…参考代码 结合自己的理解添加注释。

代码 导入相关的库 import numpy as np import pandas as pd import matplotlib from matplotlib import pyplot as plt from sklearn import linear_model导入数据进行数据处理和特征工程

1.数据处理特征工程

data_path watermelon3_0_Ch.csv data pd.read_csv(data_path).values

取所有行的第10列(标签列)进行判断

is_good data[:,9] 是 is_bad data[:,9] 否

按照数据集3.0α强制转换数据类型

X data[:,7:9].astype(float) y data[:,9] y[y是] 1 y[y否] 0 y y.astype(int)定义若干需要使用的函数 y 1 1 e − x y \frac{1}{1e^{-x}} y1e−x1​ def sigmoid(x):构造对数几率函数它是一种sigmoid函数s 1/(1np.exp(-x))return sℓ ( β ) ∑ i 1 m ( − y i β T x ^ i l n ( 1 e β T x ^ i ) ) \ell(\beta) \sum{i1}^{m}(-y{i}\beta^{T} \hat{x}{i} ln(1e^{\beta^{T} \hat{x}{i}})) ℓ(β)i1∑m​(−yi​βTx^i​ln(1eβTx^i​)) def J_cost(X,y,beta)::param X: sample array, shape(n_samples, n_features):param y: array-like, shape (n_samples,):param beta: the beta in formula 3.27 , shape(n_features 1, ) or (n_features 1, 1):return: the result of formula 3.27# 构造xhatnp.c 用于连接两个矩阵规模是X.row行X.column1列Xhat np.c[X, np.ones((X.shape[0],1))]# β和y均reshape为1列规模是X.column1行1列beta beta.reshape(-1,1)y y.reshape(-1,1)# 计算最大化似然函数的相反数L_beta -y * np.dot(X_hat,beta) np.log(1np.exp(np.dot(X_hat,beta)))# 返回式3.27的结果return L_beta.sum()β ( w ; b ) \beta (w; b) β(w;b) def initialize_beta(column):初始化β对应式3.26的假设规模是X.column1行1列xhat规模是17行X.column1列# numpy.random.randn(d0,d1,…,dn)# randn函数返回一个或一组样本具有标准正态分布。标准正态分布又称为u分布是以0为均值、以1为标准差的正态分布记为N01# dn表格每个维度# 返回值为指定维度的arraybeta np.random.randn(column1,1)*0.51return beta∂ ℓ ( β ) ∂ β − ∑ i 1 m x ^ i ( y i − p 1 ( x ^ i ; β ) ) \frac{\partial \ell(\beta)}{\partial \beta} -\sum{i1}^{m}\hat{x}{i}(y{i}-p{1}(\hat{x}{i};\beta)) ∂β∂ℓ(β)​−i1∑m​x^i​(yi​−p1​(x^i​;β)) def gradient(X,y,beta):compute the first derivative of J(i.e. formula 3.27) with respect to beta i.e. formula 3.30计算式3.27的一阶导数—————————————————-:param X: sample array, shape(n_samples, n_features):param y: array-like, shape (n_samples,):param beta: the beta in formula 3.27 , shape(n_features 1, ) or (n_features 1, 1):return:# 构造xhatnp.c 用于连接两个矩阵规模是X.row行X.column1列Xhat np.c[X, np.ones((X.shape[0],1))]# β和y均reshape为1列规模是X.column1行1列beta beta.reshape(-1,1)y y.reshape(-1,1)# 计算p1(X_hat,beta)p1 sigmoid(np.dot(X_hat,beta))gra (-Xhat*(y-p1)).sum(0)return gra.reshape(-1,1) ∂ 2 ℓ ( β ) ∂ β ∂ β T ∑ i 1 m x ^ i x ^ i T p 1 ( x ^ i ; β ) ( 1 − p 1 ( x ^ i ; β ) ) \frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T} \sum{i1}^{m}\hat{x}{i}\hat{x}{i}^Tp{1}(\hat{x}{i};\beta)(1-p{1}(\hat{x}{i};\beta)) ∂β∂βT∂2ℓ(β)​i1∑m​x^i​x^iT​p1​(x^i​;β)(1−p1​(x^i​;β)) def hessian(X,y,beta):compute the second derivative of J(i.e. formula 3.27) with respect to beta i.e. formula 3.31计算式3.27的二阶导数———————————-:param X: sample array, shape(n_samples, n_features):param y: array-like, shape (n_samples,):param beta: the beta in formula 3.27 , shape(n_features 1, ) or (n_features 1, 1):return:# 构造xhatnp.c 用于连接两个矩阵规模是X.row行X.column1列Xhat np.c[X, np.ones((X.shape[0],1))]# β和y均reshape为1列规模是X.column1行1列beta beta.reshape(-1,1)y y.reshape(-1,1)# 计算p1(X_hat,beta)p1 sigmoid(np.dot(X_hat,beta))m,nX.shape# np.eye()返回的是一个二维2的数组(N,M)对角线的地方为1其余的地方为0.P np.eye(m)p1(1-p1)assert P.shape[0] P.shape[1]# X_hat.T是X_hat的转置return np.dot(np.dot(X_hat.T,P),X_hat)使用梯度下降法求解 def update_parameters_gradDesc(X,y,beta,learning_rate,num_iterations,print_cost):update parameters with gradient descent methodfor i in range(num_iterations):grad gradient(X,y,beta)beta beta - learning_rate*grad# print_cost为true时并且迭代为10的倍数时打印本次迭代的costif (i%100)print_cost:print({}th iteration, cost is {}.format(i,J_cost(X,y,beta)))return betadef logistic_model(X,y,print_costFalse,methodgradDesc,learning_rate1.2,num_iterations1000)::param method: str gradDescorNewton# 得到X的规模row,column X.shape# 初始化βbeta initialize_beta(column)if method gradDesc:return update_parameters_gradDesc(X,y,beta,learning_rate,num_iterations,print_cost)elif method Newton:return update_parameters_newton(X,y,beta,print_cost,num_iterations)else:raise ValueError(Unknown solver %s % method)可视化结果

1.可视化数据点

设置字体为楷体

matplotlib.rcParams[font.sans-serif] [KaiTi] plt.scatter(data[:, 7][is_good], data[:, 8][is_good], cb, markero) #c参数是颜色marker是标记 plt.scatter(data[:, 7][is_bad], data[:, 8][is_bad], cr, markerx)

设置横轴坐标标题

plt.xlabel(密度) plt.ylabel(含糖量)# 2.可视化自己写的模型

学习得到模型

beta logistic_model(X,y,print_costTrue,methodgradDesc,learning_rate0.3, num_iterations1000)

得到模型参数及偏置截距

w1, w2, intercept beta x1 np.linspace(0, 1) y1 -(w1 * x1 intercept) / w2 ax1, plt.plot(x1, y1, labelrmy_logistic_gradDesc)# 3.可视化sklearn的对率回归模型进行对比 lr linear_model.LogisticRegression(solverlbfgs, C1000) # 注意sklearn的逻辑回归中C越大表示正则化程度越低。 lr.fit(X, y) lrbeta np.c[lr.coef, lr.intercept] print(J_cost(X, y, lr_beta))

可视化sklearn LogisticRegression 模型结果

w1_sk, w2sk lr.coef[0, :] x2 np.linspace(0, 1) y2 -(w1sk * x2 lr.intercept) / w2 ax2, plt.plot(x2, y2, labelrsklearn_logistic) plt.legend(locupper right) plt.show()可视化结果如下