《机器学习》课后题3.4
这次选了梯度下降法,结论就是10折法跟留一法差不多,正确率都挺高的!没有用网上的数据,用了sklearn里面自带的iris花的数据,可能数据比较好,所以出来结果也比较好!
python代码
from math import exp
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
def create_data():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
data = np.array(df.iloc[:100, [0, 1, -1]])
# print(data)
return data[:, :2], data[:, -1]
def tenfolddata(positive_data, negative_data):
fold_data = []
for i in range(10):
pos_temp = positive_data[i * 5: (i+1) * 5].tolist()
neg_temp = negative_data[i * 5: (i+1) * 5].tolist()
temp = pos_temp + neg_temp
fold_data += temp
return np.array(fold_data)
x, y = create_data()
x_negative = x[:50]
x_positive = x[50:]
x_tenfold_test = tenfolddata(x_positive, x_negative)
y_tenfold_test = np.array([[1]]