<jupyter_start><jupyter_text>BOSTON COMPETITIONのサブミットファイルを作る  まず予測モデルを作る<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_csv("./train.csv",index_col=0)
df.head()
df.describe()
df.isnull().sum()
#データをよく見て、よい予測ができるようにがんばって
y=df["MEDV"].values
x=df.iloc[:,0:12].values
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

x_train, x_test,y_train,y_test=train_test_split(x,y,random_state=0)

model=LinearRegression()
model.fit(x_train,y_train) #学習用データで学習

print("train:",model.score(x_train,y_train)) #学習用データでのスコア
print("test:",model.score(x_test,y_test)) #検証用データでのスコア<jupyter_output>train: 0.7826253845901174
test: 0.5752835559227335<jupyter_text>ここからは、test.csvに対しての予測を作る<jupyter_code>test=pd.read_csv("./test.csv",index_col=0)#読み込む
xx=test.iloc[:,:12].values
yy=model.predict(xx) #上で作ったmodelでxxについての予測をyyとして作る
# できた<jupyter_output><empty_output><jupyter_text>ここからはサブミッション用のファイルづくり。ここから後はこのままでもいい<jupyter_code>sub = pd.DataFrame(yy,columns=["MEDV"],index=test.index)
#test.csvのIDとMEDVのcolumns名を持った新しいデータフレームをyyで作る
sub.head()#確認
sub.to_csv("submission_test.csv") #submission_test.csvとして、保存
#このファイルをサブミットできます<jupyter_output><empty_output><jupyter_text>やってみよう!!<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("./train.csv", index_col = 0)
df.head()
df.describe()
crim = df.iloc[:,0].values
medv = df["MEDV"].values
plt.scatter(crim, medv)
col = df.columns.values
col
data = df.iloc[:,:13].values
print(data)
plt.figure(figsize = (16,12))
for i in range (13):
    plt.subplot(4,4,i+1)
    plt.scatter(data[:,i],medv)
    plt.title(col[i])
#関係ありそう→CRIM, INDUS, NOX, RM, DIS, RAD, PTRATIO, B, LSTAT
#関係なさそう→ZN, CHAS, AGE, TAX
df.isnull().sum()
df["LSTAT"] = df["LSTAT"].fillna(df["LSTAT"].mean())
df.describe()
y = df["MEDV"].values
x = df.iloc[:,[0,4,5,7]].values
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state = 0)

model = LinearRegression()
model.fit(x_train, y_train)

print("train:",model.score(x_train,y_train))
print("test:",model.score(x_test, y_test))
#model_2: KNeighborsRegressor

from sklearn.neighbors import KNeighborsRegressor

model_2 = KNeighborsRegressor(2)
model_2.fit(x_train, y_train)

print("train 2:",model_2.score(x_train,y_train))
print("test 2:",model_2.score(x_test, y_test))
#model_3: Lasso

from sklearn.linear_model import Lasso

model_3 = Lasso()
model_3.fit(x_train, y_train)

print("train 3:",model_3.score(x_train,y_train))
print("test 3:",model_3.score(x_test, y_test))
#model_4: SVR

from sklearn.svm import SVR

X = x_train/np.max(x_train, axis = 0)
Y = y_train/np.max(y_train)
Xtest = x_test/np.max(x_train, axis = 0)
Ytest = y_test/np.max(y_train)

model_4 = SVR(kernel = 'rbf', C = 100, gamma = 0.1)
model_4.fit(X, Y)

print("train 4:",model_4.score(X, Y))
print("test 4:",model_4.score(Xtest, Ytest))
test = pd.read_csv("./test.csv", index_col = 0)
test.isnull().sum()
test["LSTAT"] = test["LSTAT"].fillna(np.mean(test["LSTAT"]))
test.isnull().sum()
xx = test.iloc[:,[0,2,4,5,7,8,10,11,12]].values
yy = model_2.predict(xx)
sub = pd.DataFrame(yy, columns = ["MEDV"], index = test.index)
sub.head()
sub.to_csv("submission_test.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1660243107026386, 'fraction_numerical': 0.0305366142899496, 'mean_word_length': 7.740932642487047, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '128395', 'license': 'MIT', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1556, 'n_words': 217, 'path': 'src/boston/BOSTON_for_submittion.ipynb', 'repo_name': 'rosayjc/python_university', 'size': '3373'}