Commit 153bb2f1 by re

直播

parent 34435790
{ {
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 2,
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
...@@ -82,7 +82,7 @@ ...@@ -82,7 +82,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 3,
"outputs": [], "outputs": [],
"source": [ "source": [
"# 划分训练集和测试集 (70%训练, 30%测试)\n", "# 划分训练集和测试集 (70%训练, 30%测试)\n",
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 6,
"outputs": [], "outputs": [],
"source": [ "source": [
"from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.neighbors import KNeighborsClassifier\n",
...@@ -127,7 +127,7 @@ ...@@ -127,7 +127,7 @@
"start_time = time.time()\n", "start_time = time.time()\n",
"# 训练模型\n", "# 训练模型\n",
"knn.fit(X_train, y_train)\n", "knn.fit(X_train, y_train)\n",
"training_time = time.time() - start_time\n" "training_time = time.time() - start_time"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
...@@ -138,13 +138,13 @@ ...@@ -138,13 +138,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 7,
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "0.0018961429595947266" "text/plain": "0.0039865970611572266"
}, },
"execution_count": 22, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -161,7 +161,7 @@ ...@@ -161,7 +161,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 8,
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
...@@ -256,6 +256,75 @@ ...@@ -256,6 +256,75 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## SVM"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"#SVM\n",
"from sklearn.svm import SVC\n",
"\n",
"# 非线性问题使用RBF核\n",
"svc_rbf = SVC(kernel='rbf', C=10, gamma=0.1, probability=True)\n",
"svc_rbf.fit(X_train, y_train)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# 训练集\n",
"y_pred = svc_rbf.predict(X_train)\n",
"\n",
"# 评估指标\n",
"evaluate(y_train, y_pred)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# 非线性问题使用RBF核\n",
"svc_linear = SVC(kernel='linear', C=1.0, probability=True)\n",
"svc_linear.fit(X_train, y_train)\n",
"# 训练集\n",
"y_pred = svc_linear.predict(X_test)\n",
"# 评估指标\n",
"evaluate(y_test, y_pred)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"## 随机森林" "## 随机森林"
], ],
"metadata": { "metadata": {
...@@ -386,7 +455,7 @@ ...@@ -386,7 +455,7 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## SVM" "## 决策树"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
...@@ -397,112 +466,44 @@ ...@@ -397,112 +466,44 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "SVC(C=10, gamma=0.1, probability=True)",
"text/html": "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>SVC(C=10, gamma=0.1, probability=True)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">SVC</label><div class=\"sk-toggleable__content\"><pre>SVC(C=10, gamma=0.1, probability=True)</pre></div></div></div></div></div>"
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#SVM\n",
"from sklearn.svm import SVC\n",
"\n",
"# 非线性问题使用RBF核\n",
"svc_rbf = SVC(kernel='rbf', C=10, gamma=0.1, probability=True)\n",
"svc_rbf.fit(X_train, y_train)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 30,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"测试集准确率: 97.14%\n",
"\n",
"分类报告:\n",
" precision recall f1-score support\n",
"\n",
" setosa 1.00 1.00 1.00 31\n",
" versicolor 1.00 0.92 0.96 37\n",
" virginica 0.93 1.00 0.96 37\n",
"\n",
" accuracy 0.97 105\n",
" macro avg 0.97 0.97 0.97 105\n",
"weighted avg 0.97 0.97 0.97 105\n",
"\n",
"\n",
"混淆矩阵:\n",
"[[31 0 0]\n",
" [ 0 34 3]\n",
" [ 0 0 37]]\n"
]
}
],
"source": [
"# 训练集\n",
"y_pred = svc_rbf.predict(X_train)\n",
"\n",
"# 评估指标\n",
"evaluate(y_train, y_pred)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 31,
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"测试集准确率: 97.78%\n", "测试集准确率: 100.00%\n",
"\n", "\n",
"分类报告:\n", "分类报告:\n",
" precision recall f1-score support\n", " precision recall f1-score support\n",
"\n", "\n",
" setosa 1.00 1.00 1.00 19\n", " setosa 1.00 1.00 1.00 19\n",
" versicolor 1.00 0.92 0.96 13\n", " versicolor 1.00 1.00 1.00 13\n",
" virginica 0.93 1.00 0.96 13\n", " virginica 1.00 1.00 1.00 13\n",
"\n", "\n",
" accuracy 0.98 45\n", " accuracy 1.00 45\n",
" macro avg 0.98 0.97 0.97 45\n", " macro avg 1.00 1.00 1.00 45\n",
"weighted avg 0.98 0.98 0.98 45\n", "weighted avg 1.00 1.00 1.00 45\n",
"\n", "\n",
"\n", "\n",
"混淆矩阵:\n", "混淆矩阵:\n",
"[[19 0 0]\n", "[[19 0 0]\n",
" [ 0 12 1]\n", " [ 0 13 0]\n",
" [ 0 0 13]]\n" " [ 0 0 13]]\n"
] ]
} }
], ],
"source": [ "source": [
"# 非线性问题使用RBF核\n", "from sklearn.tree import DecisionTreeClassifier\n",
"svc_linear = SVC(kernel='linear', C=1.0, probability=True)\n", "\n",
"svc_linear.fit(X_train, y_train)\n", "DT = DecisionTreeClassifier(max_depth=5,\n",
"# 训练集\n", " min_samples_split=5,\n",
"y_pred = svc_linear.predict(X_test)\n", " random_state=42)\n",
"# 评估指标\n", "# 训练模型、预测并评估\n",
"DT.fit(X_train, y_train)\n",
"# 预测\n",
"y_pred = DT.predict(X_test)\n",
"# 评估\n",
"evaluate(y_test, y_pred)" "evaluate(y_test, y_pred)"
], ],
"metadata": { "metadata": {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment