135人阅读 评论(0)

# Classification分析——鸢尾花数据集

Scikit-Learn自带鸢尾花数据集，可使用datasets.load_iris()载入。

• data——每行是某个鸢尾花的花萼长度、花萼宽度、花瓣长度、花瓣宽度。
• target——第n个数据分别表示data段第n行数据所对应的鸢尾花类别编号（共3类）。

from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from numpy import *

iris_data = iris_dataset.data
iris_target = iris_dataset.target

# split data and target into training set and testing set
# 80% training, 20% testing
x_train, x_test, y_train, y_test = train_test_split(iris_data, iris_target, test_size = 0.2)
# construct SVC by using rbf as kernel function
SVC_0 = SVC(kernel = 'rbf')
SVC_0.fit(x_train, y_train)

predict = SVC_0.predict(x_test)
right = sum(predict == y_test)
# accuracy rate
print("%f%%" % (right * 100.0 / predict.shape[0]))

from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from numpy import *

def data_svc_test(data, target, index):
x_train = vstack((data[0: index], data[index + 1: -1]))
x_test = data[index]
y_train = hstack((target[0: index], target[index + 1: -1]))
y_test = target[index]
SVC_0 = SVC(kernel = 'rbf')
SVC_0.fit(x_train, y_train)
predict = SVC_0.predict(x_test)
return predict == y_test

iris_data = iris_dataset.data
iris_target = iris_dataset.target
length = iris_target.shape[0]
right = 0
for i in range(0, length):
right += data_svc_test(iris_data, iris_target, i)

# accuracy rate
print("%f%%" % (right * 100.0 / length))

# Regression分析——波士顿房价数据集

Scikit-learn自带波士顿房价集，该数据集来源于1978年美国某经济学杂志上，可由datasets.load_boston()载入。该数据集包含若干波士顿房屋的价格及其各项数据，每个数据项包含14个数据，分别是房屋均价及周边犯罪率、是否在河边等相关信息，其中最后一个数据是房屋均价。

from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVR
# preprocessing function
from sklearn.preprocessing import StandardScaler
from numpy import *

house_data = house_dataset.data
house_price = house_dataset.target
x_train, x_test, y_train, y_test = train_test_split(house_data, house_price, test_size = 0.2)
# f(x) = (x - means) / standard deviation
scaler = StandardScaler()
scaler.fit(x_train)
# standardization
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# construct SVR model
svr = SVR(kernel = 'rbf')
svr.fit(x_train, y_train)
y_predict = svr.predict(x_test)
result = hstack((y_test.reshape(-1, 1), y_predict.reshape(-1, 1)))
print(result)

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：219997次
• 积分：2332
• 等级：
• 排名：第16935名
• 原创：8篇
• 转载：245篇
• 译文：0篇
• 评论：4条
阅读排行
评论排行
最新评论