当前位置: 首页 > news >正文

【Python机器学习】实验04(1) 多分类(基于逻辑回归)实践

文章目录

  • 多分类以及机器学习实践
    • 如何对多个类别进行分类
      • 1.1 数据的预处理
      • 1.2 训练数据的准备
      • 1.3 定义假设函数,代价函数,梯度下降算法(从实验3复制过来)
      • 1.4 调用梯度下降算法来学习三个分类模型的参数
      • 1.5 利用模型进行预测
      • 1.6 评估模型
      • 1.7 试试sklearn
    • 实验4(1) 请动手完成你们第一个多分类问题,祝好运!完成下面代码
      • 2.1 数据读取
      • 2.2 训练数据的准备
      • 2.3 定义假设函数、代价函数和梯度下降算法
      • 2.4 学习这四个分类模型
      • 2.5 利用模型进行预测
      • 2.6 计算准确率

多分类以及机器学习实践

如何对多个类别进行分类

Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理。Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。数据集包含150个数据样本,分为3类,每类50个数据,每个数据包含4个属性。可通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类。

iris以鸢尾花的特征作为数据来源,常用在分类操作中。该数据集由3种不同类型的鸢尾花的各50个样本数据构成。其中的一个种类与另外两个种类是线性可分离的,后两个种类是非线性可分离的。

该数据集包含了4个属性:
Sepal.Length(花萼长度),单位是cm;
Sepal.Width(花萼宽度),单位是cm;
Petal.Length(花瓣长度),单位是cm;
Petal.Width(花瓣宽度),单位是cm;

种类:Iris Setosa(山鸢尾)、Iris Versicolour(杂色鸢尾),以及Iris Virginica(维吉尼亚鸢尾)。

1.1 数据的预处理

