from sklearn.svm import LinearSVR import matplotlib.pyplot as plt data = pd.read_excel('../tmp/new_reg_data_GM11.xls') # 读取数据 data = data.set_index('Unnamed: 0') data = data.drop(index ='模型精度') feature = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13'] #特征所在列 data_train = data.loc[range(1994, 2014)].copy() #取2014年前的数据建模 data_mean = data_train.mean() data_std = data_train.std() data_train = (data_train - data_mean) / data_std #数据标准化 x_train = data_train[feature].as_matrix() #特征数据 y_train = data_train['y'].as_matrix() #标签数据 linearsvr = LinearSVR(random_state=123) #调用LinearSVR()函数 linearsvr.fit(x_train, y_train) #预测2014 年和2015 年的财政收入,并还原结果 x = ((data[feature] - data_mean[feature]) / data std[feature]).as_matrix() data[u'y_pred'] = linearsvr.predict (x) * data_std['y'] + data_mean['y'] outputfile ='../tmp/new_reg_data_GM11_revenue.xls' data.to_excel(outputfile) print('真实值与预测值分别为: \n', data[['y', 'y_pred']]) print('预测图为: ',data[['y','y_pred']].plot(style = ['b-o','r-*'])) #画出预测结果图 plt.xlabel('年份') plt.xticks(range(1994,2015,2))
时间: 2024-02-01 13:02:07 浏览: 81
这段代码是使用线性支持向量回归(LinearSVR)进行财政收入预测的示例。首先,代码导入了所需的库,包括sklearn.svm中的LinearSVR和matplotlib.pyplot。然后,通过pd.read_excel方法读取了名为'../tmp/new_reg_data_GM11.xls'的Excel文件,并将数据设置为以'Unnamed: 0'列为索引。接下来,选择了特定的特征列,并将数据划分为训练集和测试集。然后,对训练集进行了数据标准化操作,并将特征数据和标签数据分别保存在x_train和y_train中。接着,创建了一个LinearSVR对象,并使用训练数据进行拟合。然后,对2014年和2015年的财政收入进行预测,并将结果还原至原始数据范围内。最后,将预测结果保存至Excel文件'../tmp/new_reg_data_GM11_revenue.xls',并打印出真实值和预测值。同时,代码还画出了真实值和预测值的图表,并设置了横坐标的刻度。
相关问题
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix
这段代码是在 Python 中导入了一些常用的机器学习库和模块,包括 pandas、numpy、matplotlib、sklearn 等。其中:
- pandas 是 Python 中常用的数据分析库,可以用来读取和处理数据;
- numpy 是 Python 中常用的科学计算库,提供了数组、矩阵等数据结构和各种数学函数;
- matplotlib 是 Python 中常用的数据可视化库,可以用来绘制各种图表;
- sklearn 是 Python 中常用的机器学习库,提供了许多常用的机器学习算法和工具,比如数据预处理、模型选择、模型评估等。
这段代码中还导入了不同的机器学习算法,包括逻辑回归、决策树、K近邻和支持向量机等。最后还导入了一些评估指标,比如分类报告和混淆矩阵。
在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库。如果遇到任何问题,请确保已正确安装这些库,并且版本与代码兼容。
阅读全文
相关推荐
















