import numpy as np
from sklearn import preprocessing
feature=np.array([[-500.5],
[-100.1],
[0],
[100.1],
[900.9]])
minmax_scale=preprocessing.MinMaxScaler((0,1),copy=False)
scaled_feature=minmax_scale.fit_transform(feature)
print(scaled_feature)
[[0. ]
[0.28571429]
[0.35714286]
[0.42857143]
[1. ]]
x=np.array([[-1000.1],
[-200.2],
[500.5],
[600.6],
[9000.9]])
scaler=preprocessing.StandardScaler()
standardized=scaler.fit_transform(x)
standardized
array([[-0.76058269],
[-0.54177196],
[-0.35009716],
[-0.32271504],
[ 1.97516685]])
robust_scaler=preprocessing.RobustScaler()
robust_scaler.fit_transform(x)
array([[-1.87387612],
[-0.875 ],
[ 0. ],
[ 0.125 ],
[10.61488511]])
from sklearn.preprocessing import Normalizer
y=np.array([[0.5,0.5],
[1.1,3.4],
[1.5,20.2],
[1.53,34.4],
[10.9,3.3]])
normalizer=Normalizer(norm="l2")
normalizer.fit_transform(y)
array([[0.70710678, 0.70710678],
[0.30782029, 0.95144452],
[0.07405353, 0.99725427],
[0.04443282, 0.99901237],
[0.95709822, 0.28976368]])
z=np.array([[0.5,0.5],
[1.1,3.4],
[1.5,20.2],
[1.53,34.4],
[10.9,3.3]])
normalizer=Normalizer(norm="l1")
normalizer.fit_transform(z)
array([[0.5 , 0.5 ],
[0.24444444, 0.75555556],
[0.06912442, 0.93087558],
[0.0425828 , 0.9574172 ],
[0.76760563, 0.23239437]])
from sklearn.preprocessing import PolynomialFeatures
features=np.array([[2,3],
[2,3],
[2,3]])
polynomial=PolynomialFeatures(degree=2,include_bias=False)
polynomial.fit_transform(features)
array([[2., 3., 4., 6., 9.],
[2., 3., 4., 6., 9.],
[2., 3., 4., 6., 9.]])
polynomial=PolynomialFeatures(degree=2,include_bias=False,interaction_only=True)
polynomial.fit_transform(features)
array([[2., 3., 6.],
[2., 3., 6.],
[2., 3., 6.]])
polynomial=PolynomialFeatures(degree=3,include_bias=False,interaction_only=True)
polynomial.fit_transform(features)
array([[2., 3., 6.],
[2., 3., 6.],
[2., 3., 6.]])
polynomial=PolynomialFeatures(degree=3,include_bias=False)
polynomial.fit_transform(features)
array([[ 2., 3., 4., 6., 9., 8., 12., 18., 27.],
[ 2., 3., 4., 6., 9., 8., 12., 18., 27.],
[ 2., 3., 4., 6., 9., 8., 12., 18., 27.]])
import numpy as np
from sklearn.covariance import EllipticEnvelope
from sklearn.datasets import make_blobs
x,y=make_blobs(n_samples=10,
n_features=2,
centers=1,
random_state=1)
x[0,0]=10000
x[0,1]=10000
outlier=EllipticEnvelope(contamination=0.1)
outlier.fit(x)
outlier.predict(x)
array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
x
array([[ 1.00000000e+04, 1.00000000e+04],
[-2.76017908e+00, 5.55121358e+00],
[-1.61734616e+00, 4.98930508e+00],
[-5.25790464e-01, 3.30659860e+00],
[ 8.52518583e-02, 3.64528297e+00],
[-7.94152277e-01, 2.10495117e+00],
[-1.34052081e+00, 4.15711949e+00],
[-1.98197711e+00, 4.02243551e+00],
[-2.18773166e+00, 3.33352125e+00],
[-1.97451969e-01, 2.34634916e+00]])
y
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
ff=features[:,0]
def out(x):
q1,q3=np.percentile(x,[25,75])
iqr=q1*q3
lower=q1-1.5*iqr
upper=q3+1.5*iqr
return np.where((x>upper)|(x<lower))
out(ff)
(array([0, 1], dtype=int64),)
import pandas as pd
houses=pd.DataFrame()
houses["Price"]=[534433,392333,293222,4322032]
houses["Bathrooms"]=[2,3.5,2,116]
houses["Square_Feet"]=[1500,2500,1500,48000]
houses
| Price | Bathrooms | Square_Feet |
---|
0 | 534433 | 2.0 | 1500 |
---|
1 | 392333 | 3.5 | 2500 |
---|
2 | 293222 | 2.0 | 1500 |
---|
3 | 4322032 | 116.0 | 48000 |
---|
houses[houses["Bathrooms"]<20]
| Price | Bathrooms | Square_Feet |
---|
0 | 534433 | 2.0 | 1500 |
---|
1 | 392333 | 3.5 | 2500 |
---|
2 | 293222 | 2.0 | 1500 |
---|
import numpy as np
houses["Outlier"]=np.where(houses["Bathrooms"]<20,0,1)
houses
| Price | Bathrooms | Square_Feet | Outlier |
---|
0 | 534433 | 2.0 | 1500 | 0 |
---|
1 | 392333 | 3.5 | 2500 | 0 |
---|
2 | 293222 | 2.0 | 1500 | 0 |
---|
3 | 4322032 | 116.0 | 48000 | 1 |
---|
x=houses["Square_Feet"]
houses["log"]=np.log(x)
houses
| Price | Bathrooms | Square_Feet | Outlier | log |
---|
0 | 534433 | 2.0 | 1500 | 0 | 7.313220 |
---|
1 | 392333 | 3.5 | 2500 | 0 | 7.824046 |
---|
2 | 293222 | 2.0 | 1500 | 0 | 7.313220 |
---|
3 | 4322032 | 116.0 | 48000 | 1 | 10.778956 |
---|
from sklearn.preprocessing import Binarizer
age=np.array([[6],
[12],
[20],
[36],
[65]])
binarizer=Binarizer(18)
binarizer.fit_transform(age)
array([[0],
[0],
[1],
[1],
[1]])
np.digitize(age,bins=[20,30,64],right=False)
array([[0],
[0],
[1],
[2],
[3]], dtype=int64)
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
x,y=make_blobs(n_samples=50,
n_features=2,
centers=3,
random_state=1)
plt.scatter(x[:,0],x[:,1],marker='o',c=y)
<matplotlib.collections.PathCollection at 0x1c86553e820>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2b820757f04815d61aebd373380d967c.png#pic_center)
dataframe=pd.DataFrame(x,columns=["x_1","x_2"])
dataframe.head(5)
| x_1 | x_2 |
---|
0 | -9.877554 | -3.336145 |
---|
1 | -7.287210 | -8.353986 |
---|
2 | -6.943061 | -7.023744 |
---|
3 | -7.440167 | -8.791959 |
---|
4 | -6.641388 | -8.075888 |
---|
clusterer=KMeans(3,random_state=0)
clusterer.fit(x)
dataframe["group"]=clusterer.predict(x)
dataframe.head(5)
| x_1 | x_2 | group |
---|
0 | -9.877554 | -3.336145 | 2 |
---|
1 | -7.287210 | -8.353986 | 0 |
---|
2 | -6.943061 | -7.023744 | 0 |
---|
3 | -7.440167 | -8.791959 | 0 |
---|
4 | -6.641388 | -8.075888 | 0 |
---|
features=np.array([[1,1],
[2,2],
[3,3],
[np.nan,5]])
features[~np.isnan(features).any(axis=1)]
array([[1., 1.],
[2., 2.],
[3., 3.]])
dataframe=pd.DataFrame(features,columns=["features_1","features_2"])
dataframe.dropna()
| features_1 | features_2 |
---|
0 | 1.0 | 1.0 |
---|
1 | 2.0 | 2.0 |
---|
2 | 3.0 | 3.0 |
---|
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_blobs
x,y=make_blobs(n_samples=1000,
n_features=2,
random_state=1)
scaler=StandardScaler()
standardized=scaler.fit_transform(x)
true_value=standardized[0,0]
standardized[0,0]=np.nan
knn=KNeighborsClassifier()
knn.fit(x,y)
z=knn.predict(x)
z
array([0, 1, 0, 1, 2, 2, 2, 2, 0, 1, 2, 2, 0, 0, 0, 2, 1, 0, 2, 2, 1, 1,
2, 0, 1, 2, 1, 1, 0, 1, 2, 2, 1, 1, 0, 0, 0, 0, 0, 2, 1, 0, 1, 2,
2, 1, 2, 0, 2, 1, 0, 2, 1, 2, 0, 2, 2, 1, 1, 0, 1, 2, 2, 2, 1, 0,
0, 0, 0, 2, 2, 1, 2, 2, 1, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 0, 1,
0, 1, 1, 0, 2, 0, 2, 2, 0, 1, 2, 2, 2, 1, 0, 2, 1, 2, 1, 2, 2, 0,
1, 2, 1, 0, 1, 1, 0, 1, 2, 2, 2, 0, 1, 2, 2, 2, 0, 1, 0, 0, 0, 2,
0, 1, 1, 2, 2, 2, 1, 2, 0, 0, 2, 2, 1, 0, 2, 2, 2, 1, 2, 0, 1, 0,
1, 2, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 2, 2, 1, 1, 1, 1, 1, 1,
2, 1, 2, 1, 1, 0, 1, 1, 1, 0, 2, 1, 2, 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 2, 0, 1, 1, 1, 2, 0, 2,
1, 0, 2, 2, 2, 2, 1, 2, 0, 2, 2, 1, 0, 1, 0, 1, 1, 1, 0, 1, 2, 0,
2, 1, 2, 2, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 2, 0, 0, 2, 0, 1, 0, 0,
1, 1, 0, 2, 0, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 0, 2, 2, 0, 0, 1, 1,
0, 1, 2, 1, 1, 2, 0, 1, 2, 1, 1, 0, 1, 2, 1, 0, 0, 1, 1, 0, 1, 2,
2, 0, 2, 2, 0, 1, 2, 0, 2, 0, 1, 2, 0, 2, 0, 2, 0, 0, 2, 2, 0, 0,
1, 0, 1, 0, 0, 0, 2, 0, 0, 2])
from sklearn import neighbors
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_blobs
x,y=make_blobs(n_samples=1000,
n_features=2,
random_state=1)
from sklearn.impute import SimpleImputer
mean_imputer=SimpleImputer(strategy="mean")
features_mean=mean_imputer.fit_transform(x)
features_mean[0,0]
-3.058372724614996