import sklearn.datasets as datasets
import pandas as pd
import numpy as np
data=datasets.load_iris()
data
{'data': array([[5.1, 3.5, 1.4, 0.2],[4.9, 3. , 1.4, 0.2],[4.7, 3.2, 1.3, 0.2],[4.6, 3.1, 1.5, 0.2],[5. , 3.6, 1.4, 0.2],[5.4, 3.9, 1.7, 0.4],[4.6, 3.4, 1.4, 0.3],[5. , 3.4, 1.5, 0.2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, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),'frame': None,'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n    :Number of Instances: 150 (50 in each of three classes)\n    :Number of Attributes: 4 numeric, predictive attributes and the class\n    :Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm\n        - class:\n                - Iris-Setosa\n                - Iris-Versicolour\n                - Iris-Virginica\n                \n    :Summary Statistics:\n\n    ============== ==== ==== ======= ===== ====================\n                    Min  Max   Mean    SD   Class Correlation\n    ============== ==== ==== ======= ===== ====================\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)\n    ============== ==== ==== ======= ===== ====================\n\n    :Missing Attribute Values: None\n    :Class Distribution: 33.3% for each of 3 classes.\n    :Creator: R.A. Fisher\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n    :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature.  Fisher\'s paper is a classic in the field and\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant.  One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n   - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n     Mathematical Statistics" (John Wiley, NY, 1950).\n   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n     Structure and Classification Rule for Recognition in Partially Exposed\n     Environments".  IEEE Transactions on Pattern Analysis and Machine\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions\n     on Information Theory, May 1972, 431-433.\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II\n     conceptual clustering system finds 3 classes in the data.\n   - Many, many more ...','feature_names': ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)'],'filename': 'iris.csv','data_module': 'sklearn.datasets.data'}
data_x=data["data"]
data_y=data["target"]
data_x.shape,data_y.shape
((150, 4), (150,))
data_y=data_y.reshape([len(data_y),1])
data_y
array([[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]])
#法1 ,用拼接的方法
data=np.hstack([data_x,data_y])
#法二: 用插入的方法
np.insert(data_x,data_x.shape[1],data_y,axis=1)
array([[5.1, 3.5, 1.4, ..., 2. , 2. , 2. ],[4.9, 3. , 1.4, ..., 2. , 2. , 2. ],[4.7, 3.2, 1.3, ..., 2. , 2. , 2. ],...,[6.5, 3. , 5.2, ..., 2. , 2. , 2. ],[6.2, 3.4, 5.4, ..., 2. , 2. , 2. ],[5.9, 3. , 5.1, ..., 2. , 2. , 2. ]])
data=pd.DataFrame(data,columns=["F1","F2","F3","F4","target"])
data
F1F2F3F4target
05.13.51.40.20.0
14.93.01.40.20.0
24.73.21.30.20.0
34.63.11.50.20.0
45.03.61.40.20.0
..................
1456.73.05.22.32.0
1466.32.55.01.92.0
1476.53.05.22.02.0
1486.23.45.42.32.0
1495.93.05.11.82.0

150 rows × 5 columns

data.insert(0,"ones",1)
data
onesF1F2F3F4target
015.13.51.40.20.0
114.93.01.40.20.0
214.73.21.30.20.0
314.63.11.50.20.0
415.03.61.40.20.0
.....................
14516.73.05.22.32.0
14616.32.55.01.92.0
14716.53.05.22.02.0
14816.23.45.42.32.0
14915.93.05.11.82.0

150 rows × 6 columns

data["target"]=data["target"].astype("int32")
data
onesF1F2F3F4target
015.13.51.40.20
114.93.01.40.20
214.73.21.30.20
314.63.11.50.20
415.03.61.40.20
.....................
14516.73.05.22.32
14616.32.55.01.92
14716.53.05.22.02
14816.23.45.42.32
14915.93.05.11.82

150 rows × 6 columns

1.2 训练数据的准备

data_x
array([[5.1, 3.5, 1.4, 0.2],[4.9, 3. , 1.4, 0.2],[4.7, 3.2, 1.3, 0.2],[4.6, 3.1, 1.5, 0.2],[5. , 3.6, 1.4, 0.2],[5.4, 3.9, 1.7, 0.4],[4.6, 3.4, 1.4, 0.3],[5. , 3.4, 1.5, 0.2],[4.4, 2.9, 1.4, 0.2],[4.9, 3.1, 1.5, 0.1],[5.4, 3.7, 1.5, 0.2],[4.8, 3.4, 1.6, 0.2],[4.8, 3. , 1.4, 0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.8, 6.7, 2.2],[7.7, 2.6, 6.9, 2.3],[6. , 2.2, 5. , 1.5],[6.9, 3.2, 5.7, 2.3],[5.6, 2.8, 4.9, 2. ],[7.7, 2.8, 6.7, 2. ],[6.3, 2.7, 4.9, 1.8],[6.7, 3.3, 5.7, 2.1],[7.2, 3.2, 6. , 1.8],[6.2, 2.8, 4.8, 1.8],[6.1, 3. , 4.9, 1.8],[6.4, 2.8, 5.6, 2.1],[7.2, 3. , 5.8, 1.6],[7.4, 2.8, 6.1, 1.9],[7.9, 3.8, 6.4, 2. ],[6.4, 2.8, 5.6, 2.2],[6.3, 2.8, 5.1, 1.5],[6.1, 2.6, 5.6, 1.4],[7.7, 3. , 6.1, 2.3],[6.3, 3.4, 5.6, 2.4],[6.4, 3.1, 5.5, 1.8],[6. , 3. , 4.8, 1.8],[6.9, 3.1, 5.4, 2.1],[6.7, 3.1, 5.6, 2.4],[6.9, 3.1, 5.1, 2.3],[5.8, 2.7, 5.1, 1.9],[6.8, 3.2, 5.9, 2.3],[6.7, 3.3, 5.7, 2.5],[6.7, 3. , 5.2, 2.3],[6.3, 2.5, 5. , 1.9],[6.5, 3. , 5.2, 2. ],[6.2, 3.4, 5.4, 2.3],[5.9, 3. , 5.1, 1.8]])
data_x=np.insert(data_x,0,1,axis=1)
data_x.shape,data_y.shape
((150, 5), (150, 1))
#训练数据的特征和标签
data_x,data_y
(array([[1. , 5.1, 3.5, 1.4, 0.2],[1. , 4.9, 3. , 1.4, 0.2],[1. , 4.7, 3.2, 1.3, 0.2],[1. , 4.6, 3.1, 1.5, 0.2],[1. , 5. , 3.6, 1.4, 0.2],[1. , 5.4, 3.9, 1.7, 0.4],[1. , 4.6, 3.4, 1.4, 0.3],[1. , 5. , 3.4, 1.5, 0.2],[1. , 4.4, 2.9, 1.4, 0.2],[1. , 4.9, 3.1, 1.5, 0.1],[1. , 5.4, 3.7, 1.5, 0.2],[1. , 4.8, 3.4, 1.6, 0.2],[1. , 4.8, 3. , 1.4, 0.1],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.3, 3.3, 4.7, 1.6],[1. , 4.9, 2.4, 3.3, 1. ],[1. , 6.6, 2.9, 4.6, 1.3],[1. , 5.2, 2.7, 3.9, 1.4],[1. , 5. , 2. , 3.5, 1. ],[1. , 5.9, 3. , 4.2, 1.5],[1. , 6. , 2.2, 4. , 1. ],[1. , 6.1, 2.9, 4.7, 1.4],[1. , 5.6, 2.9, 3.6, 1.3],[1. , 6.7, 3.1, 4.4, 1.4],[1. , 5.6, 3. , 4.5, 1.5],[1. , 5.8, 2.7, 4.1, 1. ],[1. , 6.2, 2.2, 4.5, 1.5],[1. , 5.6, 2.5, 3.9, 1.1],[1. , 5.9, 3.2, 4.8, 1.8],[1. , 6.1, 2.8, 4. , 1.3],[1. , 6.3, 2.5, 4.9, 1.5],[1. , 6.1, 2.8, 4.7, 1.2],[1. , 6.4, 2.9, 4.3, 1.3],[1. , 6.6, 3. , 4.4, 1.4],[1. , 6.8, 2.8, 4.8, 1.4],[1. , 6.7, 3. , 5. , 1.7],[1. , 6. , 2.9, 4.5, 1.5],[1. , 5.7, 2.6, 3.5, 1. ],[1. , 5.5, 2.4, 3.8, 1.1],[1. , 5.5, 2.4, 3.7, 1. ],[1. , 5.8, 2.7, 3.9, 1.2],[1. , 6. , 2.7, 5.1, 1.6],[1. , 5.4, 3. , 4.5, 1.5],[1. , 6. , 3.4, 4.5, 1.6],[1. , 6.7, 3.1, 4.7, 1.5],[1. , 6.3, 2.3, 4.4, 1.3],[1. , 5.6, 3. , 4.1, 1.3],[1. , 5.5, 2.5, 4. , 1.3],[1. , 5.5, 2.6, 4.4, 1.2],[1. , 6.1, 3. , 4.6, 1.4],[1. , 5.8, 2.6, 4. , 1.2],[1. , 5. , 2.3, 3.3, 1. ],[1. , 5.6, 2.7, 4.2, 1.3],[1. , 5.7, 3. , 4.2, 1.2],[1. , 5.7, 2.9, 4.2, 1.3],[1. , 6.2, 2.9, 4.3, 1.3],[1. , 5.1, 2.5, 3. , 1.1],[1. , 5.7, 2.8, 4.1,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. , 6.8, 3.2, 5.9, 2.3],[1. , 6.7, 3.3, 5.7, 2.5],[1. , 6.7, 3. , 5.2, 2.3],[1. , 6.3, 2.5, 5. , 1.9],[1. , 6.5, 3. , 5.2, 2. ],[1. , 6.2, 3.4, 5.4, 2.3],[1. , 5.9, 3. , 5.1, 1.8]]),array([[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]]))

由于有三个类别,那么在训练时三类数据要分开

data1=data.copy()
data1
onesF1F2F3F4target
015.13.51.40.20
114.93.01.40.20
214.73.21.30.20
314.63.11.50.20
415.03.61.40.20
.....................
14516.73.05.22.32
14616.32.55.01.92
14716.53.05.22.02
14816.23.45.42.32
14915.93.05.11.82

150 rows × 6 columns

data

data1.loc[data["target"]!=0,"target"]=0
data1.loc[data["target"]==0,"target"]=1
data1
onesF1F2F3F4target
015.13.51.40.21
114.93.01.40.21
214.73.21.30.21
314.63.11.50.21
415.03.61.40.21
.....................
14516.73.05.22.30
14616.32.55.01.90
14716.53.05.22.00
14816.23.45.42.30
14915.93.05.11.80

150 rows × 6 columns

data1_x=data1.iloc[:,:data1.shape[1]-1].values
data1_y=data1.iloc[:,data1.shape[1]-1].values
data1_x.shape,data1_y.shape
((150, 5), (150,))
#针对第二类,即第二个分类器的数据
data2=data.copy()
data2.loc[data["target"]==1,"target"]=1
data2.loc[data["target"]!=1,"target"]=0
data2["target"]==0
0      True
1      True
2      True
3      True
4      True... 
145    True
146    True
147    True
148    True
149    True
Name: target, Length: 150, dtype: bool
data2.shape[1]
6
data2.iloc[50:55,:]
onesF1F2F3F4target
5017.03.24.71.41
5116.43.24.51.51
5216.93.14.91.51
5315.52.34.01.31
5416.52.84.61.51
data2_x=data2.iloc[:,:data2.shape[1]-1].values
data2_y=data2.iloc[:,data2.shape[1]-1].values
#针对第三类,即第三个分类器的数据
data3=data.copy()
data3.loc[data["target"]==2,"target"]=1
data3.loc[data["target"]!=2,"target"]=0
data3
onesF1F2F3F4target
015.13.51.40.20
114.93.01.40.20
214.73.21.30.20
314.63.11.50.20
415.03.61.40.20
.....................
14516.73.05.22.31
14616.32.55.01.91
14716.53.05.22.01
14816.23.45.42.31
14915.93.05.11.81

150 rows × 6 columns

data3_x=data3.iloc[:,:data3.shape[1]-1].values
data3_y=data3.iloc[:,data3.shape[1]-1].values

1.3 定义假设函数,代价函数,梯度下降算法(从实验3复制过来)

def sigmoid(z):return 1 / (1 + np.exp(-z))
def h(X,w):z=X@wh=sigmoid(z)return h
#代价函数构造
def cost(X,w,y):#当X(m,n+1),y(m,),w(n+1,1)y_hat=sigmoid(X@w)right=np.multiply(y.ravel(),np.log(y_hat).ravel())+np.multiply((1-y).ravel(),np.log(1-y_hat).ravel())cost=-np.sum(right)/X.shape[0]return cost
def sigmoid(z):return 1 / (1 + np.exp(-z))def h(X,w):z=X@wh=sigmoid(z)return h#代价函数构造
def cost(X,w,y):#当X(m,n+1),y(m,),w(n+1,1)y_hat=sigmoid(X@w)right=np.multiply(y.ravel(),np.log(y_hat).ravel())+np.multiply((1-y).ravel(),np.log(1-y_hat).ravel())cost=-np.sum(right)/X.shape[0]return costdef grandient(X,y,iter_num,alpha):y=y.reshape((X.shape[0],1))w=np.zeros((X.shape[1],1))cost_lst=[]  for i in range(iter_num):y_pred=h(X,w)-ytemp=np.zeros((X.shape[1],1))for j in range(X.shape[1]):right=np.multiply(y_pred.ravel(),X[:,j])gradient=1/(X.shape[0])*(np.sum(right))temp[j,0]=w[j,0]-alpha*gradientw=tempcost_lst.append(cost(X,w,y.ravel()))return w,cost_lst

1.4 调用梯度下降算法来学习三个分类模型的参数

#初始化超参数
iter_num,alpha=600000,0.001
#训练第一个模型
w1,cost_lst1=grandient(data1_x,data1_y,iter_num,alpha)
import matplotlib.pyplot as plt
plt.plot(range(iter_num),cost_lst1,"b-o")
[<matplotlib.lines.Line2D at 0x2562630b100>]

1

#训练第二个模型
w2,cost_lst2=grandient(data2_x,data2_y,iter_num,alpha)
import matplotlib.pyplot as plt
plt.plot(range(iter_num),cost_lst2,"b-o")
[<matplotlib.lines.Line2D at 0x25628114280>]

2

#训练第三个模型
w3,cost_lst3=grandient(data3_x,data3_y,iter_num,alpha)
w3
array([[-3.22437049],[-3.50214058],[-3.50286355],[ 5.16580317],[ 5.89898368]])
import matplotlib.pyplot as plt
plt.plot(range(iter_num),cost_lst3,"b-o")
[<matplotlib.lines.Line2D at 0x2562e0f81c0>]

3

1.5 利用模型进行预测

h(data_x,w3)
array([[1.48445441e-11],[1.72343968e-10],[1.02798153e-10],[5.81975546e-10],[1.48434710e-11],[1.95971176e-11],[2.18959639e-10],[5.01346874e-11],[1.40930075e-09],[1.12830635e-10],[4.31888744e-12],[1.69308343e-10],[1.35613372e-10],[1.65858883e-10],[7.89880725e-14],[4.23224675e-13],[2.48199140e-12],[2.67766642e-11],[5.39314286e-12],[1.56935848e-11],[3.47096426e-11],[4.01827075e-11],[7.63005509e-12],[8.26864773e-10],[7.97484594e-10],[3.41189783e-10],[2.73442178e-10],[1.75314894e-11],[1.48456174e-11],[4.84204982e-10],[4.84239990e-10],[4.01914238e-11],[1.18813180e-12],[3.14985611e-13],[2.03524473e-10],[2.14461446e-11],[2.18189955e-12],[1.16799745e-11],[5.92281641e-10],[3.53217554e-11],[2.26727669e-11],[8.74004884e-09],[2.93949962e-10],[6.26783110e-10],[2.23513465e-10],[4.41246960e-10],[1.45841303e-11],[2.44584721e-10],[6.13010507e-12],[4.24539165e-11],[1.64123143e-03],[8.55503211e-03],[1.65105645e-02],[9.87814122e-02],[3.97290777e-02],[1.11076040e-01],[4.19003715e-02],[2.88426221e-03],[6.27161978e-03],[7.67020481e-02],[2.27204861e-02],[2.08212169e-02],[4.58067633e-03],[9.90450665e-02],[1.19419048e-03],[1.41462060e-03],[2.22638069e-01],[2.68940904e-03],[3.66014737e-01],[6.97791873e-03],[5.78803255e-01],[2.32071970e-03],[5.28941621e-01],[4.57649874e-02],[2.69208900e-03],[2.84603646e-03],[2.20421076e-02],[2.07507605e-01],[9.10460936e-02],[2.44824946e-04],[8.37509821e-03],[2.78543808e-03],[3.11283202e-03],[8.89831833e-01],[3.65880536e-01],[3.03993844e-02],[1.18930239e-02],[4.99150151e-02],[1.10252946e-02],[5.15923462e-02],[1.43653056e-01],[4.41610209e-02],[7.37513950e-03],[2.88447014e-03],[5.07366744e-02],[7.24617687e-03],[1.83460602e-02],[5.40874928e-03],[3.87210511e-04],[1.55791816e-02],[9.99862942e-01],[9.89637526e-01],[9.86183040e-01],[9.83705644e-01],[9.98410187e-01],[9.97834502e-01],[9.84208537e-01],[9.85434538e-01],[9.94141336e-01],[9.94561329e-01],[7.20333384e-01],[9.70431293e-01],[9.62754456e-01],[9.96609064e-01],[9.99222270e-01],[9.83684437e-01],[9.26437633e-01],[9.83486260e-01],[9.99950496e-01],[9.39002061e-01],[9.88043323e-01],[9.88637702e-01],[9.98357641e-01],[7.65848930e-01],[9.73006160e-01],[8.76969899e-01],[6.61137141e-01],[6.97324053e-01],[9.97185846e-01],[6.11033594e-01],[9.77494647e-01],[6.58573810e-01],[9.98437920e-01],[5.24529693e-01],[9.70465066e-01],[9.87624920e-01],[9.97236435e-01],[9.26432706e-01],[6.61104746e-01],[8.84442100e-01],[9.96082862e-01],[8.40940308e-01],[9.89637526e-01],[9.96974990e-01],[9.97386310e-01],[9.62040470e-01],[9.52214579e-01],[8.96902215e-01],[9.90200940e-01],[9.28785160e-01]])
#将数据输入三个模型的看看结果
multi_pred=pd.DataFrame(zip(h(data_x,w1).ravel(),h(data_x,w2).ravel(),h(data_x,w3).ravel()))
multi_pred
012
00.9992970.1080371.484454e-11
10.9970610.2708141.723440e-10
20.9986330.1647101.027982e-10
30.9957740.2319105.819755e-10
40.9994150.0852591.484347e-11
............
1450.0000070.1275749.620405e-01
1460.0000060.4963899.522146e-01
1470.0000100.2347458.969022e-01
1480.0000060.0584449.902009e-01
1490.0000140.2842959.287852e-01

150 rows × 3 columns

multi_pred.values[:3]
array([[9.99297209e-01, 1.08037473e-01, 1.48445441e-11],[9.97060801e-01, 2.70813780e-01, 1.72343968e-10],[9.98632728e-01, 1.64709623e-01, 1.02798153e-10]])
#每个样本的预测值
np.argmax(multi_pred.values,axis=1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], dtype=int64)
#每个样本的真实值
data_y
array([[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]])

