数据:
M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15 M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7 F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9 M,0.44,0.365,0.125,0.516,0.2155,0.114,0.155,10 I,0.33,0.255,0.08,0.205,0.0895,0.0395,0.055,7 I,0.425,0.3,0.095,0.3515,0.141,0.0775,0.12,8 F,0.53,0.415,0.15,0.7775,0.237,0.1415,0.33,20 F,0.545,0.425,0.125,0.768,0.294,0.1495,0.26,16 M,0.475,0.37,0.125,0.5095,0.2165,0.1125,0.165,9 F,0.55,0.44,0.15,0.8945,0.3145,0.151,0.32,19 F,0.525,0.38,0.14,0.6065,0.194,0.1475,0.21,14 M,0.43,0.35,0.11,0.406,0.1675,0.081,0.135,10 M,0.49,0.38,0.135,0.5415,0.2175,0.095,0.19,11 F,0.535,0.405,0.145,0.6845,0.2725,0.171,0.205,10 F,0.47,0.355,0.1,0.4755,0.1675,0.0805,0.185,10 M,0.5,0.4,0.13,0.6645,0.258,0.133,0.24,12 I,0.355,0.28,0.085,0.2905,0.095,0.0395,0.115,7 F,0.44,0.34,0.1,0.451,0.188,0.087,0.13,10 M,0.365,0.295,0.08,0.2555,0.097,0.043,0.1,7 M,0.45,0.32,0.1,0.381,0.1705,0.075,0.115,9 M,0.355,0.28,0.095,0.2455,0.0955,0.062,0.075,11 I,0.38,0.275,0.1,0.2255,0.08,0.049,0.085,10 F,0.565,0.44,0.155,0.9395,0.4275,0.214,0.27,12 F,0.55,0.415,0.135,0.7635,0.318,0.21,0.2,9 F,0.615,0.48,0.165,1.1615,0.513,0.301,0.305,10
代码:
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error,mean_absolute_error # 评价指标
from sklearn.linear_model import LogisticRegression
data_x=[]
data_y=[]
with open("abalone.data",encoding="utf-8") as f :
f=f.readlines()
for line in f:
line=line.split("\n")[:-1][0].split(",")[1:]
line=[float(i) for i in line]
# print(line)
data_x.append(line[:-1])
data_y.append(line[-1])
# 对样本进行随意切割,得到训练集和验证集
x_train,x_test,y_train,y_test = train_test_split(np.array(data_x),np.array(data_y),test_size=0.2)
# knn算法
knn = KNeighborsRegressor()
knn.fit(x_train, y_train)
test_pred = knn.predict(x_test) # 进行预测
print("knn算法 ")
print(test_pred[:10])
print(y_test[:10])
print('mean_squared_error:', mean_squared_error(y_test, test_pred,sample_weight=None, multioutput='uniform_average'))# mse
print("mean_absolute_error:",mean_absolute_error(y_test, test_pred,sample_weight=None,multioutput='uniform_average'))# mae
# svm算法
svm = SVC()
svm.fit(x_train, y_train)
test_pred = svm.predict(x_test)
print("svm算法 ")
print(test_pred[:10])
print(y_test[:10])
print('mean_squared_error:', mean_squared_error(y_test, test_pred,sample_weight=None, multioutput='uniform_average'))# mse
print("mean_absolute_error:",mean_absolute_error(y_test, test_pred,sample_weight=None,multioutput='uniform_average'))# mae
# 逻辑回归算法
lr = LogisticRegression()
lr.fit(x_train, y_train)
test_pred = lr.predict(x_test)
print("逻辑回归算法 ")
print(test_pred[:10])
print(y_test[:10])
print('mean_squared_error:', mean_squared_error(y_test, test_pred,sample_weight=None, multioutput='uniform_average'))# mse
print("mean_absolute_error:",mean_absolute_error(y_test, test_pred,sample_weight=None,multioutput='uniform_average'))# mae
结果:
所有的数据加代码:
机器学习实战鲍鱼年龄预测knnsvm逻辑回归有代码可运行-机器学习文档类资源-CSDN下载