df_data = pd.read_csv('G:/new_label.csv')
时间: 2023-10-29 17:28:05 浏览: 84
I'm sorry, as an AI language model, I don't have access to your local file system. Can you please provide more context on what you are trying to achieve so that I can assist you better?
相关问题
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对超参数进行调优。最后输出了最好的参数和相应的得分。
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--path', type=str, default=r"data/UCI HAR Dataset/UCI HAR Dataset", help='UCI dataset data path') parser.add_argument('--save', type=str, default='data/UCI_Smartphone_Raw.csv', help='save file name') args = parser.parse_args() data_path = args.path # read train subjects train_subjects = pd.read_csv(os.path.join(data_path, 'train/subject_train.txt'), header=None, names=['subject']) # read test subjects test_subjects = pd.read_csv(os.path.join(data_path, 'test/subject_test.txt'), header=None, names=['subject']) # concat subjects = pd.concat([train_subjects, test_subjects], axis=0) # read train labels train_labels = pd.read_csv(os.path.join(data_path, 'train/y_train.txt'), header=None, names=['label']) # read train labels test_labels = pd.read_csv(os.path.join(data_path, 'test/y_test.txt'), header=None, names=['label']) # labels labels = pd.concat([train_labels, test_labels], axis=0) final_dataframe = pd.concat([subjects, labels], axis=1) data = [] for name in COLUMNS: final_dataframe = pd.concat([final_dataframe, read_txt(name)], axis=1) final_dataframe.to_csv(args.save,index=False) 如何将文中txt文件改成mnist数据集数据,其他不做大修改
要将代码修改为使用MNIST数据集而不是txt文件,你需要进行以下修改:
1. 导入所需的库和模块:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_openml
```
2. 加载MNIST数据集:
```python
mnist = fetch_openml('mnist_784', version=1, cache=True)
```
3. 将图像矩阵和标签分别存储为numpy数组:
```python
X = mnist.data
y = mnist.target
```
4. 创建一个包含所有数据的Pandas数据框:
```python
df = pd.DataFrame(X)
df['label'] = y
```
5. 将数据框保存为CSV文件:
```python
df.to_csv(args.save, index=False)
```
注意,这里我们使用了`args.save`作为保存文件名,这是根据你之前的代码中的`--save`命令行参数来决定的。
除了以上修改,你还需要删除以下代码块,因为MNIST数据集已经包含了图像数据和标签,不需要再读取其他文件:
```python
# read train subjects
train_subjects = pd.read_csv(os.path.join(data_path, 'train/subject_train.txt'), header=None, names=['subject'])
# read test subjects
test_subjects = pd.read_csv(os.path.join(data_path, 'test/subject_test.txt'), header=None, names=['subject'])
# concat
subjects = pd.concat([train_subjects, test_subjects], axis=0)
# read train labels
train_labels = pd.read_csv(os.path.join(data_path, 'train/y_train.txt'), header=None, names=['label'])
# read train labels
test_labels = pd.read_csv(os.path.join(data_path, 'test/y_test.txt'), header=None, names=['label'])
# labels
labels = pd.concat([train_labels, test_labels], axis=0)
final_dataframe = pd.concat([subjects, labels], axis=1)
data = []
for name in COLUMNS:
final_dataframe = pd.concat([final_dataframe, read_txt(name)], axis=1)
```
这些修改后的代码将直接将MNIST数据集转换为CSV文件,并保存到指定的路径中。
阅读全文