1.6 评估模型

np.argmax(multi_pred.values,axis=1)==data_y.ravel()
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True, False,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True, False, False,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True, False,  True,  True,  True, False,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True])
np.sum(np.argmax(multi_pred.values,axis=1)==data_y.ravel())
145
np.sum(np.argmax(multi_pred.values,axis=1)==data_y.ravel())/len(data)
0.9666666666666667

1.7 试试sklearn

from sklearn.linear_model import LogisticRegression
#建立第一个模型
clf1=LogisticRegression()
clf1.fit(data1_x,data1_y)
#建立第二个模型
clf2=LogisticRegression()
clf2.fit(data2_x,data2_y)
#建立第三个模型
clf3=LogisticRegression()
clf3.fit(data3_x,data3_y)
LogisticRegression()
y_pred1=clf1.predict(data_x)
y_pred2=clf2.predict(data_x)
y_pred3=clf3.predict(data_x)
#可视化各模型的预测结果
multi_pred=pd.DataFrame(zip(y_pred1,y_pred2,y_pred3),columns=["模型1","模糊2","模型3"])
multi_pred
模型1模糊2模型3
0100
1100
2100
3100
4100
............
145001
146011
147001
148001
149001

150 rows × 3 columns

#判断预测结果
np.argmax(multi_pred.values,axis=1)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0,0, 1, 1, 1, 2, 0, 1, 1, 0, 0, 0, 2, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1,0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2,2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2,2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2], dtype=int64)
data_y.ravel()
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
#计算准确率
np.sum(np.argmax(multi_pred.values,axis=1)==data_y.ravel())/data.shape[0]
0.7333333333333333

