data.head(11) X=data.loc[:,:] y=data.loc[:,] from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) from sklearn.neighbors import KNeighborsClassifier clf = KNeighborsClassifier(n_neighbors=3) clf.fit(X_train, y_train.astype('int')) print("Test set predictions: {}".format(clf.predict(X_test))) print("Training set score:{:.2f}".format(clf.score(X_train,y_train))) print("Test set accuracy: {:.2f}".format(clf.score(X_test, y_test)))
时间: 2023-12-24 15:13:14 浏览: 87
这段代码中还有一个问题,即X和y的赋值语句中的语法不正确,需要指定具体的列名或索引。如果你想将所有列都作为特征输入到模型中,可以将X的赋值语句修改为:
```
X = data.iloc[:, :-1]
```
这样就可以将除了最后一列以外的所有列作为特征数据提取出来,并赋值给X。假设最后一列是"label"列,你可以将y的赋值语句修改为:
```
y = data.iloc[:, -1]
```
这样就可以将"label"列的数据提取出来作为y数据了。
相关问题
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.preprocessing import OneHotEncoder,LabelEncoder from sklearn.model_selection import cross_val_score from sklearn.model_selection import GridSearchCV df = pd.read_csv('mafs(1).csv') df.head() man = df['Gender']=='M' woman = df['Gender']=='F' data = pd.DataFrame() data['couple'] = df.Couple.unique() data['location'] = df.Location.values[::2] data['man_name'] = df.Name[man].values data['woman_name'] = df.Name[woman].values data['man_occupation'] = df.Occupation[man].values data['woman_occupaiton'] = df.Occupation[woman].values data['man_age'] = df.Age[man].values data['woman_age'] = df.Age[woman].values data['man_decision'] = df.Decision[man].values data['woman_decision']=df.Decision[woman].values data['status'] = df.Status.values[::2] data.head() data.to_csv('./data.csv') data = pd.read_csv('./data.csv',index_col=0) data.head() enc = OneHotEncoder() matrix = enc.fit_transform(data['location'].values.reshape(-1,1)).toarray() feature_labels = enc.categories_ loc = pd.DataFrame(data=matrix,columns=feature_labels) data_new=data[['man_age','woman_age','man_decision','woman_decision','status']] data_new.head() lec=LabelEncoder() for label in ['man_decision','woman_decision','status']: data_new[label] = lec.fit_transform(data_new[label]) data_final = pd.concat([loc,data_new],axis=1) data_final.head() X = data_final.drop(columns=['status']) Y = data_final.status X_train,X_test,Y_train,Y_test=train_test_split(X,Y,train_size=0.7,shuffle=True) rfc = RandomForestClassifier(n_estimators=20,max_depth=2) param_grid = [ {'n_estimators': [3, 10, 30,60,100], 'max_features': [2, 4, 6, 8], 'max_depth':[2,4,6,8,10]}, ] grid_search = GridSearchCV(rfc, param_grid, cv=9) grid_search.fit(X, Y) print(grid_search.best_score_) #最好的参数 print(grid_search.best_params_)
这段代码是使用随机森林分类器对一个约会节目的参赛者进行分类的,根据他们的年龄、职业、决策等信息,将他们的状态(是否找到约会对象)进行预测。代码中使用了OneHotEncoder和LabelEncoder对分类变量进行编码,使用GridSearchCV对超参数进行调优。最后输出了最好的参数和相应的得分。
import numpy as np import sklearn import pandas as pd data = pd.read_csv('C:/Users/86159/Desktop/TaxDetection_NoLable.csv') data.head(11) X=data.loc[:,:] y=data.loc[:,] from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) from sklearn.neighbors import KNeighborsClassifier clf = KNeighborsClassifier(n_neighbors=3) clf.fit(X_train, y_train.astype('int')) print("Test set predictions: {}".format(clf.predict(X_test))) print("Training set score:{:.2f}".format(clf.score(X_train,y_train))) print("Test set accuracy: {:.2f}".format(clf.score(X_test, y_test)))
这段代码中有一个问题,即y的赋值语句中没有指定具体的列名或索引,因此会导致后面的代码无法正确地运行。你需要指定y的列名或索引,以便正确地将y数据提取出来。假设你的数据集中有一个名为"label"的列,你可以将y的赋值语句修改为:
```
y = data.loc[:, "label"]
```
这样就可以将"label"列的数据提取出来作为y数据了。
阅读全文
相关推荐
















