Tensorflow dnnregressor一直给我相同的预测值

时间:2020-02-07 23:53:52

标签: python tensorflow machine-learning deep-learning regression

我正在使用Tensorflows dnnregressor,并且当我重新加载数据集以获取经过一定数量的行训练后的神经网络的预测值时,预测值都相同。我曾尝试更改学习率以及隐藏层和神经元的数量,但似乎没有任何效果。

这是我的代码:

import pandas as pd
from IPython.display import display
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
import warnings
with warnings.catch_warnings():  
    warnings.filterwarnings("ignore",category=FutureWarning)
    import tensorflow as tf
import pickle       # Used to save the model
import re
import csv
import logging
import os
from sklearn.model_selection import train_test_split
regex = re.compile(r"\[|\]|<", re.IGNORECASE)
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from mlxtend.plotting import plot_confusion_matrix

# Removes annoying warning messages in tensorflow and python
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='error', category=FutureWarning)
import sys
if not sys.warnoptions:
    warnings.simplefilter("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.logging.set_verbosity(tf.logging.ERROR)
tf.get_logger().setLevel(3)
tf.get_logger().setLevel('INFO')
tf.get_logger().setLevel(logging.ERROR)
logging.getLogger('tensorflow').disabled = True

all_data = pd.read_csv('ML_DATASET.csv')

all_data = all_data.fillna(0)

# Create training and test set
all_data4 = all_data.iloc[:,0:]
all_data4.columns = all_data4.columns.str.replace('+', 'plus')
all_data4.columns = all_data4.columns.str.replace(')', ' ')
all_data4.columns = all_data4.columns.str.replace('!', ' ')
all_data4.columns = all_data4.columns.str.replace('(', ' ')
all_data4.columns = all_data4.columns.str.replace(',', ' ')
all_data4.columns = all_data4.columns.str.replace(' ', '_')
all_data4.columns = all_data4.columns.str.replace('__', '_')
all_data4.columns = all_data4.columns.str.replace('%', 'percentage')
all_data4.columns = all_data4.columns.str.replace('$', '')
all_data4.columns = all_data4.columns.str.replace('<', 'lessthan') 
all_data4 = all_data4.dropna(subset = ['3_year_appreciation'])
train_dataset = all_data4.sample(frac=0.8,random_state=42)
test_dataset = all_data4.drop(train_dataset.index)
train_stats = train_dataset.describe()
train_stats.pop('3_year_appreciation')
train_stats = train_stats.transpose()
train_labels = train_dataset.pop('3_year_appreciation')
test_labels = test_dataset.pop('3_year_appreciation')
# Need to change feature columns to be of numeric type
feature_columns = ['Unweighted_Sample_Count_of_the_population', 'Avg_household_size_of_occupied_housing_units', 'Total_population_in_occupied_housing_units', 'Median_Estimated_Home_Value_owner_occupied_units_', 'Total_Population', 'Median_Gross_rent_as_percentage_of_household_inc', 'White_Population', 'Black/African_American_Population', 'Native_American_Population', 'Asian_Population', 'Pacific_Islander_Population', 'Some_other_race_Population', 'Mixed_Race_Population', 'Median_Age', 'Median_Household_Income', 'Total_Population_over_25', 'B15003_022E', 'B15003_023E', 'B15003_024E', 'B15003_025E', 'Median_Gross_Rent', 'Homeowner_households', 'Renter_households', 'Housing_units_with_mortgage', 'B19001_002E', 'B19001_003E', 'B19001_004E', 'B19001_005E', 'B19001_006E', 'B19001_007E', 'B19001_013E', 'B19001_014E', 'B19001_015E', 'B19001_016E', 'B19001_017E', 'Total_housing_Units', 'B25024_006E', 'B25024_007E', 'B25024_008E', 'B25024_009E', 'Total_Units', 'Units_with_9plus_Rooms', 'Families_making_more_than_5x_poverty_level_income', 'People_who_moved_in_the_past_year_within_same_county', 'Moved_within_same_state_but_not_same_county', 'Moved_from_different_state_same_country', 'Moved_from_different_country', 'Median_age_of_people_who_moved_from_different_state', 'Moved_within_same_county_bachelors_degree', 'Moved_from_different_state_At_or_above_150_percent_of_the_poverty_level', 'Number_of_people_who_work_at_home', 'Number_of_people_who_walk_to_work', 'White_women_25-29', 'Born_in_germany_population', 'Number_of_people_who_take_Non_taxi_public_transport_to_work', 'Number_of_people_who_work_in_county_government', 'Number_of_people_whose_Commuting_time_under_10_mins', 'Number_of_people_whose_commute_is_45-60_mins', 'Number_of_people_whose_commute_is_60-90_mins', 'Number_of_people_whose_commute_is_90plus_mins', 'Number_of_Sales_and_office_workers', 'Number_of_people_in_management_business_science_and_arts', 'Number_of_service_workers', 'Number_of_educational_and_health_service_workers', 'Number_of_arts_entertainment_and_food_service_workers', 'Number_of_finance_and_real_estate_workers', 'Number_of_tech_workers', 'Private_for-profit_wage_and_salary_workers', 'Self-employed_in_own_incorporated_business_workers', 'Local_government_workers', 'Federal_government_workers', 'Self-employed_in_own_not_incorporated_business_workers', 'People_in_households_receiving_SNAP_and_extra_social_security_income', 'Civilians_aged_25-64_with_more_than_a_bachelors_degree', 'Men_over_16_in_Education_legal_community_service_arts_and_media_occupations', 'Men_over_16_in_Food_preparation_and_serving_related_occupations', 'B08006_002E', 'B08006_003E', 'B08006_004E', 'B08006_005E', 'B08006_006E', 'B08006_007E', 'B08006_009E', 'B08006_010E', 'B08006_011E', 'B08006_012E', 'B08006_013E', 'B08006_014E', 'B08006_016E', 'B08006_019E', 'B08006_020E', 'B08006_021E', 'B08006_022E', 'B08006_023E', 'B08006_024E', 'B08006_025E', 'B08006_026E', 'B08006_027E', 'B08006_028E', 'B08006_029E', 'B08006_030E', 'B08006_031E', 'B08006_032E', 'B08006_033E', 'B08006_034E', 'B08006_036E', 'B08006_037E', 'B08006_038E', 'B08006_039E', 'B08006_040E', 'B08006_041E', 'B08006_042E', 'B08006_043E', 'B08006_044E', 'B08006_045E', 'B08006_046E', 'B08006_047E', 'B08006_048E', 'B08006_049E', 'B08006_050E', 'B08006_051E', 'B08007_002E', 'B08007_004E', 'B08007_005E', 'B08007_007E', 'B08007_008E', 'B08007_009E', 'B08007_010E', 'B08007_012E', 'B08007_013E', 'B08007_014E', 'B08007_015E', 'B08008_002E', 'B08008_003E', 'B08008_004E', 'B08008_005E', 'B08008_006E', 'B08008_007E', 'B08008_008E', 'B08008_009E', 'B08008_010E', 'B08008_011E', 'B08008_012E', 'B08008_013E', 'B08008_014E', 'B08013_001E', 'B08013_002E', 'B08013_003E', 'B08014_002E', 'B08014_003E', 'B08014_004E', 'B08014_005E', 'B08014_006E', 'B08014_007E', 'B08014_009E', 'B08014_010E', 'B08014_011E', 'B08014_012E', 'B08014_013E', 'B08014_014E', 'B08014_016E', 'B08014_017E', 'B08014_018E', 'B08014_019E', 'B08014_020E', 'B08014_021E', 'B08015_001E', 'B08015_002E', 'B08015_003E', 'B08105A_004E', 'B08105B_003E', 'B08111_002E', 'B08111_003E', 'B08111_004E', 'B08111_005E', 'B08113_002E', 'B08113_003E', 'B08113_004E', 'B08113_005E', 'B08113_006E', 'B08113_007E', 'B08113_008E', 'B13002_002E', 'B13002_003E', 'B13002_004E', 'B13002_005E', 'B13002_006E', 'B13002_007E', 'B13002_008E', 'B13002_009E', 'B13002_010E', 'B13002_011E', 'B13002_012E', 'B13002_013E', 'B13002_014E', 'B13002_015E', 'B13002_016E', 'B13002_017E', 'B13002_018E', 'B13002_019E', 'B13002A_002E', 'B13002A_003E', 'B13002A_004E', 'B13002A_005E', 'B13002A_006E', 'B13002A_007E', 'B13002B_002E', 'B13002B_003E', 'B13002B_004E', 'B13002B_005E', 'B13002B_006E', 'B13002B_007E', 'B13002C_002E', 'B13002C_003E', 'B13002C_004E', 'B13002C_005E', 'B13002C_006E', 'B13002C_007E', 'B13002D_002E', 'B13002D_003E', 'B13002D_004E', 'B13002D_005E', 'B13002D_006E', 'B13002D_007E', 'B13002E_002E', 'B13002E_003E', 'B13002E_004E', 'B13002E_005E', 'B13002E_006E', 'B13002E_007E', 'B13002F_002E', 'B13002F_003E', 'B13002F_004E', 'B13002F_005E', 'B13002F_006E', 'B13002F_007E', 'B13002G_002E', 'B13002G_003E', 'B13002G_004E', 'B13002G_005E', 'B13002G_006E', 'B13002G_007E', 'B13002H_002E', 'B13002H_003E', 'B13002H_004E', 'B13002H_005E', 'B13002H_006E', 'B13002H_007E', 'B13002I_002E', 'B13002I_003E', 'B13002I_004E', 'B13002I_005E', 'B13002I_006E', 'B13002I_007E', 'B13004_002E', 'B13004_003E', 'B13004_004E', 'B13004_005E', 'B13004_006E', 'B13004_007E', 'B13004_008E', 'B13004_009E', 'B13004_010E', 'B13004_011E', 'B13008_002E', 'B13008_003E', 'B13008_004E', 'B13008_005E', 'B13008_006E', 'B13008_007E', 'B13008_008E', 'B13008_009E', 'B13008_010E', 'B13008_011E', 'B13008_012E', 'B13008_013E', 'B13008_014E', 'B13008_015E', 'B13010_002E', 'B13010_003E', 'B13010_004E', 'B13010_005E', 'B13010_006E', 'B13010_007E', 'B13010_008E', 'B13010_009E', 'B13010_010E', 'B13010_011E', 'B13010_012E', 'B13010_013E', 'B13010_014E', 'B13010_015E', 'B13010_016E', 'B13010_017E', 'B13010_018E', 'B13010_019E', 'B13012_002E', 'B13012_003E', 'B13012_004E', 'B13012_005E', 'B13012_006E', 'B13012_007E', 'B13012_008E', 'B13012_009E', 'B13012_010E', 'B13012_011E', 'B13012_012E', 'B13012_013E', 'B13012_014E', 'B13012_015E', 'B13014_002E', 'B13014_003E', 'B13014_004E', 'B13014_005E', 'B13014_006E', 'B13014_007E', 'B13014_008E', 'B13014_009E', 'B13014_010E', 'B13014_011E', 'B13014_012E', 'B13014_013E', 'B13014_014E', 'B13014_015E', 'B13014_016E', 'B13014_017E', 'B13014_018E', 'B13014_019E', 'B13014_020E', 'B13014_021E', 'B13014_022E', 'B13014_023E', 'B13014_024E', 'B13014_025E', 'B13014_026E', 'B13014_027E', 'B13015_002E', 'B13015_003E', 'B13015_004E', 'B13015_005E', 'B13015_006E', 'B13015_007E', 'B13015_008E', 'B13015_009E', 'B13015_010E', 'B13015_011E', 'B13015_012E', 'B13015_013E', 'B13015_014E', 'B13015_015E', 'B13016_002E', 'B13016_003E', 'B13016_004E', 'B13016_005E', 'B13016_006E', 'B13016_007E', 'B13016_008E', 'B13016_009E', 'B13016_010E', 'B13016_011E', 'B13016_012E', 'B13016_013E', 'B13016_014E', 'B13016_015E', 'B13016_016E', 'B13016_017E', 'B14001_002E', 'B14001_003E', 'B14001_004E', 'B14001_005E', 'B14001_006E', 'B14001_007E', 'B14001_008E', 'B14001_009E', 'B14001_010E', 'B14002_003E', 'B14002_004E', 'B14002_005E', 'B14002_006E', 'B14002_007E', 'B14002_008E', 'B14002_009E', 'B14002_010E', 'B14002_011E', 'B14002_012E', 'B14002_013E', 'B14002_014E', 'B14002_015E', 'B14002_016E', 'B14002_017E', 'B14002_018E', 'B14002_019E', 'B14002_020E', 'B14002_021E', 'B14002_022E', 'B14002_023E', 'B14002_024E', 'B14002_025E', 'B14002_027E', 'B14002_028E', 'B14002_029E', 'B14002_030E', 'B14002_031E', 'B14002_032E', 'B14002_033E', 'B14002_034E', 'B14002_035E', 'B14002_036E', 'B14002_037E', 'B14002_038E', 'B14002_039E', 'B14002_040E', 'B14002_041E', 'B14002_042E', 'B14002_043E', 'B14002_044E', 'B14002_045E', 'B14002_046E', 'B14002_047E', 'B14002_048E', 'B14002_049E', 'B14003_003E', 'B14003_004E', 'B14003_005E', 'B14003_006E', 'B14003_007E', 'B14003_008E', 'B14003_009E', 'B14003_010E', 'B14003_011E', 'B14003_012E', 'B14003_013E', 'B14003_014E', 'B14003_015E', 'B14003_016E', 'B14003_017E', 'B14003_018E', 'B14003_019E', 'B14003_020E', 'B14003_021E', 'B14003_022E', 'B14003_023E', 'B14003_024E', 'B14003_025E', 'B14003_026E', 'B14003_027E', 'B14003_028E', 'B14003_029E', 'B14003_031E', 'B14003_032E', 'B14003_033E', 'B14003_034E', 'B14003_035E', 'B14003_036E', 'B14003_037E', 'B14003_038E', 'B14003_039E', 'B14003_040E', 'B14003_041E', 'B14003_042E', 'B14003_043E', 'B14003_044E', 'B14003_045E', 'B14003_046E', 'B14003_047E', 'B14003_048E', 'B14003_049E', 'B14003_050E', 'B14003_051E', 'B14003_052E', 'B14003_053E', 'B14003_054E', 'B14003_055E', 'B14003_056E', 'B14003_057E', 'B14004_003E', 'B14004_004E', 'B14004_005E', 'B14004_006E', 'B14004_007E', 'B14004_008E', 'B14004_009E', 'B14004_010E', 'B14004_011E', 'B14004_012E', 'B14004_013E', 'B14004_014E', 'B14004_015E', 'B14004_016E', 'B14004_017E', 'B14004_019E', 'B14004_020E', 'B14004_021E', 'B14004_022E', 'B14004_023E', 'B14004_024E', 'B14004_025E', 'B14004_026E', 'B14004_027E', 'B14004_028E', 'B14004_029E', 'B14004_030E', 'B14004_031E', 'B14004_032E', 'B14004_033E', 'B14005_003E', 'B14005_004E', 'B14005_005E', 'B14005_006E', 'B14005_007E', 'B14005_008E', 'B14005_009E', 'B14005_010E', 'B14005_011E', 'B14005_012E', 'B14005_013E', 'B14005_014E', 'B14005_015E', 'B14005_017E', 'B14005_018E', 'B14005_019E', 'B14005_020E', 'B14005_021E', 'B14005_022E', 'B14005_023E', 'B14005_024E', 'B14005_025E', 'B14005_026E', 'B14005_027E', 'B14005_028E', 'B14005_029E', 'B14006_002E', 'B14006_003E', 'B14006_004E', 'B14006_005E', 'B14006_006E', 'B14006_007E', 'B14006_008E', 'B14006_009E', 'B14006_010E', 'B14006_011E', 'B14006_012E', 'B14006_013E', 'B14006_014E', 'B14006_015E', 'B14006_016E', 'B14006_017E', 'B14006_018E', 'B14006_019E', 'B14006_020E', 'B14006_021E', 'B14007_003E', 'B14007_004E', 'B14007_005E', 'B14007_006E', 'B14007_007E', 'B14007_008E', 'B14007_009E', 'B14007_010E', 'B14007_011E', 'B14007_012E', 'B14007_013E', 'B14007_014E', 'B14007_015E', 'B14007_016E', 'B14007_017E', 'B14007_018E', 'B14007A_003E', 'B14007A_004E', 'B14007A_005E', 'B14007A_006E', 'B14007A_007E', 'B14007A_008E', 'B14007A_009E', 'B14007A_010E', 'B14007A_011E', 'B14007A_012E', 'B14007A_013E', 'B14007A_014E', 'B14007A_015E', 'B14007A_016E', 'B14007A_017E', 'B14007A_018E', 'B14007A_019E', 'B14007B_002E', 'B14007B_003E', 'B14007B_004E', 'B14007B_005E', 'B14007B_006E', 'B14007B_007E', 'B14007B_008E', 'B14007B_009E', 'B14007B_010E', 'B14007B_011E', 'B14007B_012E', 'B14007B_013E', 'B14007B_014E', 'B14007B_015E', 'B14007B_016E', 'B14007B_017E', 'B14007B_018E', 'B14007B_019E', 'B14007C_002E', 'B14007C_003E', 'B14007C_004E', 'B14007C_005E', 'B14007C_006E', 'B14007C_007E', 'B14007C_008E', 'B14007C_009E', 'B14007C_010E', 'B14007C_011E', 'B14007C_012E', 'B14007C_013E', 'B14007C_014E', 'B14007C_015E', 'B14007C_016E', 'B14007C_017E', 'B14007C_018E', 'B14007C_019E', 'B14007D_002E', 'B14007D_003E', 'B14007D_004E', 'B14007D_005E', 'B14007D_006E', 'B14007D_007E', 'B14007D_008E', 'B14007D_009E', 'B14007D_010E', 'B14007D_011E', 'B14007D_012E', 'B14007D_013E', 'B14007D_014E', 'B14007D_015E', 'B14007D_016E', 'B14007D_017E', 'B14007D_018E', 'B14007D_019E', 'B19054_002E', 'B19054_003E', 'B19055_002E', 'B19055_003E', 'B19056_002E', 'B19056_003E', 'B19057_002E', 'B19057_003E', 'B19058_002E', 'B19058_003E', 'B19059_002E', 'B19059_003E', 'B19060_002E', 'B19060_003E', 'B08016_002E', 'B08016_003E', 'B08016_004E', 'B08016_005E', 'B08016_006E', 'B08016_007E', 'B08016_008E', 'B08016_009E', 'B08016_010E', 'B08016_011E', 'B08016_012E', 'B08016_013E', 'B08016_014E', 'B08016_015E', 'B08016_016E', 'B08016_017E', 'B08016_018E', 'B08016_019E', 'B08016_020E', 'B08016_021E', 'B08016_022E', 'B08016_023E', 'B08017_002E', 'B08017_003E', 'B08017_004E', 'B08017_005E', 'B08017_006E', 'B08017_007E', 'B08017_008E', 'B08017_009E', 'B08017_010E', 'B08017_011E', 'B08017_012E', 'B08017_013E', 'B08017_015E', 'B08017_016E', 'B08017_017E', 'B08017_018E', 'B08017_019E', 'B08017_020E', 'B08017_021E', 'B08017_022E', 'B08017_023E', 'B08018_002E', 'B08018_003E', 'B08018_004E', 'B08018_005E', 'B08018_006E', 'B08018_007E', 'B08018_008E', 'B08101_049E', 'B08105A_007E', 'B08105B_007E', 'B08105C_007E', 'B08105D_007E', 'B08105E_007E', 'B08105F_007E', 'B08105G_007E', 'B08105H_007E', 'B08105I_007E', 'B08111_031E', 'B08113_049E', 'B08119_055E', 'B08121_007E', 'B08122_025E', 'B08122_026E', 'B08122_027E', 'B08122_028E', 'B24080_003E', 'B24080_004E', 'B24080_005E', 'B24080_006E', 'B24080_007E', 'B24080_008E', 'B24080_009E', 'B24080_010E', 'B24080_011E', 'B24080_012E', 'B24080_013E', 'B24080_014E', 'B24080_015E', 'B24080_016E', 'B24080_017E', 'B24080_018E', 'B24080_019E', 'B24080_020E', 'B24080_021E', 'B24081_001E', 'B24081_002E', 'B24081_003E', 'B24081_004E', 'B24081_005E', 'B24081_006E', 'B24081_007E', 'B24081_008E', 'B24081_009E', 'B24082_001E', 'B24082_002E', 'B24082_003E', 'B24082_004E', 'B24082_005E', 'B24082_006E', 'B24082_007E', 'B24082_008E', 'B24082_009E', 'B24082_010E', 'B24082_011E', 'B24082_012E', 'B24082_013E', 'B24082_014E', 'B24082_015E', 'B24082_016E', 'B24082_017E', 'B24082_018E', 'B24090_001E', 'B24090_002E', 'B24090_003E', 'B24090_004E', 'B24090_005E', 'B24090_006E', 'B24090_007E', 'B24090_008E', 'B24090_009E', 'B24090_010E', 'B24090_011E', 'B24090_012E', 'B24090_013E', 'B24090_014E', 'B24090_015E', 'B24090_016E', 'B24090_017E', 'B24090_018E', 'B24090_019E', 'B24090_020E', 'B24090_021E', 'B24091_001E', 'B24091_002E', 'B24091_003E', 'B24091_004E', 'B24091_005E', 'B24091_006E', 'B24091_007E', 'B24091_008E', 'B24091_009E', 'B24092_001E', 'B24092_002E', 'B24092_003E', 'B24092_004E', 'B24092_005E', 'B24092_006E', 'B24092_007E', 'B24092_008E', 'B24092_009E', 'B24092_010E', 'B24092_011E', 'B24092_012E', 'B24092_013E', 'B24092_014E', 'B24092_015E', 'B24092_016E', 'B24092_017E', 'B24092_018E', 'C24040_001E', 'C24040_002E', 'C24040_003E', 'C24040_004E', 'C24040_005E', 'C24040_006E', 'C24040_007E', 'C24040_008E', 'C24040_009E', 'C24040_010E', 'C24040_011E', 'C24040_012E', 'C24040_013E', 'C24040_014E', 'C24040_015E', 'C24040_016E', 'C24040_017E', 'C24040_018E', 'C24040_019E', 'C24040_020E', 'C24040_021E', 'C24040_022E', 'C24040_023E', 'C24040_024E', 'C24040_025E', 'C24040_026E', 'C24040_027E', 'C24040_028E', 'C24040_029E', 'C24040_030E', 'C24040_031E', 'C24040_032E', 'C24040_033E', 'C24040_034E', 'C24040_035E', 'C24040_036E', 'C24040_037E', 'C24040_038E', 'C24040_039E', 'C24040_040E', 'C24040_041E', 'C24040_042E', 'C24040_043E', 'C24040_044E', 'C24040_045E', 'C24040_046E', 'C24040_047E', 'C24040_048E', 'C24040_049E', 'C24040_050E', 'C24040_051E', 'C24040_052E', 'C24040_053E', 'C24040_054E', 'C24040_055E', 'C24050_001E', 'C24050_002E', 'C24050_003E', 'C24050_004E', 'C24050_005E', 'C24050_006E', 'C24050_007E', 'C24050_008E', 'C24050_009E', 'C24050_010E', 'C24050_011E', 'C24050_012E', 'C24050_013E', 'C24050_014E', 'C24050_015E', 'C24050_016E', 'C24050_017E', 'C24050_018E', 'C24050_019E', 'C24050_020E', 'C24050_021E', 'C24050_022E', 'C24050_023E', 'C24050_024E', 'C24050_025E', 'C24050_026E', 'C24050_027E', 'C24050_028E', 'C24050_029E', 'C24050_030E', 'C24050_031E', 'C24050_032E', 'C24050_033E', 'C24050_034E', 'C24050_035E', 'C24050_036E', 'C24050_037E', 'C24050_038E', 'C24050_039E', 'C24050_040E', 'C24050_041E', 'C24050_042E', 'C24050_043E', 'C24050_044E', 'C24050_045E', 'C24050_046E', 'C24050_047E', 'C24050_048E', 'C24050_049E', 'C24050_050E', 'C24050_051E', 'C24050_052E', 'C24050_053E', 'C24050_054E', 'C24050_055E', 'C24050_056E', 'C24050_057E', 'C24050_058E', 'C24050_059E', 'C24050_060E', 'C24050_061E', 'C24050_062E', 'C24050_063E', 'C24050_064E', 'C24050_065E', 'C24050_066E', 'C24050_067E', 'C24050_068E', 'C24050_069E', 'C24050_070E', 'C24050_071E', 'C24050_072E', 'C24050_073E', 'C24050_074E', 'C24050_075E', 'C24050_076E', 'C24050_077E', 'C24050_078E', 'C24050_079E', 'C24050_080E', 'C24050_081E', 'C24050_082E', 'C24050_083E', 'C24050_084E', 'C24060_001E', 'C24060_002E', 'C24060_003E', 'C24060_004E', 'C24060_005E', 'C24060_006E', 'C24060_007E', 'C24060_008E', 'C24060_009E', 'C24060_010E', 'C24060_011E', 'C24060_012E', 'C24060_013E', 'C24060_014E', 'C24060_015E', 'C24060_016E', 'C24060_017E', 'C24060_018E', 'C24060_019E', 'C24060_020E', 'C24060_021E', 'C24060_022E', 'C24060_023E', 'C24060_024E', 'C24060_025E', 'C24060_026E', 'C24060_027E', 'C24060_028E', 'C24060_029E', 'C24060_030E', 'C24060_031E', 'C24060_032E', 'C24060_033E', 'C24060_034E', 'C24060_035E', 'C24060_036E', 'C24070_001E', 'C24070_002E', 'C24070_003E', 'C24070_004E', 'C24070_005E', 'C24070_006E', 'C24070_007E', 'C24070_008E', 'C24070_009E', 'C24070_010E', 'C24070_011E', 'C24070_012E', 'C24070_013E', 'C24070_014E', 'C24070_015E', 'C24070_016E', 'C24070_017E', 'C24070_018E', 'C24070_019E', 'C24070_020E', 'C24070_021E', 'C24070_022E', 'C24070_023E', 'C24070_024E', 'C24070_025E', 'C24070_026E', 'C24070_027E', 'C24070_029E', 'C24070_030E', 'C24070_031E', 'C24070_032E', 'C24070_033E', 'C24070_034E', 'C24070_035E', 'C24070_036E', 'C24070_037E', 'C24070_038E', 'C24070_039E', 'C24070_040E', 'C24070_041E', 'C24070_043E', 'C24070_044E', 'C24070_045E', 'C24070_046E', 'C24070_047E', 'C24070_048E', 'C24070_049E', 'C24070_050E', 'C24070_051E', 'C24070_052E', 'C24070_053E', 'C24070_054E', 'C24070_055E', 'C24070_057E', 'C24070_058E', 'C24070_059E', 'C24070_060E', 'C24070_061E', 'C24070_062E', 'C24070_063E', 'C24070_064E', 'C24070_065E', 'C24070_066E', 'C24070_067E', 'C24070_068E', 'C24070_069E', 'C24070_070E', 'C24070_071E', 'C24070_072E', 'C24070_073E', 'C24070_074E', 'C24070_075E', 'C24070_076E', 'C24070_077E', 'C24070_078E', 'C24070_079E', 'C24070_080E', 'C24070_081E', 'C24070_082E', 'C24070_083E', 'C24070_084E', 'B27001_004E', 'B27001_005E', 'B27001_006E', 'B27001_007E', 'B27001_008E', 'B27001_009E', 'B27001_010E', 'B27001_011E', 'B27001_012E', 'B27001_013E', 'B27001_014E', 'B27001_015E', 'B27001_016E', 'B27001_017E', 'B27001_018E', 'B27001_019E', 'B27001_020E', 'B27001_021E', 'B27001_022E', 'B27001_023E', 'B27001_024E', 'B27001_025E', 'B27001_026E', 'B27001_027E', 'B27001_028E', 'B27001_029E', 'B27001_030E', 'B27001_031E', 'B27001_032E', 'B27001_033E', 'B27001_034E', 'B27001_035E', 'B27001_036E', 'B27001_037E', 'B27001_038E', 'B27001_039E', 'B27001_040E', 'B27001_041E', 'B27001_042E', 'B27001_043E', 'B27001_044E', 'B27001_045E', 'B27001_046E', 'B27001_047E', 'B27001_048E', 'B27001_049E', 'B27001_050E', 'B27001_051E', 'B27001_052E', 'B27001_053E', 'B27001_054E', 'B27001_055E', 'B27001_056E', 'B27001_057E', 'Bachelorsplus', 'Households_with_Income_lessthan35k', 'Households_with_Income_100kplus', 'Pct_of_housing_units_in_4plus_unit_buildings']
feat_cols = []
for x in feature_columns:
    x.strip()
    feat_cols.append(tf.feature_column.numeric_column(x))   

# # Normalize data
def norm(x):
    return (x - train_stats['mean']) / train_stats['std']

X_train = norm(train_dataset)
y_train = train_labels
X_test = norm(test_dataset)
y_test = test_labels

# Define the input function
BATCH_SIZE = 10
epochs = None


input_func=tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train,batch_size=BATCH_SIZE,num_epochs=None,shuffle=True)
eval_input_func = tf.estimator.inputs.pandas_input_fn(x=X_test,
                                                      y=y_test, 
                                                      batch_size=10, 
                                                      num_epochs=1, 
                                                      shuffle=False)
test_input_func = tf.estimator.inputs.pandas_input_fn(x= X_test,                                                   
                                                 batch_size=100, 
                                                 num_epochs=1, 
                                                 shuffle=False)

dnn_regressor = tf.estimator.DNNRegressor(
    feature_columns=feat_cols,
    hidden_units=[1024, 512, 256],
    optimizer=tf.train.ProximalAdagradOptimizer(
      learning_rate=0.01,
      l1_regularization_strength=0.01
    ))

# Train model
dnn_regressor.train(input_fn=input_func,steps=1000)

# Predictions
pred_input_func=tf.estimator.inputs.pandas_input_fn(x=X_test,batch_size=BATCH_SIZE,num_epochs=1,shuffle=False)

predictions=list(dnn_regressor.predict(input_fn=pred_input_func))

# Clear Cache
all_data = pd.DataFrame()
all_data4 = pd.DataFrame()
X_train = pd.DataFrame()
y_train = pd.DataFrame()
X_test = pd.DataFrame()
y_test = pd.DataFrame()
train_dataset = pd.DataFrame()
test_dataset = pd.DataFrame()
train_stats = pd.DataFrame()
train_labels = pd.DataFrame()
test_labels = pd.DataFrame()


#Normalize function
def norm(x,train_stats):
    return (x - train_stats['mean']) / train_stats['std']