实验4(1) 请动手完成你们第一个多分类问题,祝好运!完成下面代码

2.1 数据读取

data_x,data_y=datasets.make_blobs(n_samples=200, n_features=6,  centers=4,random_state=0)
data_x.shape,data_y.shape
((200, 6), (200,))

2.2 训练数据的准备

data=np.insert(data_x,data_x.shape[1],data_y,axis=1)
data=pd.DataFrame(data,columns=["F1","F2","F3","F4","F5","F6","target"])
data
F1F2F3F4F5F6target
02.1166327.972800-9.328969-8.224605-12.1784295.4984472.0
11.8864494.6210062.8415950.431245-2.4713502.5078330.0
22.3913296.464609-9.805900-7.289968-9.6509856.3884602.0
3-1.0347766.6268869.031235-0.8129085.4498550.1340621.0
4-0.4815938.1917537.504717-1.9756886.6490210.6368241.0
........................
1955.4348937.1284719.7895466.0613820.6341335.7570243.0
196-0.4066257.5860019.322750-1.8373336.477815-0.9927251.0
1972.0314627.804427-8.539512-9.824409-10.0469356.9180852.0
1984.0818896.12768511.0911264.812011-0.0059155.3422113.0
1990.9857447.285737-8.395940-6.586471-9.6517656.6510122.0

