1 Star 0 Fork 0

whitebear-coder/Machine_Learning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Random_Forest 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
whitebear-coder 提交于 2021-03-22 20:50 . Create Random_Forest
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
data = pd.read_csv("iris.csv")
data = np.array(data) # 变成array形式
np.random.shuffle(data)
percent = 0.5
def train_test_splits(data, percent):
train_size = int(data.shape[0]*percent)
return data[:train_size], data[train_size:]
train, test = train_test_splits(data, percent)
x_train = train[:, 0:train.shape[1] - 1]
y_train = train[:, train.shape[1]-1]
x_test = test[:, 0:test.shape[1] - 1]
y_test = test[:, test.shape[1]-1]
# 利用随机森林进行训练
forest = DecisionTreeClassifier()
forest.fit(x_train, y_train)
#
num = y_test.shape[0]
score = forest.score(x_test, y_test)
result = forest.predict(x_test)
print(y_test, result)
plt.figure()
# 只取前一百条可视化,因为数据太多了
plt.plot(np.arange(num), y_test, "go-", label="True value")
plt.plot(np.arange(num), result, "ro-", label="Predict value")
plt.title(f"RandomForest---score:{score}")
# plt.legend(loc="best")
plt.show()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/white_mai/Machine_Learning.git
git@gitee.com:white_mai/Machine_Learning.git
white_mai
Machine_Learning
Machine_Learning
main

搜索帮助