<jupyter_start><jupyter_text>Linear Regression (housing.csv)<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import preprocessing, linear_model
from sklearn.metrics import mean_squared_error, r2_score
f = pd.read_csv('housing.csv', header=None, delim_whitespace=True)
print(f.head())
f_numpy = np.array(f)
housing_x = f_numpy[:,:13]
housing_y = f_numpy[:,13:]

# train-test split
housing_x_train, housing_x_test, housing_y_train, housing_y_test = train_test_split(housing_x, housing_y, test_size=0.1, random_state=1)
# z-scope normalization
scaler = preprocessing.StandardScaler().fit(housing_x_train)
housing_x_train = scaler.transform(housing_x_train)

# build linear regression model
model = linear_model.LinearRegression()
# train the model
model.fit(housing_x_train, housing_y_train)

# make prediction
housing_x_test = scaler.transform(housing_x_test)
housing_y_pred = model.predict(housing_x_test)

# mean squared error
print(f'Mean squared error: {mean_squared_error(housing_y_test, housing_y_pred)}')
print(f'Variance score: {r2_score(housing_y_test, housing_y_pred)}')<jupyter_output>Mean squared error: 20.544274659325744
Variance score: 0.778638658029892<jupyter_text>Polynomial Regression (wine-quality)<jupyter_code>import numpy as np
import pandas as pd
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn import preprocessing
f = pd.read_csv('winequality-red.csv')
print(f.head())
x = f.drop(['quality'], axis=1)
y = f['quality']

poly = PolynomialFeatures(degree=2).fit(x)
x_poly = poly.transform(x)

# train-test split
x_train, x_test, y_train, y_test = train_test_split(x_poly, y, test_size=0.2)

# training data normalization
scaler = preprocessing.StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)

# build model
model = linear_model.LinearRegression()
model.fit(x_train, y_train)

# Testing data normalization
x_test = scaler.transform(x_test)

# predict y
y_pred = model.predict(x_test)

# evaluation
print(f'Mean squared error: {mean_squared_error(y_test, y_pred)}')
print(f'Variance score: {r2_score(y_test, y_pred)}')<jupyter_output>Mean squared error: 0.45767840419918243
Variance score: 0.3161702985336503<jupyter_text>Logistic Regression (diabetes)<jupyter_code>import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn import datasets, preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score, confusion_matrix
f = pd.read_csv('pima-indians-diabetes.csv')
print(f.head())
x = f[['pregnant','insulin','bmi','age']]
y = f['label']

# train-test-split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)

# training data normalization
scaler = preprocessing.StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)

# build model
model = LogisticRegression()
model.fit(x_train, y_train)

# testing data normalization
x_test = scaler.transform(x_test)

# predict y
y_pred = model.predict(x_test)

accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')<jupyter_output>Accuracy: 0.7142857142857143
{'fraction_non_alphanumeric': 0.10450944546008531, 'fraction_numerical': 0.031078610603290677, 'mean_word_length': 7.921195652173913, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '288814', 'license': 'MIT', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1167, 'n_words': 243, 'path': 'practice2_regression.ipynb', 'repo_name': 'wangyuhsin/machine_learning_practice', 'size': '3282'}