会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132600个问题
Python 全系列/第二阶段:Python 深入与提高/文件处理 25606楼

import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score

N = 100
x = np.random.rand(N) * 6 - 3   
y = np.sin(x) + np.random.rand(N) * 0.05
# print(y)

#将x转化成一列
x = x.reshape(-1, 1)
# print(x)

dt_reg = DecisionTreeRegressor(criterion='mse', max_depth=3)
dt_reg.fit(x, y)

x_test = np.linspace(-3, 3, 50).reshape(-1,1) 
y_hat = dt_reg.predict(x_test)


plt.plot(x, y, 'y*', label="actual")  
plt.plot(x_test, y_hat, "b-", linewidth=2, label='predict')
plt.legend(loc='upper left')
plt.grid()
# plt.savefig("./temp_decision_tree_regressor")
plt.show()   #必须先保存,再展示

#比较不同深度的决策树
depth = [2, 4, 6, 8, 10]
color='rgbmy'
dt_reg = DecisionTreeRegressor(criterion="mse")   #默认criterion="mse",
plt.plot(x, y, 'ko', label="actual")
x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
y_test = np.sin(x_test) + np.random.rand(N) * 0.05
print(y_test)
for d, c in zip(depth, color):
    dt_reg.set_params(max_depth=d)  #设置超参数
    dt_reg.fit(x, y)
    y_hat = dt_reg.predict(x_test)
    plt.plot(x_test, y_hat, '-', color=c, linewidth=2, label='depth=%d' % d)
    acc_score = accuracy_score(y_test, y_hat)
    print("depth:",d)
    print('acc score:',acc_score)

plt.legend(loc='upper left')
plt.grid(b=True)
plt.savefig("./temp_compare_decision_tree_regressor")
plt.show()

老师,请问, 这里想测试max_depth为多少的时候会过拟合,加了这三行代码,报错了,不知道哪里不对

image.png

image.png

Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/决策树算法 25607楼

 var arr =[21,21,44,54,89,123,77,77];
        var arr1 =[21,21,44,54,89,123,77,77];
        var arr2 =[21,21,44,54,89,123,77,77];

        //1.splice方法删除
        function unique(arr){
            var len=arr.length;
            for(var i=0;i<len;i++){
                for(var j=i+1;j<len;j++){
                    if(arr[i]==arr[j]){
                            arr.splice(j,1);
                            j--;
                            len--;//减少循环次数
                    }
                }
            }
            return arr;
        }
        unique(arr);
        console.log("splice去重后的数组:"+arr);
        
        //2.indexOf方法(不存在的push到新数组中)
        function unique1(arr){
            var resultArr=[];
            for(var i=0;i<arr.length;i++){
                if(resultArr.indexOf(arr[i])==-1){
                    resultArr.push(arr[i]);
                }
            }
            return resultArr;
        }
        unique1(arr1);
        console.log("indexOf方法去重后的数组:"+arr1);

如果直接console.log(unique1(arr1))结果是对的,可我这么写,第一个方法出来结果是对的,第二个为什么还是原数组呢,不知道哪里出问题了

WEB前端全系列/第二阶段:JavaScript编程模块/算法与数据 25609楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 25610楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 25614楼
JAVA 全系列/第十三阶段:分布式文件存储与数据缓存/Redis 25615楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 25616楼

MyDoublyLinkedList <>MyList<>{

    Node<>{
        Node<> Node<> (itemNode<> prevNode<> next) {
            .= item.= prev.= next}
    }
    Node Node (element) {
        .LinkLast(element)}
    (element){
        Node t = .Node<> node = Node<>(elementt).= node(t==){
            .= node}{
            t.= node}
        .++}
    (index) {
        .checkIndex(index)Node<> node = .getNode(index)node.}
    (index){
        (index<||index>.-){
            IndexOutOfBoundsException()}
    }
    Node (index){

        (index<(.>>)){
            Node<> a = .(i=i<=indexi++){
                a = a.}
            a}{
            Node<> a = .(i=.-i>=indexi--){
                a = a.}
            a}
    }
    () {
        .}
    (index) {
        .checkIndex(index)Node<> node = getNode(index)item = node.(index==){
            .= node.node..= }(index==.-){
            .= node.node..= }{
            Node<> node1 = getNode(index-)Node<> node2 = getNode(index+)node1.= node2node2.= node1}
        .--item}
    (element){
        .LinkFirst(element)}
    (element){
        Node<> a = .Node<> node = Node<>(elementa).= node(a==){
            .=node}{
            a.= node}
        .++}
    (element){
       .LinkLast(element)}

    (String[] args) {

        MyDoublyLinkedList<String> myList = MyDoublyLinkedList<>()myList.add()System..println(myList.get())System..println(myList.size())}
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 25619楼
Python 全系列/第七阶段:网页编程基础/CSS3 25620楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637