import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import Series,DataFrame from pylab import mpl mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認字體 mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負號'-'顯示爲方塊的問題 # 同一城市的數據進行級聯 pd.concat([df1, df2, df3], ignore_index=True) plt.scatter(li1, li2) plt.xlabel = 'distance' plt.ylabel = 'temperature' plt.title = 'dis-temp'
# 創建一個溫度模型,讓其能夠根據距離,預測出該距離對應城市的最高溫度 # 導入sklearn,創建線性迴歸模型 from sklearn.linear_model import LinearRegression # 實例化算法模型對象, y = kx + b linear = LinearRegression() # 樣本數據提取,通常封裝到 np的 array 中 feature = np.array(distance) target = np.array(max_temp) # 訓練模型,特徵數據必須是二維的 linear.fit(feature.reshape(-1,1), target) # 基於訓練好的模型對象實現預測功能(獲取方程解) linear.predict([[266],[333]]) x = np.linspace(0, 400, num=100) y = linear.predict(x.reshape[-1,-1]) plt.scatter(distance, max_temp) plt.scatter(x, y)
# scikit-learn庫庫中使用k-近鄰算法 df = pd.read_csv('./film.txt') feature = df[['Action lens'],['Love lens']] target = df['target'] # 導入模型 from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors=3) knn.fit(feature, target) # knn模型訓練打分結果 knn.score(feature, target) knn.predict([[30], [50]])
# 提供機器學習的測試數據 from sk-learn.datasets as datasets datasets.load_iris()
# 訓練數據 x_train = feature[0:32500] y_train = target[0:32500] # 測試數據,測試模型的精準度 x_test = feature[32500:] y_test = target[32500:] # 實例化模型 knn = KNeighborsClassifier(n_neighbors=30) knn.fit(x_train, y_train) knn.score(x_train, y_train) # 測試模型精準度 print(knn.predict(x_test), y_test)
圖片的像素壓縮python
ndimages(df, zoom=(目標像素/當前圖片像素,目標像素/當前圖片像素))算法
df.reshape((1, -1)):轉換爲 1 * n 的數組數組
import matplotlib.pyplot as plt import numpy as np import pandas as pd from sklearing.neighbors import KNeighborsClassifier # 1.提取樣本數據 feature = [] target = [] for i in range(10): for j in range(1, 501): img_path = f'./data/{i}/{i}_{j}.bmp' img_arr = plt.imread(img_path) feature.append(img_arr) target.append(i) # 2.將list類型樣本轉爲 array 形式 feature = np.array(feature) target = np.array(target) # 3.特徵數據是三維的,不可做爲訓練數組,必須轉換成二維數組 feature = feature.reshape((5000, 784)) # 只能固定一個隨機函數的隨機因子 np.random.seed(10) np.random.shuffle(feature) np.random.seed(10) np.random.shuffle(feature) # 訓練數據和測試數據 x_train = feature[:4980] y_train = target[:4980] x_test = feature[4980:] y_test = target[4980:] # 4.生成訓練模型 knn = KNeighborsClassfier(n_negihbors=17) knn.fit(x_train, y_train) knn.score(x_train, y_train) print(knn.predict(x_test)) print(y_test) # 5.將訓練好的模型進行保存 from sklearn.externals import joblib joblib.dump(knn, './knn.m') # 加載保存的模型 knn = joblib.load('./knn.m') # 6.讓模型對外部一張圖片進行識別(應用) ex_img = plt.imread('./test.jpg') # 將多個數字進行裁剪 img_two = ex_img[:70,130:185,:] # 外部圖片數據是三維,實現降維 img_two = img_two.mean(axis=2) # 像素調整,等比例壓縮 import scipy.ndimage as ndimages img_two = ndimage.zoom(img_two, zoom=(28/75, 28/55)) # 將圖片轉換爲一維數組 img_two = img_two.reshape((1, -1)) knn.predict(img_two)