#Append_machine_learning_outputs
def append_ML_outputs(dataframe, year, dnn_regressor):
    dataframe = dataframe[dataframe['Year'].isin([year])]
    print(len(dataframe))
    cols = dataframe.columns.tolist()
    cols = cols[-2:] + cols[:-2]
    cols.insert(0, cols.pop(cols.index('LocationplusType')))
    dataframe=dataframe[cols]
    dataframe = dataframe.replace([np.inf, -np.inf], np.nan)
    dataframe = dataframe.fillna(0)
    print(len(dataframe))
    stats = dataframe.describe()
    stats = stats.transpose()
    dataframe3 = dataframe.drop(['LocationplusType','Tract_number','Year'],axis=1)
    print(len(dataframe3))
    normed_data = norm(dataframe3,stats)
    normed_data = pd.merge(dataframe[['LocationplusType','Tract_number','Year']],normed_data,left_index=True,right_index=True)
    dataframe4 = normed_data.drop(['LocationplusType','Year_x','Tract_number_x'],axis=1)
    dataframe4 = dataframe4.drop(['3_year_appreciation'],axis=1)
    print(len(dataframe4))
    pred_input_func=tf.estimator.inputs.pandas_input_fn(x=dataframe4,batch_size=BATCH_SIZE,num_epochs=1,shuffle=False)
    example_result = pd.DataFrame(dnn_regressor.predict(input_fn=pred_input_func))
    orig_data = dataframe.reset_index(drop=True)
    df_test = pd.merge(orig_data[['LocationplusType','Year']],example_result,left_index=True,right_index=True)
    df_test.rename(columns={0:'Predicted Growth Rank'}, inplace=True)
    return df_test

