一、使用朴素贝叶斯制作鸢尾花数据模型
from sklearn. preprocessing import StandardScaler
from sklearn. naive_bayes import MultinomialNB
from sklearn. datasets import load_iris
from sklearn. model_selection import train_test_split
from sklearn. feature_extraction. text import CountVectorizerif __name__ == '__main__' : ir = load_iris ( ) features = ir. datatargets = ir. target# print ( features[ : 20 ] ) # 分割数据集x_train, x_test, y_train, y_test = train_test_split ( features, targets, train_size= 0.8 , random_state= 100 ) # 数据量之间差距过大,需要预处理下stdobj = StandardScaler ( ) stdobj. fit ( x_train) x_train = stdobj. transform ( x_train) x_test = stdobj. transform ( x_test) # 进行特征提取countobj = CountVectorizer ( ) countobj. fit ( x_train) # countobj x_train = countobj. transform ( x_train) x_test = countobj. transform ( x_test) # print ( x_train[ : 20 ] ) # 拟合朴素贝叶斯模型estimate = MultinomialNB ( ) estimate. fit ( x_train, y_train) model_score = estimate. score ( x_test, y_test) print ( model_score) resault= estimate. predict ( x_test[ 0 ] ) print ( f"{ir.feature_names[resault]}" )
二、使用__new__()方法可以制作单例模式,因为__new__()方法是在实例化对象时调用的第一个方法,可以控制对象的创建过程。在__new__()方法中,我们可以判断是否已经创建了实例,如果已经创建了实例,则返回已有的实例,否则创建一个新的实例。这样就可以保证在整个程序中只有一个实例存在。同时,由于__new__()方法是在实例化对象时调用的第一个方法,所以可以在__init__()方法中进行初始化操作,从而实现重新初始化的功能。
class Singleton ( object) : def __new__ ( cls) : if not hasattr ( cls, '_instance' ) : cls. _instance = super ( Singleton, cls) . __new__ ( cls) return cls. _instancea = Singleton ( )
b = Singleton ( )
c = Singleton ( ) print ( a, id ( a) )
print ( b, id ( b) )
print ( c, id ( c) )
在这个示例代码中,我们定义了一个Singleton类,并在其中实现了__new__ ( ) 方法。在__new__ ( ) 方法中,我们首先判断是否已经创建了实例,如果已经创建了实例,则返回已有的实例,否则创建一个新的实例。这样就可以保证在整个程序中只有一个实例存在。最后,我们创建了三个Singleton对象,并打印它们的内存地址,可以看到它们的内存地址都是相同的,说明它们都是同一个实例。另外,我们还可以在__init__ ( ) 方法中进行初始化操作,从而实现重新初始化的功能。下面是一个使用__new__ ( ) 方法和__init__ ( ) 方法实现单例模式的示例代码:class SingletonCls: def __new__ ( cls, * args, * * kwargs) : if not hasattr ( cls, "_instance" ) : cls. _instance = super ( SingletonCls, cls) . __new__ ( cls) return cls. _instancedef __init__ ( self, * args, * * kwargs) : passclass Foo ( SingletonCls) : def __init__ ( self, name) : self. name = namels = Foo ( "ls" )
print ( ls. name)
zs = Foo ( "zs" )
print ( ls. name)
print ( zs. name)
三、写代码,有如下字典,按照要求实现每一个功能
dic= { ‘k1’: ’v1’, ’k2’: [ ‘alex’, ’sb’] , ( 1 , 2 , 3 , 4 , 5 ) : { ‘k3’: [ ‘2 ’, 100 , ’wer’] } }
1、将’k2’对应的值的最后面添加一个元素’23’; 2、将’k2’对应的值的第一个位置插入一个元素’a’; 3、将(1,2,3,4,5)对应的值添加一个键值对’k4’,’v4’; 4、将(1,2,3,4,5)对应的值添加一个键值对(1,2,3),’ok’; 5、将’k3’对应的值的’wer’更改为’qq’;
if __name__ == '__main__' : dic = { 'k1' : 'v1' , 'k2' : [ 'alex' , 'sb' ] , ( 1 , 2 , 3 , 4 , 5 ) : { 'k3' : [ '2' , 100 , 'wer' ] } } dic[ 'k2' ] . append ( "23" ) dic[ 'k2' ] . insert ( 0 , 'a' ) dic. get ( ( 1 , 2 , 3 , 4 , 5 ) ) [ 'k4' ] = "v4" dic. get ( ( 1 , 2 , 3 , 4 , 5 ) ) [ ( 1 , 2 , 3 ) ] = "ok" dic. get ( ( 1 , 2 , 3 , 4 , 5 ) ) . get ( "k3" ) [ 2 ] = "qq" ; print ( dic)
四、使用__new__()方法,制作单例
class SingletonObject ( object) : __instance= Nonedef __new__ ( cls) : if cls. __instance is None: cls. __instance= object. __new__ ( cls) return cls. __instance
if __name__ == '__main__' : s1= SingletonObject ( ) s2= SingletonObject ( ) print ( s1) print ( s2)