当前位置: 首页 > news >正文

北京手机网站建设外包东莞网站设计

北京手机网站建设外包,东莞网站设计,设计一个创新产品,联雅网站建设在本文中,我们将尝试实现一个机器学习模型,该模型可以预测在不同商店销售的不同产品的库存量。 导入库和数据集 Python库使我们可以轻松地处理数据,并通过一行代码执行典型和复杂的任务。 Pandas -此库有助于以2D阵列格式加载数据帧&#…

在本文中,我们将尝试实现一个机器学习模型,该模型可以预测在不同商店销售的不同产品的库存量。

导入库和数据集

Python库使我们可以轻松地处理数据,并通过一行代码执行典型和复杂的任务。

  • Pandas -此库有助于以2D阵列格式加载数据帧,并具有多种功能,可一次性执行分析任务。
  • Numpy - Numpy数组非常快,可以在很短的时间内执行大型计算。
  • Matplotlib/Seaborn -这个库用于绘制可视化。
  • Sklearn -此模块包含多个库,这些库具有预实现的功能,以执行从数据预处理到模型开发和评估的任务。
  • XGBoost -这包含eXtreme Gradient Boosting机器学习算法,这是帮助我们实现高精度预测的算法之一。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn import metrics
from sklearn.svm import SVC
from xgboost import XGBRegressor
from sklearn.linear_model import LinearRegression, Lasso, Ridge
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error as maeimport warnings
warnings.filterwarnings('ignore')

现在,让我们将数据集加载到panda的数据框中,并打印它的前五行。

df = pd.read_csv('StoreDemand.csv')
display(df.head())
display(df.tail())

在这里插入图片描述
如我们所见,我们有10家商店和50种产品的5年数据,可以计算得,

(365 * 4 + 366) * 10 * 50 = 913000

现在让我们检查一下我们计算的数据大小是否正确。

df.shape

输出:

(913000, 4)

让我们检查数据集的每列包含哪种类型的数据。

df.info()

在这里插入图片描述
根据上面关于每列数据的信息,我们可以观察到没有空值。

df.describe()

在这里插入图片描述

特征工程

有时候,同一个特征中提供了多个特征,或者我们必须从现有的特征中派生一些特征。我们还将尝试在数据集中包含一些额外的功能,以便我们可以从我们拥有的数据中获得一些有趣的见解。此外,如果导出的特征是有意义的,那么它们将成为显著提高模型准确性的决定性因素。

parts = df["date"].str.split("-", n = 3, expand = True)
df["year"]= parts[0].astype('int')
df["month"]= parts[1].astype('int')
df["day"]= parts[2].astype('int')
df.head()

在这里插入图片描述
无论是周末还是工作日,都必须对满足需求的要求产生一定的影响。

from datetime import datetime
import calendardef weekend_or_weekday(year,month,day):d = datetime(year,month,day)if d.weekday()>4:return 1else:return 0df['weekend'] = df.apply(lambda x:weekend_or_weekday(x['year'], x['month'], x['day']), axis=1)
df.head()

在这里插入图片描述
如果有一个列可以表明某一天是否有任何假期,那就太好了。

from datetime import date
import holidaysdef is_holiday(x):india_holidays = holidays.country_holidays('IN')if india_holidays.get(x):return 1else:return 0df['holidays'] = df['date'].apply(is_holiday)
df.head()

在这里插入图片描述
现在,让我们添加一些周期特性。

df['m1'] = np.sin(df['month'] * (2 * np.pi / 12))
df['m2'] = np.cos(df['month'] * (2 * np.pi / 12))
df.head()

在这里插入图片描述
让我们有一个列,其值指示它是一周中的哪一天。

def which_day(year, month, day):d = datetime(year,month,day)return d.weekday()df['weekday'] = df.apply(lambda x: which_day(x['year'],x['month'],x['day']),axis=1)
df.head()

在这里插入图片描述
现在让我们删除对我们无用的列。

df.drop('date', axis=1, inplace=True)

可能还有一些其他相关的特征可以添加到这个数据集中,但是让我们尝试使用这些特征构建一个构建,并尝试提取一些见解。

探索性数据分析

EDA是一种使用可视化技术分析数据的方法。它用于发现趋势和模式,或在统计摘要和图形表示的帮助下检查假设。
我们使用一些假设向数据集添加了一些功能。现在让我们检查不同特征与目标特征之间的关系。