200 rows × 7 columns

data["target"]=data["target"].astype("int32")
data
F1F2F3F4F5F6target
02.1166327.972800-9.328969-8.224605-12.1784295.4984472
11.8864494.6210062.8415950.431245-2.4713502.5078330
22.3913296.464609-9.805900-7.289968-9.6509856.3884602
3-1.0347766.6268869.031235-0.8129085.4498550.1340621
4-0.4815938.1917537.504717-1.9756886.6490210.6368241
........................
1955.4348937.1284719.7895466.0613820.6341335.7570243
196-0.4066257.5860019.322750-1.8373336.477815-0.9927251
1972.0314627.804427-8.539512-9.824409-10.0469356.9180852
1984.0818896.12768511.0911264.812011-0.0059155.3422113
1990.9857447.285737-8.395940-6.586471-9.6517656.6510122

200 rows × 7 columns

data.insert(0,"ones",1)
data
onesF1F2F3F4F5F6target
012.1166327.972800-9.328969-8.224605-12.1784295.4984472
111.8864494.6210062.8415950.431245-2.4713502.5078330
212.3913296.464609-9.805900-7.289968-9.6509856.3884602
31-1.0347766.6268869.031235-0.8129085.4498550.1340621
41-0.4815938.1917537.504717-1.9756886.6490210.6368241
...........................
19515.4348937.1284719.7895466.0613820.6341335.7570243
1961-0.4066257.5860019.322750-1.8373336.477815-0.9927251
19712.0314627.804427-8.539512-9.824409-10.0469356.9180852
19814.0818896.12768511.0911264.812011-0.0059155.3422113
19910.9857447.285737-8.395940-6.586471-9.6517656.6510122

200 rows × 8 columns

#第一个类别的数据
data1=data.copy()
data1.loc[data["target"]==0,"target"]=1
data1.loc[data["target"]!=0,"target"]=0
data1
onesF1F2F3F4F5F6target
012.1166327.972800-9.328969-8.224605-12.1784295.4984470
111.8864494.6210062.8415950.431245-2.4713502.5078331
212.3913296.464609-9.805900-7.289968-9.6509856.3884600
31-1.0347766.6268869.031235-0.8129085.4498550.1340620
41-0.4815938.1917537.504717-1.9756886.6490210.6368240
...........................
19515.4348937.1284719.7895466.0613820.6341335.7570240
1961-0.4066257.5860019.322750-1.8373336.477815-0.9927250
19712.0314627.804427-8.539512-9.824409-10.0469356.9180850
19814.0818896.12768511.0911264.812011-0.0059155.3422110
19910.9857447.285737-8.395940-6.586471-9.6517656.6510120

200 rows × 8 columns

data1_x=data1.iloc[:,:data1.shape[1]-1].values
data1_y=data1.iloc[:,data1.shape[1]-1].values
data1_x.shape,data1_y.shape
((200, 7), (200,))
#第二个类别的数据
data2=data.copy()
data2.loc[data["target"]==1,"target"]=1
data2.loc[data["target"]!=1,"target"]=0
data2
onesF1F2F3F4F5F6target
012.1166327.972800-9.328969-8.224605-12.1784295.4984470
111.8864494.6210062.8415950.431245-2.4713502.5078330
212.3913296.464609-9.805900-7.289968-9.6509856.3884600
31-1.0347766.6268869.031235-0.8129085.4498550.1340621
41-0.4815938.1917537.504717-1.9756886.6490210.6368241
...........................
19515.4348937.1284719.7895466.0613820.6341335.7570240
1961-0.4066257.5860019.322750-1.8373336.477815-0.9927251
19712.0314627.804427-8.539512-9.824409-10.0469356.9180850
19814.0818896.12768511.0911264.812011-0.0059155.3422110
19910.9857447.285737-8.395940-6.586471-9.6517656.6510120

