# 导入所需的库import numpy as npimport pandas as pdfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import mean_squared_error# 读取数据集data = pd.read_csv('shanghai_housing_price.csv')# 数据预处理data = data.drop(['Index', 'id', 'url', 'Cid', 'DOM'], axis=1) # 删除无关特征data = data.dropna() # 删除缺失值# 特征工程data['livingRoom'] = data['livingRoom'].apply(lambda x: int(x.split('室')[0]))data['drawingRoom'] = data['drawingRoom'].apply(lambda x: int(x.split('厅')[0]))data['kitchen'] = data['kitchen'].apply(lambda x: int(x.split('厨')[0]))data['bathroom'] = data['bathroom'].apply(lambda x: int(x.split('卫')[0]))data['constructionTime'] = data['constructionTime'].apply(lambda x: int(x.split('年')[0]))# 划分训练集和测试集X = data.iloc[:, :-1].valuesy = data.iloc[:, -1].valuesX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)# 训练模型rf = RandomForestRegressor(n_estimators=100, random_state=0)rf.fit(X_train, y_train)# 预测结果y_pred = rf.predict(X_test)# 评估模型mse = mean_squared_error(y_test, y_pred)print('均方误差:', mse)
时间: 2024-01-14 07:04:08 浏览: 156
要单独呈现csv文件,你可以在代码中使用pandas库中的to_csv()函数将数据保存为csv格式的文件,例如:
```python
# 读取数据集
data = pd.read_csv('shanghai_housing_price.csv')
# 将数据保存为csv格式的文件
data.to_csv('shanghai_housing_price_clean.csv', index=False)
```
这样就可以将经过数据预处理和特征工程处理后的数据保存为一个新的csv文件,方便后续的使用。需要注意的是,to_csv()函数中的index参数表示是否保存行索引,默认为True,因此如果不需要保存行索引,需要将index参数设为False。
相关问题
优化这段代码# import modules 导入模块 from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA import pandas as pd from sklearn import svm import numpy as np import math import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import colors from sklearn.model_selection import train_test_split from sklearn import datasets from matplotlib.colors import ListedColormap from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.model_selection import StratifiedShuffleSplit,StratifiedKFold from sklearn.model_selection import GridSearchCV from sklearn.model_selection import GridSearchCV, LeaveOneOut, cross_val_predict from sklearn.model_selection import KFold from sklearn.linear_model import LogisticRegression from sklearn.naive_bayes import GaussianNB from sklearn.neighbors import KNeighborsClassifier from sklearn import svm from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import roc_auc_score from sklearn.metrics import roc_auc_score import math import datetime import multiprocessing as mp from sklearn.ensemble import StackingClassifier from sklearn.pipeline import make_pipeline from sklearn.svm import LinearSVC import random
你可以对这段代码进行如下优化:
```python
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import colors
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split, StratifiedShuffleSplit, StratifiedKFold, GridSearchCV, LeaveOneOut, cross_val_predict, KFold
from sklearn.decomposition import PCA
from sklearn.svm import SVC, LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.metrics import roc_auc_score
# 导入模块
# 设置随机种子
random.seed(42)
np.random.seed(42)
# 其他代码...
```
这样做的优点是将导入的模块进行整理和分类,提高了代码的可读性。同时,设置了随机种子,保证了代码的可复现性。你可以根据需要添加其他的代码或模块。
运行下面代码需要安装哪些包from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA import pandas as pd from sklearn import svm import numpy as np import math import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import colors from sklearn.model_selection import train_test_split from sklearn import datasets from matplotlib.colors import ListedColormap from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.model_selection import StratifiedShuffleSplit,StratifiedKFold from sklearn.model_selection import GridSearchCV from sklearn.model_selection import GridSearchCV, LeaveOneOut, cross_val_predict from sklearn.model_selection import KFold from sklearn.linear_model import LogisticRegression from sklearn.naive_bayes import GaussianNB from sklearn.neighbors import KNeighborsClassifier from sklearn import svm from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import roc_auc_score from sklearn.metrics import roc_auc_score import math import datetime import multiprocessing as mp from sklearn.ensemble import StackingClassifier from sklearn.pipeline import make_pipeline from sklearn.svm import LinearSVC import random
运行上述代码需要安装以下Python包:
- scikit-learn:提供了机器学习算法和工具,包括模型选择、特征提取、预处理等。可以使用`pip install scikit-learn`进行安装。
- pandas:提供了高性能、易于使用的数据结构和数据分析工具。可以使用`pip install pandas`进行安装。
- matplotlib:用于绘制图表和数据可视化的库。可以使用`pip install matplotlib`进行安装。
这些包是常用的数据分析和机器学习库,可以通过pip安装。在安装之前,确保你已经正确地配置了Python环境和pip工具。
阅读全文