微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何检查线性回归算法的性能并改进?

如何解决如何检查线性回归算法的性能并改进?

我已经在 this dataset 上使用 tensorflow 实现了线性回归,以预测给定规格后的笔记本电脑价格。我计划在一个网络应用上使用它,它可以根据用户的要求为他们提供笔记本电脑的预算。

from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
import tensorflow.compat.v2.feature_column as fc
from sklearn.model_selection import train_test_split
import tensorflow as tf

df = pd.read_csv('laptop_price.csv',encoding = 'latin-1')
df['Weight']=df['Weight'].replace('kg','',regex=True).astype(float)
df['Ram']=df['Ram'].replace('GB',regex=True).astype(int)
df.rename(columns = {'Weight':'Weight (kg)'},inplace = True)
df.rename(columns = {'Ram':'Ram (GB)'},inplace = True)

final_dataset = df[['Company','TypeName','Inches','ScreenResolution','cpu','Ram (GB)','Memory','Gpu','OpSys','Weight (kg)','Price_euros']]

columns = list(df)
columns.remove('laptop_ID')

NUMERIC_COLUMNS = ['Inches','Ram (GB)']
CATEGORICAL_COLUMNS = []
for item in columns:
    if item not in (NUMERIC_COLUMNS):
        CATEGORICAL_COLUMNS.append(item)
CATEGORICAL_COLUMNS.remove('Price_euros')
CATEGORICAL_COLUMNS.remove('Product')
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = final_dataset[feature_name].unique()  
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name,vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))

print(feature_columns)

X = final_dataset.iloc[:,final_dataset.columns!= 'Price_euros']
y = final_dataset['Price_euros']
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0)

def make_input_fn(data_df,label_df,num_epochs=10,shuffle=True,batch_size=32):
  def input_function(): 
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df),label_df)) 
    if shuffle:
      ds = ds.shuffle(1000)  
    ds = ds.batch(batch_size).repeat(num_epochs)  
    return ds 
  return input_function 
train_input_fn = make_input_fn(X_train,y_train) 
eval_input_fn = make_input_fn(X_test,y_test,num_epochs=1,shuffle=False)

linear_est = tf.estimator.LinearRegressor(
    feature_columns,model_dir=None,label_dimension=1,weight_column=None,optimizer='Ftrl',config=None,warm_start_from=None,sparse_combiner='sum'
)

linear_est.train(train_input_fn) 
result = linear_est.evaluate(eval_input_fn)

clear_output()  
print(result) 

我得到的输出是:

{'average_loss': 1481760.6,'label/mean': 1178.7135,'loss': 1454420.2,'prediction/mean': 202.05832,'global_step': 290}

多次训练模型后,平均损失和损失在减少,预测均值越来越接近标签/均值,而全局步长一直在增加。我应该继续训练模型直到标签/平均值和预测/平均值相等吗?

有没有更好的方法来改进预测?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?