import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_breast_cancer import torch from torch import nn from sklearn.linear_model import LogisticRegression from torch.utils.data import Dataset from torch.utils.data import DataLoader from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.linear_model import LinearRegression data = load_breast_cancer() data.keys() X = data["data"] y = data["target"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) _mean = X_train.mean(axis=0) _std = X_train.std(axis=0) X_train = (X_train - _mean) / _std X_test = (X_test - _mean) / _std """ KKN 分类算法 """ knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X=X_train, y=y_train) y_pred = knn.predict(X=X_test) acc = (y_pred == y_test).mean() """ 线性回归??? """ lr = LinearRegression() lr.fit(X=X_train, y=y_train) y_pred = lr.predict(X=X_test) y_pred1 = (y_pred >= 0.5).astype(int) (y_pred1 == y_test).mean() def sigmoid(x): return 1 / (1 + np.exp(-x)) x = np.linspace(-10, 10, 100) plt.plot(x, sigmoid(x)) plt.grid() lr1 = LogisticRegression(max_iter=10000) lr1.fit(X=X_train, y=y_train) # 模型 model = nn.Linear(in_features=30, out_features=1) class MyDataset(Dataset): def __init__(self, X, y): self.X = X self.y = y def __getitem__(self, idx): x = torch.tensor(data=self.X[idx], dtype=torch.float32) y = torch.tensor(data=[self.y[idx]], dtype=torch.float32) return x, y def __len__(self): return len(self.X) train_dataset = MyDataset(X=X_train, y=y_train) train_dataloader = DataLoader(dataset=train_dataset, shuffle=True, batch_size=24) loss_fn = nn.MSELoss() optimizer = torch.optim.SGD(params=model.parameters(), lr=1e-3) def train(): for epoch in range(10000): for X, y in train_dataloader: y_pred = model(X) y_pred = torch.sigmoid(y_pred) loss = loss_fn(y_pred, y) loss.backward() optimizer.step() optimizer.zero_grad() print(loss.item()) train() y_pred2 = model(torch.tensor(data=X_test, dtype=torch.float32)).sigmoid_() torch.concatenate(tensors=(1 - y_pred2, y_pred2), dim=1)
day5_yh
最新推荐文章于 2024-09-09 12:17:42 发布