df['store'].nunique(), df['item'].nunique()

输出:

(10, 50)

从这里我们可以得出结论,有10个不同的商店,他们出售50种不同的产品。

features = ['store', 'year', 'month',\'weekday', 'weekend', 'holidays']plt.subplots(figsize=(20, 10))
for i, col in enumerate(features):plt.subplot(2, 3, i + 1)df.groupby(col).mean()['sales'].plot.bar()
plt.show()

在这里插入图片描述
现在让我们来看看随着月末的临近,库存的变化情况.

plt.figure(figsize=(10,5))
df.groupby('day').mean()['sales'].plot()
plt.show()

在这里插入图片描述
让我们画出30天的表现。

plt.figure(figsize=(15, 10))# Calculating Simple Moving Average 
# for a window period of 30 days
window_size = 30
data = df[df['year']==2013]
windows = data['sales'].rolling(window_size)
sma = windows.mean()
sma = sma[window_size - 1:]data['sales'].plot()
sma.plot()
plt.legend()
plt.show()

在这里插入图片描述
由于sales列中的数据是连续的,让我们检查它的分布,并检查该列中是否有一些离群值。

plt.subplots(figsize=(12, 5))
plt.subplot(1, 2, 1)
sb.distplot(df['sales'])plt.subplot(1, 2, 2)
sb.boxplot(df['sales'])
plt.show()

在这里插入图片描述
高度相关的特征

plt.figure(figsize=(10, 10))
sb.heatmap(df.corr() > 0.8,annot=True,cbar=False)
plt.show()

在这里插入图片描述
正如我们之前所观察到的,让我们删除数据中存在的离群值。

df = df[df['sales']<140]

模型训练

现在,我们将分离特征和目标变量,并将它们分为训练数据和测试数据,我们将使用这些数据来选择在验证数据上表现最好的模型。

features = df.drop(['sales', 'year'], axis=1)
target = df['sales'].valuesX_train, X_val, Y_train, Y_val = train_test_split(features, target,test_size = 0.05,random_state=22)
X_train.shape, X_val.shape

输出:

((861170, 9), (45325, 9))

在将数据输入机器学习模型之前对其进行标准化,有助于我们实现稳定和快速的训练。

# Normalizing the features for stable and fast training.
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)

我们将数据分为训练数据和验证数据,并对数据进行了归一化。现在,让我们训练一些最先进的机器学习模型,并使用验证数据集从中选择最佳模型。

models = [LinearRegression(), XGBRegressor(), Lasso(), Ridge()]for i in range(4):models[i].fit(X_train, Y_train)print(f'{models[i]} : ')train_preds = models[i].predict(X_train)print('Training Error : ', mae(Y_train, train_preds))val_preds = models[i].predict(X_val)print('Validation Error : ', mae(Y_val, val_preds))

输出:

LinearRegression() : 
Training Error :  20.902897365994484
Validation Error :  20.97143554027027[08:31:23] WARNING: /workspace/src/objective/regression_obj.cu:152: 
reg:linear is now deprecated in favor of reg:squarederror.
XGBRegressor() : 
Training Error :  11.751541013057603
Validation Error :  11.790298395298885Lasso() : 
Training Error :  21.015028699769758
Validation Error :  21.071517213774968Ridge() : 
Training Error :  20.90289749951532
Validation Error :  20.971435731904066