all_data.columns = all_data.columns.str.replace('+', 'plus')
all_data.columns = all_data.columns.str.replace(')', ' ')
all_data.columns = all_data.columns.str.replace('!', ' ')
all_data.columns = all_data.columns.str.replace('(', ' ')
all_data.columns = all_data.columns.str.replace(',', ' ')
all_data.columns = all_data.columns.str.replace(' ', '_')
all_data.columns = all_data.columns.str.replace('__', '_')
all_data.columns = all_data.columns.str.replace('%', 'percentage')
all_data.columns = all_data.columns.str.replace('$', '')
all_data.columns = all_data.columns.str.replace('<', 'lessthan') 
# len(df)
df_list=[]
for year in all_data['Year'].unique():
    df_list.append(append_ML_outputs(all_data, year, dnn_regressor))
    df_final = pd.concat(df_list)
# Uncomment line below to write a new file
df_final.to_csv('predicted_values.csv',index=False)

编辑:

我现在包括一个GitHub存储库,您可以找到here。这将包括数据和源代码。为了澄清这个问题,是关于笔记本中的最后一个代码块,其中在经过一定数量的预测值后,我得到的预测值是相同的。

更新后的编辑:

我意识到较大的ML_DATA.csv文件不在我提供的github链接中。该文件的大小为3.6 GB,因此我不得不将其压缩为一个文件,然后将其推送。现在所有数据都应该在那里。

0 个答案:

没有答案