200 rows × 8 columns

data2_x=data2.iloc[:,:data2.shape[1]-1].values
data2_y=data2.iloc[:,data2.shape[1]-1].values
#第三个类别的数据
data3=data.copy()
data3.loc[data["target"]==2,"target"]=1
data3.loc[data["target"]!=2,"target"]=0
data3
onesF1F2F3F4F5F6target
012.1166327.972800-9.328969-8.224605-12.1784295.4984471
111.8864494.6210062.8415950.431245-2.4713502.5078330
212.3913296.464609-9.805900-7.289968-9.6509856.3884601
31-1.0347766.6268869.031235-0.8129085.4498550.1340620
41-0.4815938.1917537.504717-1.9756886.6490210.6368240
...........................
19515.4348937.1284719.7895466.0613820.6341335.7570240
1961-0.4066257.5860019.322750-1.8373336.477815-0.9927250
19712.0314627.804427-8.539512-9.824409-10.0469356.9180851
19814.0818896.12768511.0911264.812011-0.0059155.3422110
19910.9857447.285737-8.395940-6.586471-9.6517656.6510121

200 rows × 8 columns

data3_x=data3.iloc[:,:data3.shape[1]-1].values
data3_y=data3.iloc[:,data3.shape[1]-1].values
#第四个类别的数据
data4=data.copy()
data4.loc[data["target"]==3,"target"]=1
data4.loc[data["target"]!=3,"target"]=0
data4
onesF1F2F3F4F5F6target
012.1166327.972800-9.328969-8.224605-12.1784295.4984470
111.8864494.6210062.8415950.431245-2.4713502.5078330
212.3913296.464609-9.805900-7.289968-9.6509856.3884600
31-1.0347766.6268869.031235-0.8129085.4498550.1340620
41-0.4815938.1917537.504717-1.9756886.6490210.6368240
...........................
19515.4348937.1284719.7895466.0613820.6341335.7570241
1961-0.4066257.5860019.322750-1.8373336.477815-0.9927250
19712.0314627.804427-8.539512-9.824409-10.0469356.9180850
19814.0818896.12768511.0911264.812011-0.0059155.3422111
19910.9857447.285737-8.395940-6.586471-9.6517656.6510120

200 rows × 8 columns

data4_x=data4.iloc[:,:data4.shape[1]-1].values
data4_y=data4.iloc[:,data4.shape[1]-1].values

2.3 定义假设函数、代价函数和梯度下降算法

def sigmoid(z):return 1 / (1 + np.exp(-z))
def h(X,w):z=X@wh=sigmoid(z)return h
#代价函数构造
def cost(X,w,y):#当X(m,n+1),y(m,),w(n+1,1)y_hat=sigmoid(X@w)right=np.multiply(y.ravel(),np.log(y_hat).ravel())+np.multiply((1-y).ravel(),np.log(1-y_hat).ravel())cost=-np.sum(right)/X.shape[0]return cost
def grandient(X,y,iter_num,alpha):y=y.reshape((X.shape[0],1))w=np.zeros((X.shape[1],1))cost_lst=[]  for i in range(iter_num):y_pred=h(X,w)-ytemp=np.zeros((X.shape[1],1))for j in range(X.shape[1]):right=np.multiply(y_pred.ravel(),X[:,j])gradient=1/(X.shape[0])*(np.sum(right))temp[j,0]=w[j,0]-alpha*gradientw=tempcost_lst.append(cost(X,w,y.ravel()))return w,cost_lst

2.4 学习这四个分类模型

import matplotlib.pyplot as plt
#初始化超参数
iter_num,alpha=600000,0.001
#训练第1个模型
w1,cost_lst1=grandient(data1_x,data1_y,iter_num,alpha)
plt.plot(range(iter_num),cost_lst1,"b-o")
[<matplotlib.lines.Line2D at 0x25624eb08e0>]

4

#训练第2个模型
w2,cost_lst2=grandient(data2_x,data2_y,iter_num,alpha)
plt.plot(range(iter_num),cost_lst2,"b-o")
[<matplotlib.lines.Line2D at 0x25631b87a60>]

5

#训练第3个模型
w3,cost_lst3=grandient(data3_x,data3_y,iter_num,alpha)
plt.plot(range(iter_num),cost_lst3,"b-o")
[<matplotlib.lines.Line2D at 0x2562bcdfac0>]

6

#训练第4个模型
w4,cost_lst4=grandient(data4_x,data4_y,iter_num,alpha)
plt.plot(range(iter_num),cost_lst4,"b-o")
[<matplotlib.lines.Line2D at 0x25631ff4ee0>]

7

2.5 利用模型进行预测

data_x
array([[ 2.11663151e+00,  7.97280013e+00, -9.32896918e+00,-8.22460526e+00, -1.21784287e+01,  5.49844655e+00],[ 1.88644899e+00,  4.62100554e+00,  2.84159548e+00,4.31244563e-01, -2.47135027e+00,  2.50783257e+00],[ 2.39132949e+00,  6.46460915e+00, -9.80590050e+00,-7.28996786e+00, -9.65098460e+00,  6.38845956e+00],...,[ 2.03146167e+00,  7.80442707e+00, -8.53951210e+00,-9.82440872e+00, -1.00469351e+01,  6.91808489e+00],[ 4.08188906e+00,  6.12768483e+00,  1.10911262e+01,4.81201082e+00, -5.91530191e-03,  5.34221079e+00],[ 9.85744105e-01,  7.28573657e+00, -8.39593964e+00,-6.58647097e+00, -9.65176507e+00,  6.65101187e+00]])
data_x=np.insert(data_x,0,1,axis=1)
data_x.shape
(200, 7)
w3.shape
(7, 1)
multi_pred=pd.DataFrame(zip(h(data_x,w1).ravel(),h(data_x,w2).ravel(),h(data_x,w3).ravel(),h(data_x,w4).ravel()))
multi_pred
0123
00.0204364.556248e-159.999975e-012.601227e-27
10.8204884.180906e-053.551499e-055.908691e-05
20.1093097.316201e-149.999978e-017.091713e-24
30.0366089.999562e-011.048562e-095.724854e-03
40.0030759.999292e-012.516742e-096.423038e-05
...............
1950.0172783.221293e-063.753372e-149.999943e-01
1960.0033699.999966e-016.673394e-102.281428e-03
1970.0006061.118174e-139.999941e-011.780212e-28
1980.0130724.999118e-059.811154e-149.996689e-01
1990.1515481.329623e-139.999447e-012.571989e-24

