机器学习代码实现
参考书籍:机器学习西瓜书——周志华 后续慢慢补全
python代码示例
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
X=2*np.random.rand(10,1)
y=4+3*X+np.random.randn(10,1)
lin_reg=LinearRegression()
lin_reg.fit(X,y)
plt.plot(X,y,"b.")
x_new=[[0],[2]]
y_predict=lin_reg.predict(x_new)
plt.plot(x_new,y_predict,"r-")
plt.axis([0,2,0,15])
plt.show()`
图片示例(每次生成的图像不一样因为是随机的 数据集):
- 3.3对数几率回归——逻辑回归
sklearn中文文档 案例: —后面有时间再补---- —上代码—
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin',
'Normal Nucleoli', 'Mitoses', 'Class']
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",
names=names)
data.head()
data = data.replace(to_replace="?", value=np.NaN)
data = data.dropna()
x = data.iloc[:, 1:10]
x.head()
y = data["Class"]
y.head()
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)
y_predict = estimator.predict(x_test)
print(y_predict)
estimator.score(x_test, y_test)
from sklearn.metrics import roc_auc_score
ret = sklearn.metrics.classification_report(y_test, y_predict, labels=(2,4), target_names=("良性", "恶性"))
print(ret)
然后可以得到这样的分类评估报告图:
y_test = np.where(y_test > 2.5, 1, 0)
print("AUC指标:", sklearn.metrics.roc_auc_score(y_test, y_predict))
问题留言: 我先溜了
-------------2022.1.27--------------------------------
|