第1 -第10个机器学习练习

1.导入scikit-learn库

importsklearn

2.加载数据集

fromsklearn.datasetsimportload_iris
iris=load_iris()
X=iris.data
y=iris.target

3.划分数据集为训练集和测试集


fromsklearn.model_selectionimporttrain_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)

4.标准化特征值

fromsklearn.preprocessingimportStandardScaler
scaler=StandardScaler()
X_train_scaled=scaler.fit_transform(X_train)
X_test_scaled=scaler.transform(X_test)

5.训练线性回归模型


fromsklearn.linear_modelimportLinearRegression
regressor=LinearRegression()
regressor.fit(X_train,y_train)

6.使用k近邻算法分类


fromsklearn.neighborsimportKNeighborsClassifier
classifier=KNeighborsClassifier(n_neighbors=3)
classifier.fit(X_train,y_train)

7.计算决策树分类器的准确率


fromsklearn.treeimportDecisionTreeClassifier
classifier=DecisionTreeClassifier(random_state=42)
classifier.fit(X_train,y_train)
score=classifier.score(X_test,y_test)
print("Accuracy:",score)

8.计算朴素贝叶斯分类器的准确率


fromsklearn.naive_bayesimportGaussianNB
classifier=GaussianNB()
classifier.fit(X_train,y_train)
score=classifier.score(X_test,y_test)
print("Accuracy:",score)

9.计算支持向量机分类器的准确率


fromsklearn.svmimportSVC
classifier=SVC(random_state=42)
classifier.fit(X_train,y_train)
score=classifier.score(X_test,y_test)
print("Accuracy:",score)

10.训练随机森林模型


fromsklearn.ensembleimportRandomForestClassifier
classifier=RandomForestClassifier(n_estimators=100,random_state=42)
classifier.fit(X_train,y_train)
posted on 2023-03-29 08:30  skywide  阅读(9)  评论(0编辑  收藏  举报  来源