import matplotlib as mpl import matplotlib.pyplot as plt import pandas as pd from sklearn.linear_model import LinearRegression import numpy as np from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error from sklearn.metrics import r2_score mpl.rcParams['font.sans-serif']=['KaiTi'] mpl.rcParams['axes.unicode_minus']= False data=pd.read_csv('data.csv') #print(data.head) data.dropna(axis=0,how='any',inplace=True) data['单价']=data['单价'].map(lambda d:d.replace('元/平米','')) data['单价']=data['单价'].astype(float) data['总价']=data['总价'].map(lambda e:e.replace('万','')) data['总价']=data['总价'].astype(float) data['建筑面积']=data['建筑面积'].map(lambda p:p.replace('平米','')) data['建筑面积']=data['建筑面积'].astype(float) copy_d=data.copy() copy_d[['室','厅','卫']]=copy_d['户型'].str.extract('(\d+)室(\d+)厅(\d+)卫') copy_d['室']=copy_d['室'].astype(float) new_data=data[['总价','建筑面积']] new_data['室']=copy_d['室'] new_data.dropna(axis=0,how='any',inplace=True) print(new_data) new_data.loc[2583]=[None,180.00,4] data_train=new_data.loc[0:2582] x_list=['建筑面积','室'] ndata_mean=data_train.mean() ndata_std=data_train.std() data_train=(data_train-ndata_mean)/ndata_std x_train=data_train[x_list].values y_train=data_train['总价'].values svr=LinearRegression() svr.fit(x_train,y_train) x_test=((new_data[x_list]-ndata_mean[x_list])/ndata_std[x_list]).values y_test=((new_data['总价']-ndata_mean['总价'])/ndata_std).values print(y_test) y_pred=svr.predict(x_test) new_data['y_pred']=y_test*ndata_std['总价']+ndata_mean['总价'] print(new_data[['总价','y_pred']]) svr_acc=svr.score(x_test,y_test)*100 svr_mae=mean_absolute_error(x_test,y_pred) print(svr_mae)
时间: 2024-01-19 11:02:35 浏览: 129
这段代码主要是使用线性回归模型对房价进行预测,其中使用了 pandas 和 sklearn 进行数据的处理和模型的建立。具体来说,代码首先读取了一个 csv 文件,并进行了一些数据清洗,然后对数据进行了特征工程,提取出了室、厅、卫等特征,并将其转化为数值类型。接着将数据集分为训练集和测试集,使用训练集对线性回归模型进行训练,最后对测试集进行预测,并计算了模型的精度和误差等指标。
相关问题
在Python中想运行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中运行这些代码,你需要确保已经安装了相应的包。大多数包都可以使用`pip`进行安装。
首先,在命令行中运行以下命令来安装所需的包:
```bash
pip install scikit-learn pandas matplotlib numpy
```
然后,在你的Python脚本中导入所需的库:
```python
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.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
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库。如果遇到任何问题,请确保已正确安装这些库,并且版本与代码兼容。
运行下面代码需要安装哪些包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工具。
阅读全文