我无法创建多变量回归线

时间:2019-06-25 15:04:21

标签: python pandas regression reshape

enter image description here我正在尝试使用csv文件中的数据进行多变量回归

有人告诉我我需要在错误消息中重塑数组,但是当我这样做时,它说我无法重塑列表/数组。

import csv
import numpy as np
import random
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn import linear_model

phillies_of = pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIES.csv', header = None)
phillies_pr = pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIES_PR.csv',header = None)

fielding_percentage = [int(i) for i in phillies_of.iloc[1:,25]]
runs_per_game = [int(i) for i in phillies_of.iloc[1:,5]]
total_runs = [int(i) for i in phillies_of.iloc[1:,9]]
strikeouts = [int(i) for i in phillies_of.iloc[1:,18]]
walks = [int(i) for i in phillies_of.iloc[1:,17]]
errors = [int(i) for i in phillies_of.iloc[1:,23]]
hits = [int(i) for i in phillies_of.iloc[1:,10]]
homerun = [int(i) for i in phillies_of.iloc[1:,13]]
slugging_percentage = [int(i) for i in phillies_of.iloc[1:,21]]
fan_attendance = phillies_pr.loc[1:,7]= 
phillies_pr.loc[1:,7].str.replace('\$|,','').astype(int)
payroll = phillies_pr.loc[1:,7]= 
phillies_pr.loc[1:33,10].str.replace('\$|,','').astype(int)

y = [int(i) for i in phillies_of.iloc[1:,2]]
x = [fielding_percentage, runs_per_game, total_runs, strikeouts, walks, errors, hits, homerun, slugging_percentage, fan_attendance, payroll]

reg = linear_model.LinearRegression()
reg.fit(x, y)

回溯(最近通话最近):   文件“ / Users / hannahbeegle / Desktop / Text Files / TeamDataBase.py”,第73行,在     reg.fit(x,y)   文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/linear_model/base.py”,第463行,适合     y_numeric = True,multi_output = True)   文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py”,第719行,在check_X_y中     estimator = estimator)   文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py”,行521,在check_array中     “如果包含单个样本。”。format(array)) ValueError:预期的2D数组,而是1D数组: 数组= [列表([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0])  列表([4,4,4,4,3,4,4,4,3,4,4,4,5,5,4,4,5,4,4,5,5,5,4,4,4 ,4、4、4、4、4、3、3、4、3、3、3、4、4、3、3、4、3、4、3、3、4、4、4、4、3、4 ,4、3、3、4、4、4、4、4、4、4、4])  列表([759,732,649,573,573,688,700,641,738,735,753,810,849,769,803,907,708,729,810,840,826,791,773,645 ,542,767,682,749,682,584,555,747,615,632,632,746,739,395,630,669,600,678,620,583,661,799,628,643,736 ,691、514、631、782、708、803、677、730、712、724])  列表([1290,1184,1240,1107,1369,1384,1289,1260,1140,1064,1023,1149,1169,1084,1158,933,1028,1039,1010,962,1062,1160,1032,933 ,668,946,924,906,1010,996,848,834,904,849,896,847,869,540,899,818,874,876,811,759,772,870,770,747,736 ,665、782、947、913、976、825、954、975、880、793])  列表([511,474,502,471,472,542,567,504,634,602,618,534,526,534,587,545,558,493,595,608,548,597,530,520 ,377,560,493,563,473,485,432,641,538,553,555,582,554,321,434,490,550,537,589,543,571,608,532,434,522 ,485、414、512、512、408、486、525、581、534、463])  列表([80,97,101,90,85,85,86,83,126,96,107,107,99,86,116,121,114,103,129,111,91,114,130,100 ,81,108,109,138,158,152,151,116,141,159,139,137,137,102,162,183,153,175,167,175,132,166,156,146,141 ,115、125、138、154、140、143、129、124、111、141])  列表([1433,1467,1404,1361,1316,1354,1341,1345,1411,1459,1514,1562,1510,1453,1503,1608,1428,1432,1490,1481,1489,1490,1514,1202 ,1031、1444、1391、1407、1376、1281、1319、1401、1348、1359、1338、1489、1411、886、1352、1389、1313、1404、1309、1323、1375、1497、1363、1434、1495 ,1411、1399、1307、1476、1419、1522、1345、1376、1365、1393])  列表([175,165,122,100,123,181,149,173,139,149,130,176,222,184,178,235,164,174,179,197,215,174,197,168 ,137,169,138,141,162,128,96,152,138,126,111,130,146,64,144,126,123,139,82,107,120,206,144,153,160 ,141、80、158、207、196、159、139、181、188、170])  列表([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0、0、0、0、0、0、0、0、0、0])  1 2158124 2 1905354 3 1915144 41831080 5 2423852 6 3012403 7 3565718 8 3680718 9 3777322 10 3600693 11 3422583 12 3108325 13 2701815 14 2665304 15 3250092 16 2259948 17 1618467 18 1782054 19 1612769 20 1825337 21 1715722 22 1490638 23 1801677 24 2043598 25 2290971 26 3137674 27 1927448 28 2050012 29 1992484 30 1861985 1990041 31 32 2100110 33 1933335 34 1830350 35 2062693 36 2128339 37 2376394 38 1638752 39 2651650 40 2775011 41 2583389 42 2700070 43 2480150 44 1909233 45 1808648 46 1475934 47 1343329 48 1511223 49 708247 50 519414 51 664546 52 828888 53 1108201 54 1166376 55 1425891 56 907141 57 762034 58 590039 59 862205 名称:7,dtype:int64  1 93874333 2 86276000 3 84846666 4 103082167 5 176444967 6 150860000 7 171501558 8 172976379 9 141928379 10 115479046 11 97879880 12 89428213 13 88273333 14 95522000 15 93219167 16 70780000 17 57954999 18 41663833 19 47513000 20 31897500 21 36297500 22 36656500 23 34314500 24 30555945 25 31599000 26 28538334 27 24492834 28 22487332 29 13740167 30 10779000 31 13900500 32 12482997 33 11590166 名称:10,dtype:int64]。 如果数据具有单个功能,则使用array.reshape(-1,1)来重塑数据;如果包含单个样本,则使用array.reshape(1,-1)来重塑数据。

  
    

>

  

0 个答案:

没有答案