200 rows × 4 columns

2.6 计算准确率

np.sum(np.argmax(multi_pred.values,axis=1)==data_y.ravel())/len(data)
1.0

相关文章:

【Python机器学习】实验04(1) 多分类(基于逻辑回归)实践

文章目录 多分类以及机器学习实践如何对多个类别进行分类1.1 数据的预处理1.2 训练数据的准备1.3 定义假设函数&#xff0c;代价函数&#xff0c;梯度下降算法&#xff08;从实验3复制过来&#xff09;1.4 调用梯度下降算法来学习三个分类模型的参数1.5 利用模型进行预测1.6 评…...

【ChatGLM_01】ChatGLM2-6B本地安装与部署(大语言模型)

基于本地知识库的问答 1、简介&#xff08;1&#xff09;ChatGLM2-6B&#xff08;2&#xff09;LangChain&#xff08;3&#xff09;基于单一文档问答的实现原理&#xff08;4&#xff09;大规模语言模型系列技术&#xff1a;以GLM-130B为例&#xff08;5&#xff09;新建知识库…...

谷歌Tsunami(海啸)扫描器搭建扩展使用教程

目录 介绍 下载地址 功能总结 原理 服务探测 漏洞检测 安装...

诚迈科技承办大同首届信息技术产业峰会,共话数字经济崭新未来

7月28日&#xff0c;“聚势而强共领信创”2023大同首届信息技术产业峰会圆满举行。本次峰会由中共大同市委、大同市人民政府主办&#xff0c;中国高科技产业化研究会国际交流合作中心、山西省信创协会协办&#xff0c;中共大同市云冈区委、大同市云冈区人民政府、诚迈科技&…...

【Python】Python使用TK实现动态爱心效果

【Python】Python使用Tk实现动态爱心效果 画布使用了缓存机制&#xff0c;启动时绘制足够多的帧数&#xff0c;运行时一帧帧地取出来展示&#xff0c;明显更流畅&#xff0c;加快了程序执行速度。将控制跳动动画的函数从正弦函数换成了贝塞尔函数&#xff0c;贝塞尔函数更灵活…...

Unity3d C#快速打开萤石云监控视频流(ezopen)支持WebGL平台,替代UMP播放视频流的方案(含源码)

前言 Universal Media Player算是视频流播放功能常用的插件了&#xff0c;用到现在已经不知道躺了多少坑了&#xff0c;这个插件虽然是白嫖的&#xff0c;不过被甲方和领导吐槽的就是播放视频流的速度特别慢&#xff0c;可能需要几十秒来打开监控画面&#xff0c;等待的时间较…...

【Android】APP启动优化学习笔记

启动优化目的 用户体验&#xff1a; 应用的启动速度直接影响用户体验。用户希望应用能够快速启动并迅速响应他们的操作。如果应用启动较慢&#xff0c;用户可能会感到不满&#xff0c;并且有可能选择卸载或切换到竞争对手的应用。通过启动优化&#xff0c;可以提高应用的启动…...

docker的使用

docker安装 https://docs.docker.com/engine/install/debian/ 设置国内镜像 创建或修改 /etc/docker/daemon.json 文件&#xff0c;修改为如下形式 {"registry-mirrors": ["https://registry.hub.docker.com","http://hub-mirror.c.163.com"…...

iOS使用Rust调研

编辑已恢复 我们已与您断开连接。尝试重连时会保存您所做的变更。尝试重连 标题 1 已保存 Bin Song B 要发布此内容&#xff0c;请选择键盘上的 ⌘Enter。 发布 关闭 Rust技术空间 … 跨平台使用调研 iOS使用Rust调研 添加表情符号 添加标题图像 添加状态 一、iOS 项…...

抖音引流推广的几个方法,抖音全自动引流脚本软件详细使用教学

大家好我是你们的小编一辞脚本&#xff0c;今天给大家分享新的知识&#xff0c;很开心可以在CSDN平台分享知识给大家,很多伙伴看不到代码我先录制一下视频 在给大家做代码&#xff0c;给大家分享一下抖音引流脚本的知识和视频演示 不懂的小伙伴可以认真看一下&#xff0c;我们…...

k8s概念-DaemonSet

回到目录 参考链接https://v1-23.docs.kubernetes.io/zh/docs/concepts/workloads/controllers/daemonset/ DaemonSet 确保全部&#xff08;或者某些&#xff09;节点上运行一个 Pod 的副本 当节点加入到K8S集群中&#xff0c;pod会被&#xff08;DaemonSet&#xff09;调度到…...

Mac 终端快捷键设置:如何给 Mac 中的 Terminal 设置 Ctrl+Alt+T 快捷键快速启动

