机器学习(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_)
相关推荐
兴趣使然黄小黄2 小时前
【AI-agent】LangChain开发智能体工具流程
人工智能·microsoft·langchain
出门吃三碗饭3 小时前
Transformer前世今生——使用pytorch实现多头注意力(八)
人工智能·深度学习·transformer
l1t3 小时前
利用DeepSeek改写SQLite版本的二进制位数独求解SQL
数据库·人工智能·sql·sqlite
说私域3 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序FAQ设计及其意义探究
人工智能·小程序
开利网络3 小时前
合规底线:健康产品营销的红线与避坑指南
大数据·前端·人工智能·云计算·1024程序员节
非著名架构师4 小时前
量化“天气风险”:金融与保险机构如何利用气候大数据实现精准定价与投资决策
大数据·人工智能·新能源风光提高精度·疾风气象大模型4.0
熙梦数字化5 小时前
2025汽车零部件行业数字化转型落地方案
大数据·人工智能·汽车
刘海东刘海东5 小时前
逻辑方程结构图语言的机器实现(草稿)
人工智能
亮剑20185 小时前
第2节:程序逻辑与控制流——让程序“思考”
开发语言·c++·人工智能
hixiong1235 小时前
C# OpenCVSharp使用 读光-票证检测矫正模型
人工智能·opencv·c#