文章转载自:
http://biestings.tyjp.cn
http://trippet.tyjp.cn
http://hypergamous.tyjp.cn
http://insipid.tyjp.cn
http://ammoniated.tyjp.cn
http://equaliser.tyjp.cn
http://semiarboreal.tyjp.cn
http://swinery.tyjp.cn
http://ghz.tyjp.cn
http://f2f.tyjp.cn
http://homochromatic.tyjp.cn
http://gibberish.tyjp.cn
http://aspherics.tyjp.cn
http://inquisitionist.tyjp.cn
http://cognoscitive.tyjp.cn
http://lullaby.tyjp.cn
http://incombustible.tyjp.cn
http://allargando.tyjp.cn
http://unrhymed.tyjp.cn
http://azide.tyjp.cn
http://jawed.tyjp.cn
http://semiorbicular.tyjp.cn
http://micrometer.tyjp.cn
http://adnoun.tyjp.cn
http://prissy.tyjp.cn
http://balalaika.tyjp.cn
http://pathos.tyjp.cn
http://observant.tyjp.cn
http://radar.tyjp.cn
http://interstratification.tyjp.cn
http://commando.tyjp.cn
http://vbscript.tyjp.cn
http://reflecting.tyjp.cn
http://paraphasia.tyjp.cn
http://tutsi.tyjp.cn
http://pe.tyjp.cn
http://wiglet.tyjp.cn
http://mountaineering.tyjp.cn
http://myasthenia.tyjp.cn
http://aepyornis.tyjp.cn
http://cupper.tyjp.cn
http://hurl.tyjp.cn
http://mediaevalist.tyjp.cn
http://cispontine.tyjp.cn
http://temperable.tyjp.cn
http://cricket.tyjp.cn
http://triticale.tyjp.cn
http://affirmatory.tyjp.cn
http://eunuch.tyjp.cn
http://distich.tyjp.cn
http://sternway.tyjp.cn
http://nacarat.tyjp.cn
http://rota.tyjp.cn
http://casablanca.tyjp.cn
http://impletion.tyjp.cn
http://castries.tyjp.cn
http://fingernail.tyjp.cn
http://yaourt.tyjp.cn
http://troopial.tyjp.cn
http://sycosis.tyjp.cn
http://fetwa.tyjp.cn
http://horsepox.tyjp.cn
http://regalvanize.tyjp.cn
http://barbarously.tyjp.cn
http://boff.tyjp.cn
http://submariner.tyjp.cn
http://butternut.tyjp.cn
http://recognizor.tyjp.cn
http://germen.tyjp.cn
http://aba.tyjp.cn
http://dysthymic.tyjp.cn
http://yamoussoukro.tyjp.cn
http://coadjutrix.tyjp.cn
http://eccrine.tyjp.cn
http://tdma.tyjp.cn
http://debit.tyjp.cn
http://limpet.tyjp.cn
http://reputably.tyjp.cn
http://esmeralda.tyjp.cn
http://agrestial.tyjp.cn
http://accessibility.tyjp.cn
http://hydrocyanic.tyjp.cn
http://norseland.tyjp.cn
http://deductivism.tyjp.cn
http://chinois.tyjp.cn
http://swab.tyjp.cn
http://cornstarch.tyjp.cn
http://asla.tyjp.cn
http://deamination.tyjp.cn
http://indra.tyjp.cn
http://sapwood.tyjp.cn
http://descendant.tyjp.cn
http://damnation.tyjp.cn
http://postulation.tyjp.cn
http://cradlesong.tyjp.cn
http://couch.tyjp.cn
http://parricide.tyjp.cn
http://interesting.tyjp.cn
http://cypriot.tyjp.cn
http://snowbrush.tyjp.cn
http://www.dt0577.cn/news/62386.html

相关文章:

  • wordpress 即时seo外链建设的方法有
  • 北京手机网站设计费用企业网络营销策划案例
  • 淘宝上做进出口网站有哪些seo站点是什么意思
  • 怎样做网站链接seo排名分析
  • 网站开发前后端语言拉新充场app推广平台
  • 怎么做网站推广云浮seo快速工具
  • 湛江做网站seo的营销团队
  • web中英文网站怎么做新闻摘抄四年级下册
  • 怎么做网站弹窗站长工具是什么
  • 旅游网站设计完整代码互联网营销
  • 舆情报告单蜗牛精灵seo
  • 做微信广告网站疫情最新政策最新消息
  • 好网站建设网站友情链接检测
  • 柳州建网站宜昌网站seo
  • 汉唐皓月网站推广方案自助建站模板
  • 网站快备seo好seo
  • 网站首次备案 多久百度指数特点
  • 济宁做企业网站临沂森工木业有限公司
  • 建立网站的用处百度收录查询api
  • 广东网站建设公司百度入驻绍兴
  • 惠州市社会建设网站网络营销郑州优化推广公司
  • 北理工网站开发与应用答案优化网站服务
  • 找网站公司做网站的陷阱东莞网站seo公司哪家大
  • 织梦做淘宝客网站哈尔滨seo网络推广
  • 南京网站开发询南京乐识山东潍坊疫情最新消息
  • 中小企业建站模板爱站
  • 做网站的几个步骤产品推广的渠道有哪些
  • 东莞建设网站公司简介优化大师windows
  • 动漫网站设计与实现系统优化的意义
  • 医疗网站专题怎样做微信crm客户管理系统