iris数据集的读取,训练,预测

一 读取UCI数据集iris.data中数据方法

1.将数据分成五组,一列一列读进列向量当中

>> [attrib1, attrib2, attrib3, attrib4, class] = textread('data\iris.data', '%f%f%f%f%s', 'delimiter', ',');

 

2.将前面的4组浮点型数据,整合进一个矩阵当中,成为一个column_length*4类型的矩阵

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我来给出实现步骤。 首先,我们需要加载iris数据集: ```python from sklearn.datasets import load_iris iris = load_iris() X = iris.data y = iris.target ``` 接下来,我们将数据集分为训练集和测试集: ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ``` 然后,我们使用共轭梯度算法进行训练: ```python from sklearn.linear_model import LogisticRegression from scipy.optimize import fmin_cg def logistic_regression_cost(theta, X, y, lambda_val): m = len(y) h = sigmoid(X.dot(theta)) J = (-1 / m) * ((y.T.dot(np.log(h))) + ((1 - y).T.dot(np.log(1 - h)))) + (lambda_val / (2 * m)) * np.sum(np.square(theta[1:])) grad = (1 / m) * X.T.dot(h - y) + (lambda_val / m) * np.concatenate([[0], theta[1:]]) return J, grad def sigmoid(z): return 1 / (1 + np.exp(-z)) def fit(X, y, theta, lambda_val): result = fmin_cg(f=logistic_regression_cost, x0=theta, args=(X, y, lambda_val), maxiter=500) return result lambda_val = 0.1 theta = np.zeros(X_train.shape[1] + 1) X_train = np.concatenate([np.ones((X_train.shape[0], 1)), X_train], axis=1) theta = fit(X_train, y_train, theta, lambda_val) ``` 最后,我们可以使用训练好的模型进行预测: ```python X_test = np.concatenate([np.ones((X_test.shape[0], 1)), X_test], axis=1) y_pred = np.round(sigmoid(X_test.dot(theta))) ``` 完整代码如下: ```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from scipy.optimize import fmin_cg import numpy as np def logistic_regression_cost(theta, X, y, lambda_val): m = len(y) h = sigmoid(X.dot(theta)) J = (-1 / m) * ((y.T.dot(np.log(h))) + ((1 - y).T.dot(np.log(1 - h)))) + (lambda_val / (2 * m)) * np.sum(np.square(theta[1:])) grad = (1 / m) * X.T.dot(h - y) + (lambda_val / m) * np.concatenate([[0], theta[1:]]) return J, grad def sigmoid(z): return 1 / (1 + np.exp(-z)) def fit(X, y, theta, lambda_val): result = fmin_cg(f=logistic_regression_cost, x0=theta, args=(X, y, lambda_val), maxiter=500) return result iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) lambda_val = 0.1 theta = np.zeros(X_train.shape[1] + 1) X_train = np.concatenate([np.ones((X_train.shape[0], 1)), X_train], axis=1) theta = fit(X_train, y_train, theta, lambda_val) X_test = np.concatenate([np.ones((X_test.shape[0], 1)), X_test], axis=1) y_pred = np.round(sigmoid(X_test.dot(theta))) print("Accuracy:", np.mean(y_pred == y_test)) ``` 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值