回答編集履歴

4

追記

2021/02/15 13:35

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -25,3 +25,169 @@
25
25
  `x_list3=pd.concat([price, x_list], axis=1)` ここで、x_list中にはpriceはすでに存在していますが、更にconcatしようとしているのはなぜでしょうか。
26
26
 
27
27
  これを省くと、`sns.pairplot(x_list, hue="Manhattan_dummry")`は動作するようです。
28
+
29
+
30
+
31
+ # 追記
32
+
33
+ 下記、試してみてください。
34
+
35
+ ```python3
36
+
37
+ import pandas as pd
38
+
39
+ input_book = pd.ExcelFile('AB_NYC_2019_2.xlsx')
40
+
41
+ input_sheet_name = input_book.sheet_names
42
+
43
+ num_sheet = len(input_sheet_name)
44
+
45
+ print(input_sheet_name)
46
+
47
+ print("Sheet の数:", num_sheet)
48
+
49
+ input_sheet_df = input_book.parse(input_sheet_name[0])
50
+
51
+ input_sheet_df.head(10)
52
+
53
+
54
+
55
+ import matplotlib.pyplot as plt
56
+
57
+ import scipy.stats
58
+
59
+
60
+
61
+ manhattan_dummy=input_sheet_df["Manhattan_dummry"]
62
+
63
+ private_dummy=input_sheet_df["private_dummy"]
64
+
65
+ home_dummy=input_sheet_df["home_dummy"]
66
+
67
+ shared_dummy=input_sheet_df["shared_dummy"]
68
+
69
+ price=input_sheet_df["price"]
70
+
71
+ minimum_nights=input_sheet_df["minimum_nights"]
72
+
73
+ number_of_reviews=input_sheet_df["number_of_reviews"]
74
+
75
+ reviews_per_month=input_sheet_df["reviews_per_month"]
76
+
77
+ calculated_host_listings_count=input_sheet_df["calculated_host_listings_count"]
78
+
79
+ availability_365=input_sheet_df["availability_365"]
80
+
81
+
82
+
83
+ result = scipy.stats.linregress(number_of_reviews,price)
84
+
85
+ print('傾き=', result.slope.round(4),'切片=', result.intercept.round(4), '信頼係数=', result.rvalue.round(4),
86
+
87
+ 'p値=', result.pvalue.round(4), '標準偏差=', result.stderr.round(4))
88
+
89
+ result_slope = result.slope
90
+
91
+ result_intercept = result.intercept
92
+
93
+
94
+
95
+ plt.plot(number_of_reviews, [result_slope*u + result_intercept for u in number_of_reviews])
96
+
97
+ plt.scatter(number_of_reviews,price)
98
+
99
+ plt.title('price and number_of_reviews in Airbnb Dataset')
100
+
101
+ plt.ylabel('price')
102
+
103
+ plt.xlabel('number of reviews')
104
+
105
+ plt.show()
106
+
107
+
108
+
109
+ import statsmodels.api as sm
110
+
111
+ model = sm.OLS(price, sm.add_constant(number_of_reviews))
112
+
113
+ result = model.fit()
114
+
115
+ print(result.summary())
116
+
117
+ print('p-values\n', result.pvalues)
118
+
119
+
120
+
121
+ import seaborn as sns
122
+
123
+ plt.figure(figsize=(12, 9))
124
+
125
+ equation_df=pd.concat([manhattan_dummy,
126
+
127
+ private_dummy, home_dummy, shared_dummy, price, minimum_nights,
128
+
129
+ number_of_reviews, reviews_per_month,
130
+
131
+ calculated_host_listings_count, availability_365], axis=1)
132
+
133
+ sns.heatmap(equation_df.pct_change().corr(), annot=True, cmap='Blues')
134
+
135
+
136
+
137
+ import numpy as np
138
+
139
+ import statsmodels.api as sm
140
+
141
+ from sklearn import linear_model, datasets
142
+
143
+ from sklearn.linear_model import LinearRegression
144
+
145
+ price = pd.DataFrame(equation_df.price)
146
+
147
+
148
+
149
+ x_list = equation_df.drop("price",1)
150
+
151
+ x_list = equation_df.drop("reviews_per_month",1)
152
+
153
+ x_list = x_list.drop("shared_dummy",1)
154
+
155
+ x_list = x_list.drop("home_dummy",1)
156
+
157
+ x_list = x_list.drop(x_list.columns[np.isnan(x_list).any()], axis=1)
158
+
159
+
160
+
161
+ model = sm.OLS(price, sm.add_constant(x_list))
162
+
163
+ result =model.fit()
164
+
165
+ print(result.summary())
166
+
167
+ print(result.pvalues)
168
+
169
+
170
+
171
+ from statsmodels.stats.outliers_influence import variance_inflation_factor
172
+
173
+ num_cols = model.exog.shape[1]
174
+
175
+ print(num_cols) #説明変数の列数
176
+
177
+ vifs = [variance_inflation_factor(model.exog, i) for i in range(0, num_cols)]
178
+
179
+ pdv = pd.DataFrame(vifs, index=model.exog_names, columns=["VIF"])
180
+
181
+ print(pdv)
182
+
183
+
184
+
185
+ p_plot = sns.pairplot(x_list, hue="Manhattan_dummry")
186
+
187
+ p_plot.savefig("pair.png")
188
+
189
+
190
+
191
+ ```
192
+
193
+ ![pairplot](9cceaf7da8e010c417fafffd06c1b0c3.png)

3

追記

2021/02/15 13:35

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -17,3 +17,11 @@
17
17
  `print('p-values\n', results.pvalues)` resultsになってますがresultですね。
18
18
 
19
19
  `print('p-values\n', result.pvalues)`
20
+
21
+
22
+
23
+ 0. concat
24
+
25
+ `x_list3=pd.concat([price, x_list], axis=1)` ここで、x_list中にはpriceはすでに存在していますが、更にconcatしようとしているのはなぜでしょうか。
26
+
27
+ これを省くと、`sns.pairplot(x_list, hue="Manhattan_dummry")`は動作するようです。

2

修正

2021/02/15 05:10

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -1,6 +1,6 @@
1
1
  import文がいろんなところにあるのは、jupyter notebookとかで試したセルを、そのままつなげたんでしょうか。エラー文もそれぞれセルのものですね? 本当は、セルごとに分けて一つずつ質問したほうが、回答がつきやすいと思います。
2
2
 
3
- あと、ネット上にあるデータならリンクを貼っておいてください。
3
+ ~~あと、ネット上にあるデータならリンクを貼っておいてください。~~ 失礼、改めてみたら貼ってありましたね。
4
4
 
5
5
 
6
6
 

1

修正

2021/02/15 05:02

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -17,11 +17,3 @@
17
17
  `print('p-values\n', results.pvalues)` resultsになってますがresultですね。
18
18
 
19
19
  `print('p-values\n', result.pvalues)`
20
-
21
-
22
-
23
- 0. concatの向き
24
-
25
- `x_list3=pd.concat([price, x_list], axis=1)`のところ、[48366 rows x 1 columns]と[48366 rows x 7 columns]いう、横幅の違う2つを縦につなげようとしてエラーが出ています。横につなげるんではないんでしょうか。
26
-
27
- `x_list3=pd.concat([price, x_list], axis=0)`