In [1]:
import pandas as pd
import numpy as np
from matplotlib.pyplot import subplots
house = pd.read_csv('house.csv')
house.head(3)
Out[1]:
| Order | PID | MS SubClass | MS Zoning | Lot Frontage | Lot Area | Street | Alley | Lot Shape | Land Contour | ... | Pool Area | Pool QC | Fence | Misc Feature | Misc Val | Mo Sold | Yr Sold | Sale Type | Sale Condition | SalePrice | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 526301100 | 20 | RL | 141.0 | 31770 | Pave | NaN | IR1 | Lvl | ... | 0 | NaN | NaN | NaN | 0 | 5 | 2010 | WD | Normal | 215000 |
| 1 | 2 | 526350040 | 20 | RH | 80.0 | 11622 | Pave | NaN | Reg | Lvl | ... | 0 | NaN | MnPrv | NaN | 0 | 6 | 2010 | WD | Normal | 105000 |
| 2 | 3 | 526351010 | 20 | RL | 81.0 | 14267 | Pave | NaN | IR1 | Lvl | ... | 0 | NaN | NaN | Gar2 | 12500 | 6 | 2010 | WD | Normal | 172000 |
3 rows × 82 columns
In [2]:
house = house[house['Bldg Type'] == '1Fam']
house = house[house['Sale Condition'] == 'Normal']
sales = house[['SalePrice', '1st Flr SF', '2nd Flr SF', 'Total Bsmt SF',
'Garage Area', 'Wood Deck SF', 'Open Porch SF', 'Lot Area',
'Year Built', 'Yr Sold']]
sales.head(3)
Out[2]:
| SalePrice | 1st Flr SF | 2nd Flr SF | Total Bsmt SF | Garage Area | Wood Deck SF | Open Porch SF | Lot Area | Year Built | Yr Sold | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 215000 | 1656 | 0 | 1080.0 | 528.0 | 210 | 62 | 31770 | 1960 | 2010 |
| 1 | 105000 | 896 | 0 | 882.0 | 730.0 | 140 | 0 | 11622 | 1961 | 2010 |
| 2 | 172000 | 1329 | 0 | 1329.0 | 312.0 | 393 | 36 | 14267 | 1958 | 2010 |
In [4]:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
In [5]:
X = sales[['1st Flr SF', '2nd Flr SF', 'Open Porch SF', 'Year Built']]
y = sales['SalePrice']
from sklearn.model_selection import train_test_split
# Assume X and y are your features and target variable
# Split data into training and testing sets (example)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create and fit the model
model = LinearRegression()
model.fit(X_train, y_train)
# Get the R² score
score = model.score(X_test, y_test)
print(f"Model R² Score: {score}")
Model R² Score: 0.7832171768278281
In [6]:
# Let us save all the predictor variables in X and the response in y
X = sales[['1st Flr SF', '2nd Flr SF']]
y = sales['SalePrice']
# Split data into training and testing sets (example)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Fitting a polynomial of degree d
score = []
for d in range(1, 6):
model = Pipeline([
("poly", PolynomialFeatures(degree=d, include_bias=False)),
("scaler", StandardScaler()),
("linreg", LinearRegression())
])
model.fit(X_train, y_train)
y_test_pred = model.predict(X_test)
score.append(mean_squared_error(y_test_pred, y_test))
print('R^2 score on test set with degree', d, ' : ', r2_score(y_test_pred, y_test))
fig, ax = subplots(figsize = (4, 4))
ax.plot(range(1, 6), score)
R^2 score on test set with degree 1 : 0.5636673108738282 R^2 score on test set with degree 2 : 0.5686254523419286 R^2 score on test set with degree 3 : 0.5603163173001231 R^2 score on test set with degree 4 : 0.6129129404922433 R^2 score on test set with degree 5 : 0.20652947636342744
Out[6]:
[<matplotlib.lines.Line2D at 0x11ffb72b0>]
In [7]:
ckd = pd.read_csv('ckd.csv')
ckd = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count', 'Class']]
ckd
Out[7]:
| Age | Blood Pressure | Blood Glucose Random | White Blood Cell Count | Red Blood Cell Count | Class | |
|---|---|---|---|---|---|---|
| 0 | 48 | 70 | 117 | 6700 | 3.9 | 1 |
| 1 | 53 | 90 | 70 | 12100 | 3.7 | 1 |
| 2 | 63 | 70 | 380 | 4500 | 3.8 | 1 |
| 3 | 68 | 80 | 157 | 11000 | 2.6 | 1 |
| 4 | 61 | 80 | 173 | 9200 | 3.2 | 1 |
| ... | ... | ... | ... | ... | ... | ... |
| 153 | 55 | 80 | 140 | 6700 | 4.9 | 0 |
| 154 | 42 | 70 | 75 | 7800 | 6.2 | 0 |
| 155 | 12 | 80 | 100 | 6600 | 5.4 | 0 |
| 156 | 17 | 60 | 114 | 7200 | 5.9 | 0 |
| 157 | 58 | 80 | 131 | 6800 | 6.1 | 0 |
158 rows × 6 columns
In [9]:
X = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count']]
y = ckd['Class']
# Create a train:test split of the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.30, random_state=42, stratify=y
)
# We shall use the StandardScaler to scale the training dataset
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
# The transform method is then used to scale the test data using parameters
# obtained from the training data.
X_test_scaled = scaler.transform(X_test)
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(max_iter=1000)
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
# Evaluate
print('Evaluation of Logistic Regression classification')
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
Evaluation of Logistic Regression classification
Accuracy: 0.9583333333333334
Confusion Matrix:
[[34 1]
[ 1 12]]
Classification Report:
precision recall f1-score support
0 0.97 0.97 0.97 35
1 0.92 0.92 0.92 13
accuracy 0.96 48
macro avg 0.95 0.95 0.95 48
weighted avg 0.96 0.96 0.96 48
In [14]:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=13)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
# Evaluate
print('Evaluation of KNN classification')
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
Evaluation of KNN classification
Accuracy: 0.8541666666666666
Confusion Matrix:
[[35 0]
[ 7 6]]
Classification Report:
precision recall f1-score support
0 0.83 1.00 0.91 35
1 1.00 0.46 0.63 13
accuracy 0.85 48
macro avg 0.92 0.73 0.77 48
weighted avg 0.88 0.85 0.83 48
In [ ]: