【零基础机器学习】手撕逻辑回归代码🤔
更多代码: gitee主页:https://gitee.com/GZHzzz
博客主页: CSDN:https://blog.csdn.net/gzhzzaa
写在前面
- 本文主要面向零基础入门机器学习,希望大家相互学习,共同进步!😎
show me code,no bb
- 手撕版本逻辑回归
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# data_create
def create_date():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df['test']='0'
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label','test']
data = np.array(df.iloc[:100, [0,1,-2]])
return data[:,:2],data[:,-1]
X,y=create_date()
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3)
class logisticRegressionClassifier:
def __init__(self,max_iter=200,learning_rate=0.01):
self.max_iter=max_iter
self.learning_rate=learning_rate
def sigmoid(self,x):
return 1/(1+np.exp(-x))
def fit (self,X,y):
X = np.hstack((np.ones((X.shape[0],1)),X))
self.weight= np.zeros((X.shape[1],1))
y = np.expand_dims(y,axis =1)
# training
for iter_ in range(self.max_iter):
h = self.sigmoid(np.dot(X,self.weight))
error = y-h
self.weight = self.weight+self.learning_rate * np.dot(X.T, error)
def predict(self,x):
x = np.hstack((np.ones(x.shape[0],1),x))
pred = self.sigmoid(np.dot(x, self.weight))
if pred>0.5:
return 1
else:
return 0
def score(self,X_test,y_test):
right = 0
X_test = np.hstack((np.ones((X_test.shape[0],1)), X_test))
res = np.dot(X_test , self.weight)
for (result,y) in zip(res,y_test):
if (result>0 and y==1) or (result < 0 and y == 0):
right+=1
return right / len(X_test)
- 训练:
instance =logisticRegressionClassifier()
instance.fit(X_train,y_train)
- 画图
x_points = np.arange(4,8)
y_ = -(instance.weight[1]*x_points + instance.weight[0])/instance.weight[2]
plt.plot(x_points,y_)
plt.scatter(X[:50,0],X[:50,1],label = '0')
plt.scatter(X[50:,0],X[50:,1],label = '1')
plt.legend()
- sklearn版本逻辑回归
from sklearn.linear_model import LogisticRegression
quick = LogisticRegression(max_iter=200)
quick.fit(X_train,y_train)
#画图
x_ponits = np.arange(4, 8)
y_ = -(quick.coef_[0][0]*x_ponits + quick.intercept_)/quick.coef_[0][1]
plt.plot(x_ponits, y_)
plt.plot(X[:50, 0], X[:50, 1], 'o', color='blue', label='0')
plt.plot(X[50:, 0], X[50:, 1], 'o', color='orange', label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
- 所有代码均可直接运行!😎
写在最后
十年磨剑,与君共勉!
更多代码:gitee主页:https://gitee.com/GZHzzz
博客主页:CSDN:https://blog.csdn.net/gzhzzaa
- Fighting!😎
while True:
Go life