机器学习(13):逻辑回归

逻辑回归的输入是线性回归的输出

线性回归的输出是连续值(如 h(w)=w1​x1​+w2​x2​+...+b),而 sigmoid 函数可以将这个连续输出映射到 0, 1 区间,使其具备概率含义。

代码

python 复制代码
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 100)
y = 1 / (1 + np.exp(-x))
plt.plot(x, y)
plt.show()

激活函数sigmoid

python 复制代码
from sklearn.linear_model import LogisticRegression
import pandas as pd 
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
data = pd.read_csv("./src/titanic/titanic.csv")
print(data.columns)

y = data["survived"].values
x = data[["pclass", "age", "sex"]]
# x[["age"]].fillna(x[["age"]].mean(), inplace=True)
# print(y.shape,type(y))
# print(x.head())
x["age"].fillna(x["age"].mean(), inplace=True)#对空值进行处理
x= x.to_dict(orient="records")

# print(x[:5])
dicter = DictVectorizer(sparse=False)
x=dicter.fit_transform(x)
print(dicter.get_feature_names_out())
print(x[:5])

scaler = StandardScaler()
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=42)  
x_train = scaler.fit_transform(x_train)  
x_test = scaler.transform(x_test)

model = LogisticRegression(max_iter=1000,fit_intercept=True)
model.fit(x_train,y_train)

score = model.score(x_test,y_test)
print(score)
python 复制代码
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
x,y = load_iris(return_X_y=True)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=42)

model = LogisticRegression(max_iter=5000)
model.fit(x_train,y_train)

score = model.score(x_test,y_test)
print(score)
x_new=[[5,5,4,2],
       [1,1,4,3]]
y_predict = model.predict(x_new)
y_por = model.predict_proba(x_new)
print(y_predict)
print(y_por)
print(model.coef_)
print(model.intercept_)
相关推荐
回眸&啤酒鸭4 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智4 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅4 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein4 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb4 天前
智能网联汽车安全能力框架
人工智能
龙亘川4 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI4 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai