1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Logistic Regression(逻辑回归) +python3.6(pycharm)实现

Logistic Regression(逻辑回归) +python3.6(pycharm)实现

时间:2021-10-23 09:23:05

相关推荐

Logistic Regression(逻辑回归) +python3.6(pycharm)实现

数学基础知识略过,可自行查询探究。

遇到的bugs:

1.AttributeError: module 'scipy' has no attribute '__version__'

解决办法:installed packages: 'scipy' :左上角File-- settings--选择 Project Interpreter--点击右上角 绿色 加号--输出 scipy--左下角 Install Package--哦了

2.AttributeError: 'LogisticRegression' object has no attribute 'sigmoid'

解决办法:

1. 命名py脚本时,不要与python预留字,模块名等相同

2.删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件;在已经生成.pyc文件的情况下,若代码不更新,运行时依旧会走pyc,所以要删除.pyc文件),重新运行代码;或者找一个可以运行代码的环境,拷贝替换当前机器的.pyc文件即可

具体代码如下:

#!/usr/bin/env python# _*_coding:utf-8 _*_#@Time :/3/30 23:03#@Author :Niutianzhuang#@FileName: test_Logistic Regression.py#@Software: PyCharmimport numpy as npfrom sklearn.model_selection import train_test_splitfrom sklearn.datasets import make_blobsimport matplotlib.pyplot as pltnp.random.seed(123)#form ipython import get_ipython#get_ipython().run_line_magic('matplotlib', 'inline')#Dataset: a simple toy dataset of two classesX, y_true = make_blobs(n_samples = 1000, centers = 2)fig = plt.figure(figsize = (8,6))plt.scatter(X[:,0], X[:,1], c = y_true)plt.title('Toy Dataset')plt.xlabel('First feature')plt.ylabel('Second feature')plt.show()#Reshape targets to get column vector with shape (n_samples, 1)y_true = y_true[:, np.newaxis]#Split the data into a training and test setX_train, X_test, y_train, y_test = train_test_split(X,y_true)print(f'Shape X_train: {X_train.shape}')print(f'Shape y_train: {y_train.shape}')print(f'Shape X_test: {X_test.shape}')print(f'Shape y_test: {y_test.shape}')#Logistic Regression classclass test_LogisticRegression:def __init__(self):passdef sigmod(self, a):''':param a::return:'''return 1 / (1 + np.exp(-a))def train(self, X, y_true, n_iters, learning_rate):'''Trains the logistic regression model on given data X and targets y:param X::param y_true::param n_iters::param learning_rate::return:'''#Step 0: Initializing the parametersn_samples, n_features = X.shapeself.s((weights = np.zeron_features, 1))self.bias = 0costs = []for i in range(n_iters):#Step 1 and 2: Computing a linear combination of the input features and weights#applying the sigmoid activation functiony_predict = self.sigmoid(np.dot(X, self.weights) + self.bias)#Step 3: Computing the cost over the whole training setcost = (- 1 / n_samples) * np.sum(y_true * np.log(y_predict)) + (1-y_true) * (np.log(1 - y_predict))#Step 4: Computing the gradientsdw = (1 / n_samples) * np.dot(X.T, (y_predict - y_true))db = (1 / n_samples) * np.sum(y_predict - y_true)#Step 5: UPdating the parametersself.weights = self.weights - learning_rate * dwself.bias = self.bias - learning_rate *dbcosts.append(cost)if i % 100 == 0:print(f'Cost after iteration {i}: {cost}')return self.weights, self.bias, costsdef predict(self, X):'''Predicting binary labels for a set of examples X:param self::param X::return:'''y_predict = self.sigmoid(np.dot(X,self.weights)) + self.biasy_predict_labels = [1 if elem > 0.5 else 0 for elem in y_predict]return np.array(y_predict_labels) [:, np.newaxis]#Initializing and training the modelregressor = test_LogisticRegression()w_trained, b_trained, costs = regressor.train(X_train, y_train, n_iters=600, learning_rate=0.009)fig_cost = plt.figure(figsize=(8,6))plt.plot(np.arange(600), costs)plt.title('Development of cost over training')plt.xlabel('Number of iterations')plt.ylabel('Cost')plt.show()#Testing the modely_p_train = regressor.predict(X_train)y_p_test = regressor.predict(X_test)print(f'train accuracy: {100 - np.mean(np.abs(y_p_train - y_train))} %')print(f'test accuracy: {100 - np.mean(np.abs(y_p_test - y_test))} %')

.pcy 知识扩展

pyc文件是用来保存python虚拟机编译生成的byte code的。在python的运行过程中,如果遇到import首先在设定好的path中寻找对应的.pyc或者.dll 文件。

pyc是由py文件经过编译后二进制文件,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python的虚 拟机来执行的。pyc的内容,是跟python的版本相关的,不同版本编译后的pyc文件是不同的,2.5编译的pyc文件,2.4版本的 python是无法执行的。pyc文件也是可以反编译的,不同版本编译后的pyc文件是不同。

参考/carolzhang8406/article/details/6342174

/python-script-error-attributeerror-module-object-has-no-attribute-solve-method.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。