Mac 电脑中正常是没有直接打开终端命令行的快捷键指令的&#xff0c;但可以通过 commandspace 打开聚焦搜索&#xff0c;然后输入 ter 或者 terminal 全拼打开。但习惯了 linux 的同学会觉得这个操作很别扭。于是我们希望能通过键盘按键直接打开。 操作流程如下&#xff1a; 1…...

VR 变电站事故追忆反演——正泰电力携手图扑

VR(Virtual Reality&#xff0c;虚拟现实)技术作为近年来快速发展的一项新技术&#xff0c;具有广泛的应用前景&#xff0c;支持融合人工智能、机器学习、大数据等技术&#xff0c;实现更加智能化、个性化的应用。在电力能源领域&#xff0c;VR 技术在高性能计算机和专有设备支…...

fpga开发——蜂鸣器

蜂鸣器的原理 有源蜂鸣器和无源蜂鸣器 无源蜂鸣器利用电磁感应现象&#xff0c;为音圈接入交变电流后形成的电磁铁与永磁铁相吸或相斥而推动振膜发声&#xff0c;接入直流电只能持续推动振膜而无法产生声音&#xff0c;只能在接通或断开时产生声音。无源蜂鸣器的工作原理与扬声…...

【Liux下6818开发板(ARM)】触摸屏

(꒪ꇴ꒪ ),hello我是祐言博客主页&#xff1a;C语言基础,Linux基础,软件配置领域博主&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff01;送给读者的一句鸡汤&#x1f914;&#xff1a;集中起来的意志可以击穿顽石!作者水平很有限&#xff0c;如果发现错误&#x…...

苍穹外卖day11——数据统计图形报表(Apache ECharts)

效果展示 Apache ECharts 介绍 常见图表 入门案例 快速上手 - Handbook - Apache ECharts 营业额统计——需求分析与设计 产品原型 接口设计 VO设计 营业额统计——代码开发 Controller中 /*** 数据统计相关接口*/ RestController RequestMapping("/admin/report&qu…...

在制作PC端Game Launcher游戏启动器时涉及到的技术选型

1&#xff09;在制作PC端Game Launcher游戏启动器时涉及到的技术选型​ 2&#xff09;​如何将图片显示到Canvas的Raw Image上面 3&#xff09;Unity 2018.4.4f1退出重启后出现黑屏 4&#xff09;如何获取到GPU耗时 这是第346篇UWA技术知识分享的推送&#xff0c;精选了UWA社区…...

SQL力扣练习(九)

目录 1.订单最多的用户(586) 示例 1 解法一(limit) 解法二(dense_rank()) 2.体育馆的人流量 示例 1 解法一(临时表) 解法二&#xff08;三表法&#xff09; 1.订单最多的用户(586) 表: Orders --------------------------- | Column Name | Type | ---------…...

软考高级架构师笔记-10数学计算题

目录 1. 前文回顾 & 考情分析2. 最小生成树3. 最短路径4. 网络与最大流量5. 线性规划6. 动态规划/决策表7. 博弈论8. 状态转移矩阵9. 决策论10. 结语1. 前文回顾 & 考情分析 前文回顾: 软考高级架构师笔记-1计算机硬件软考高级架构师笔记-2计算机软件(操作系统)软考…...

设计模式五:建造者模式(Builder Pattern)

建造者模式(Builder Pattern)是一种创建型设计模式&#xff0c;用于通过一系列步骤来构建复杂对象。它将对象的构建过程与其表示分离&#xff0c;从而允许相同的构建过程可以创建不同的表示。 建造者模式中的几个角色&#xff1a; 产品(Product)&#xff1a;表示被构建的复杂…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

React第五十七节 Router中RouterProvider使用详解及注意事项

前言 在 React Router v6.4 中&#xff0c;RouterProvider 是一个核心组件&#xff0c;用于提供基于数据路由&#xff08;data routers&#xff09;的新型路由方案。 它替代了传统的 <BrowserRouter>&#xff0c;支持更强大的数据加载和操作功能&#xff08;如 loader 和…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

SCAU期末笔记 - 数据分析与数据挖掘题库解析

这门怎么题库答案不全啊日 来简单学一下子来 一、选择题&#xff08;可多选&#xff09; 将原始数据进行集成、变换、维度规约、数值规约是在以下哪个步骤的任务?(C) A. 频繁模式挖掘 B.分类和预测 C.数据预处理 D.数据流挖掘 A. 频繁模式挖掘&#xff1a;专注于发现数据中…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

Mac下Android Studio扫描根目录卡死问题记录

环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中&#xff0c;提示一个依赖外部头文件的cpp源文件需要同步&#xff0c;点…...

uniapp手机号一键登录保姆级教程(包含前端和后端)

目录 前置条件创建uniapp项目并关联uniClound云空间开启一键登录模块并开通一键登录服务编写云函数并上传部署获取手机号流程(第一种) 前端直接调用云函数获取手机号&#xff08;第三种&#xff09;后台调用云函数获取手机号 错误码常见问题 前置条件 手机安装有sim卡手机开启…...

nnUNet V2修改网络——暴力替换网络为UNet++

更换前,要用nnUNet V2跑通所用数据集,证明nnUNet V2、数据集、运行环境等没有问题 阅读nnU-Net V2 的 U-Net结构,初步了解要修改的网络,知己知彼,修改起来才能游刃有余。 U-Net存在两个局限,一是网络的最佳深度因应用场景而异,这取决于任务的难度和可用于训练的标注数…...

软件工程 期末复习

瀑布模型&#xff1a;计划 螺旋模型&#xff1a;风险低 原型模型: 用户反馈 喷泉模型:代码复用 高内聚 低耦合&#xff1a;模块内部功能紧密 模块之间依赖程度小 高内聚&#xff1a;指的是一个模块内部的功能应该紧密相关。换句话说&#xff0c;一个模块应当只实现单一的功能…...