np.newaxis
import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, linear_model from sklearn.metrics import mean_squared_error, r2_score # Load the diabetes dataset diabetes = datasets.load_diabetes() # Use only one feature diabetes_X = diabetes.data[:, np.newaxis, 2] #原始数据集维度 data = diabetes.data print(data.shape) #np.newaxis放在第一个参数位置 data_1 = data[np.newaxis,:] print(data_1.shape) #np.newaxis放在第二个参数位置 data_2 = data[:,np.newaxis,:]#等价于data_2 = data[:,np.newaxis]和data_2 = data[:,np.newaxis,...] print(data_2.shape) #np.newaxis放在第三个参数位置 data_3 = data[:,:,np.newaxis] print(data_3.shape) #此处相当于把data数据集转换成442*1*10后,在第三个维度中的10个系列中选取第3个系列(index为2)。 diabetes_X =data[:, np.newaxis, 2] print(diabetes_X.shape) print(data,diabetes_X,sep='\n') print(data[:,2])#此处data[:,2]和data[:, np.newaxis, 2]的内容一样,只是显示的排列方式不一样。都是原data的第三列data[:2]的内容。
(442, 10) (1, 442, 10) (442, 1, 10) (442, 10, 1) (442, 1) [[ 0.03807591 0.05068012 0.06169621 ... -0.00259226 0.01990842 -0.01764613] [-0.00188202 -0.04464164 -0.05147406 ... -0.03949338 -0.06832974 -0.09220405] [ 0.08529891 0.05068012 0.04445121 ... -0.00259226 0.00286377 -0.02593034] ... [ 0.04170844 0.05068012 -0.01590626 ... -0.01107952 -0.04687948 0.01549073] [-0.04547248 -0.04464164 0.03906215 ... 0.02655962 0.04452837 -0.02593034] [-0.04547248 -0.04464164 -0.0730303 ... -0.03949338 -0.00421986 0.00306441]] [[ 0.06169621] [-0.05147406] [ 0.04445121] [-0.01159501] [-0.03638469] [-0.04069594] [-0.04716281] [-0.00189471] [ 0.06169621] [ 0.03906215] [-0.08380842] [ 0.01750591] [-0.02884001] [-0.00189471] [-0.02560657] [-0.01806189] [ 0.04229559] [ 0.01211685] [-0.0105172 ] [-0.01806189] [-0.05686312] [-0.02237314] [-0.00405033] [ 0.06061839] [ 0.03582872] [-0.01267283] [-0.07734155] [ 0.05954058] [-0.02129532] [-0.00620595] [ 0.04445121] [-0.06548562] [ 0.12528712] [-0.05039625] [-0.06332999] [-0.03099563] [ 0.02289497] [ 0.01103904] [ 0.07139652] [ 0.01427248] [-0.00836158] [-0.06764124] [-0.0105172 ] [-0.02345095] [ 0.06816308] [-0.03530688] [-0.01159501] [-0.0730303 ] [-0.04177375] [ 0.01427248] [-0.00728377] [ 0.0164281 ] [-0.00943939] [-0.01590626] [ 0.0250506 ] [-0.04931844] [ 0.04121778] [-0.06332999] [-0.06440781] [-0.02560657] [-0.00405033] [ 0.00457217] [-0.00728377] [-0.0374625 ] [-0.02560657] [-0.02452876] [-0.01806189] [-0.01482845] [-0.02991782] [-0.046085 ] [-0.06979687] [ 0.03367309] [-0.00405033] [-0.02021751] [ 0.00241654] [-0.03099563] [ 0.02828403] [-0.03638469] [-0.05794093] [-0.0374625 ] [ 0.01211685] [-0.02237314] [-0.03530688] [ 0.00996123] [-0.03961813] [ 0.07139652] [-0.07518593] [-0.00620595] [-0.04069594] [-0.04824063] [-0.02560657] [ 0.0519959 ] [ 0.00457217] [-0.06440781] [-0.01698407] [-0.05794093] [ 0.00996123] [ 0.08864151] [-0.00512814] [-0.06440781] [ 0.01750591] [-0.04500719] [ 0.02828403] [ 0.04121778] [ 0.06492964] [-0.03207344] [-0.07626374] [ 0.04984027] [ 0.04552903] [-0.00943939] [-0.03207344] [ 0.00457217] [ 0.02073935] [ 0.01427248] [ 0.11019775] [ 0.00133873] [ 0.05846277] [-0.02129532] [-0.0105172 ] [-0.04716281] [ 0.00457217] [ 0.01750591] [ 0.08109682] [ 0.0347509 ] [ 0.02397278] [-0.00836158] [-0.06117437] [-0.00189471] [-0.06225218] [ 0.0164281 ] [ 0.09618619] [-0.06979687] [-0.02129532] [-0.05362969] [ 0.0433734 ] [ 0.05630715] [-0.0816528 ] [ 0.04984027] [ 0.11127556] [ 0.06169621] [ 0.01427248] [ 0.04768465] [ 0.01211685] [ 0.00564998] [ 0.04660684] [ 0.12852056] [ 0.05954058] [ 0.09295276] [ 0.01535029] [-0.00512814] [ 0.0703187 ] [-0.00405033] [-0.00081689] [-0.04392938] [ 0.02073935] [ 0.06061839] [-0.0105172 ] [-0.03315126] [-0.06548562] [ 0.0433734 ] [-0.06225218] [ 0.06385183] [ 0.03043966] [ 0.07247433] [-0.0191397 ] [-0.06656343] [-0.06009656] [ 0.06924089] [ 0.05954058] [-0.02668438] [-0.02021751] [-0.046085 ] [ 0.07139652] [-0.07949718] [ 0.00996123] [-0.03854032] [ 0.01966154] [ 0.02720622] [-0.00836158] [-0.01590626] [ 0.00457217] [-0.04285156] [ 0.00564998] [-0.03530688] [ 0.02397278] [-0.01806189] [ 0.04229559] [-0.0547075 ] [-0.00297252] [-0.06656343] [-0.01267283] [-0.04177375] [-0.03099563] [-0.00512814] [-0.05901875] [ 0.0250506 ] [-0.046085 ] [ 0.00349435] [ 0.05415152] [-0.04500719] [-0.05794093] [-0.05578531] [ 0.00133873] [ 0.03043966] [ 0.00672779] [ 0.04660684] [ 0.02612841] [ 0.04552903] [ 0.04013997] [-0.01806189] [ 0.01427248] [ 0.03690653] [ 0.00349435] [-0.07087468] [-0.03315126] [ 0.09403057] [ 0.03582872] [ 0.03151747] [-0.06548562] [-0.04177375] [-0.03961813] [-0.03854032] [-0.02560657] [-0.02345095] [-0.06656343] [ 0.03259528] [-0.046085 ] [-0.02991782] [-0.01267283] [-0.01590626] [ 0.07139652] [-0.03099563] [ 0.00026092] [ 0.03690653] [ 0.03906215] [-0.01482845] [ 0.00672779] [-0.06871905] [-0.00943939] [ 0.01966154] [ 0.07462995] [-0.00836158] [-0.02345095] [-0.046085 ] [ 0.05415152] [-0.03530688] [-0.03207344] [-0.0816528 ] [ 0.04768465] [ 0.06061839] [ 0.05630715] [ 0.09834182] [ 0.05954058] [ 0.03367309] [ 0.05630715] [-0.06548562] [ 0.16085492] [-0.05578531] [-0.02452876] [-0.03638469] [-0.00836158] [-0.04177375] [ 0.12744274] [-0.07734155] [ 0.02828403] [-0.02560657] [-0.06225218] [-0.00081689] [ 0.08864151] [-0.03207344] [ 0.03043966] [ 0.00888341] [ 0.00672779] [-0.02021751] [-0.02452876] [-0.01159501] [ 0.02612841] [-0.05901875] [-0.03638469] [-0.02452876] [ 0.01858372] [-0.0902753 ] [-0.00512814] [-0.05255187] [-0.02237314] [-0.02021751] [-0.0547075 ] [-0.00620595] [-0.01698407] [ 0.05522933] [ 0.07678558] [ 0.01858372] [-0.02237314] [ 0.09295276] [-0.03099563] [ 0.03906215] [-0.06117437] [-0.00836158] [-0.0374625 ] [-0.01375064] [ 0.07355214] [-0.02452876] [ 0.03367309] [ 0.0347509 ] [-0.03854032] [-0.03961813] [-0.00189471] [-0.03099563] [-0.046085 ] [ 0.00133873] [ 0.06492964] [ 0.04013997] [-0.02345095] [ 0.05307371] [ 0.04013997] [-0.02021751] [ 0.01427248] [-0.03422907] [ 0.00672779] [ 0.00457217] [ 0.03043966] [ 0.0519959 ] [ 0.06169621] [-0.00728377] [ 0.00564998] [ 0.05415152] [-0.00836158] [ 0.114509 ] [ 0.06708527] [-0.05578531] [ 0.03043966] [-0.02560657] [ 0.10480869] [-0.00620595] [-0.04716281] [-0.04824063] [ 0.08540807] [-0.01267283] [-0.03315126] [-0.00728377] [-0.01375064] [ 0.05954058] [ 0.02181716] [ 0.01858372] [-0.01159501] [-0.00297252] [ 0.01750591] [-0.02991782] [-0.02021751] [-0.05794093] [ 0.06061839] [-0.04069594] [-0.07195249] [-0.05578531] [ 0.04552903] [-0.00943939] [-0.03315126] [ 0.04984027] [-0.08488624] [ 0.00564998] [ 0.02073935] [-0.00728377] [ 0.10480869] [-0.02452876] [-0.00620595] [-0.03854032] [ 0.13714305] [ 0.17055523] [ 0.00241654] [ 0.03798434] [-0.05794093] [-0.00943939] [-0.02345095] [-0.0105172 ] [-0.03422907] [-0.00297252] [ 0.06816308] [ 0.00996123] [ 0.00241654] [-0.03854032] [ 0.02612841] [-0.08919748] [ 0.06061839] [-0.02884001] [-0.02991782] [-0.0191397 ] [-0.04069594] [ 0.01535029] [-0.02452876] [ 0.00133873] [ 0.06924089] [-0.06979687] [-0.02991782] [-0.046085 ] [ 0.01858372] [ 0.00133873] [-0.03099563] [-0.00405033] [ 0.01535029] [ 0.02289497] [ 0.04552903] [-0.04500719] [-0.03315126] [ 0.097264 ] [ 0.05415152] [ 0.12313149] [-0.08057499] [ 0.09295276] [-0.05039625] [-0.01159501] [-0.0277622 ] [ 0.05846277] [ 0.08540807] [-0.00081689] [ 0.00672779] [ 0.00888341] [ 0.08001901] [ 0.07139652] [-0.02452876] [-0.0547075 ] [-0.03638469] [ 0.0164281 ] [ 0.07786339] [-0.03961813] [ 0.01103904] [-0.04069594] [-0.03422907] [ 0.00564998] [ 0.08864151] [-0.03315126] [-0.05686312] [-0.03099563] [ 0.05522933] [-0.06009656] [ 0.00133873] [-0.02345095] [-0.07410811] [ 0.01966154] [-0.01590626] [-0.01590626] [ 0.03906215] [-0.0730303 ]] [ 0.06169621 -0.05147406 0.04445121 -0.01159501 -0.03638469 -0.04069594 -0.04716281 -0.00189471 0.06169621 0.03906215 -0.08380842 0.01750591 -0.02884001 -0.00189471 -0.02560657 -0.01806189 0.04229559 0.01211685 -0.0105172 -0.01806189 -0.05686312 -0.02237314 -0.00405033 0.06061839 0.03582872 -0.01267283 -0.07734155 0.05954058 -0.02129532 -0.00620595 0.04445121 -0.06548562 0.12528712 -0.05039625 -0.06332999 -0.03099563 0.02289497 0.01103904 0.07139652 0.01427248 -0.00836158 -0.06764124 -0.0105172 -0.02345095 0.06816308 -0.03530688 -0.01159501 -0.0730303 -0.04177375 0.01427248 -0.00728377 0.0164281 -0.00943939 -0.01590626 0.0250506 -0.04931844 0.04121778 -0.06332999 -0.06440781 -0.02560657 -0.00405033 0.00457217 -0.00728377 -0.0374625 -0.02560657 -0.02452876 -0.01806189 -0.01482845 -0.02991782 -0.046085 -0.06979687 0.03367309 -0.00405033 -0.02021751 0.00241654 -0.03099563 0.02828403 -0.03638469 -0.05794093 -0.0374625 0.01211685 -0.02237314 -0.03530688 0.00996123 -0.03961813 0.07139652 -0.07518593 -0.00620595 -0.04069594 -0.04824063 -0.02560657 0.0519959 0.00457217 -0.06440781 -0.01698407 -0.05794093 0.00996123 0.08864151 -0.00512814 -0.06440781 0.01750591 -0.04500719 0.02828403 0.04121778 0.06492964 -0.03207344 -0.07626374 0.04984027 0.04552903 -0.00943939 -0.03207344 0.00457217 0.02073935 0.01427248 0.11019775 0.00133873 0.05846277 -0.02129532 -0.0105172 -0.04716281 0.00457217 0.01750591 0.08109682 0.0347509 0.02397278 -0.00836158 -0.06117437 -0.00189471 -0.06225218 0.0164281 0.09618619 -0.06979687 -0.02129532 -0.05362969 0.0433734 0.05630715 -0.0816528 0.04984027 0.11127556 0.06169621 0.01427248 0.04768465 0.01211685 0.00564998 0.04660684 0.12852056 0.05954058 0.09295276 0.01535029 -0.00512814 0.0703187 -0.00405033 -0.00081689 -0.04392938 0.02073935 0.06061839 -0.0105172 -0.03315126 -0.06548562 0.0433734 -0.06225218 0.06385183 0.03043966 0.07247433 -0.0191397 -0.06656343 -0.06009656 0.06924089 0.05954058 -0.02668438 -0.02021751 -0.046085 0.07139652 -0.07949718 0.00996123 -0.03854032 0.01966154 0.02720622 -0.00836158 -0.01590626 0.00457217 -0.04285156 0.00564998 -0.03530688 0.02397278 -0.01806189 0.04229559 -0.0547075 -0.00297252 -0.06656343 -0.01267283 -0.04177375 -0.03099563 -0.00512814 -0.05901875 0.0250506 -0.046085 0.00349435 0.05415152 -0.04500719 -0.05794093 -0.05578531 0.00133873 0.03043966 0.00672779 0.04660684 0.02612841 0.04552903 0.04013997 -0.01806189 0.01427248 0.03690653 0.00349435 -0.07087468 -0.03315126 0.09403057 0.03582872 0.03151747 -0.06548562 -0.04177375 -0.03961813 -0.03854032 -0.02560657 -0.02345095 -0.06656343 0.03259528 -0.046085 -0.02991782 -0.01267283 -0.01590626 0.07139652 -0.03099563 0.00026092 0.03690653 0.03906215 -0.01482845 0.00672779 -0.06871905 -0.00943939 0.01966154 0.07462995 -0.00836158 -0.02345095 -0.046085 0.05415152 -0.03530688 -0.03207344 -0.0816528 0.04768465 0.06061839 0.05630715 0.09834182 0.05954058 0.03367309 0.05630715 -0.06548562 0.16085492 -0.05578531 -0.02452876 -0.03638469 -0.00836158 -0.04177375 0.12744274 -0.07734155 0.02828403 -0.02560657 -0.06225218 -0.00081689 0.08864151 -0.03207344 0.03043966 0.00888341 0.00672779 -0.02021751 -0.02452876 -0.01159501 0.02612841 -0.05901875 -0.03638469 -0.02452876 0.01858372 -0.0902753 -0.00512814 -0.05255187 -0.02237314 -0.02021751 -0.0547075 -0.00620595 -0.01698407 0.05522933 0.07678558 0.01858372 -0.02237314 0.09295276 -0.03099563 0.03906215 -0.06117437 -0.00836158 -0.0374625 -0.01375064 0.07355214 -0.02452876 0.03367309 0.0347509 -0.03854032 -0.03961813 -0.00189471 -0.03099563 -0.046085 0.00133873 0.06492964 0.04013997 -0.02345095 0.05307371 0.04013997 -0.02021751 0.01427248 -0.03422907 0.00672779 0.00457217 0.03043966 0.0519959 0.06169621 -0.00728377 0.00564998 0.05415152 -0.00836158 0.114509 0.06708527 -0.05578531 0.03043966 -0.02560657 0.10480869 -0.00620595 -0.04716281 -0.04824063 0.08540807 -0.01267283 -0.03315126 -0.00728377 -0.01375064 0.05954058 0.02181716 0.01858372 -0.01159501 -0.00297252 0.01750591 -0.02991782 -0.02021751 -0.05794093 0.06061839 -0.04069594 -0.07195249 -0.05578531 0.04552903 -0.00943939 -0.03315126 0.04984027 -0.08488624 0.00564998 0.02073935 -0.00728377 0.10480869 -0.02452876 -0.00620595 -0.03854032 0.13714305 0.17055523 0.00241654 0.03798434 -0.05794093 -0.00943939 -0.02345095 -0.0105172 -0.03422907 -0.00297252 0.06816308 0.00996123 0.00241654 -0.03854032 0.02612841 -0.08919748 0.06061839 -0.02884001 -0.02991782 -0.0191397 -0.04069594 0.01535029 -0.02452876 0.00133873 0.06924089 -0.06979687 -0.02991782 -0.046085 0.01858372 0.00133873 -0.03099563 -0.00405033 0.01535029 0.02289497 0.04552903 -0.04500719 -0.03315126 0.097264 0.05415152 0.12313149 -0.08057499 0.09295276 -0.05039625 -0.01159501 -0.0277622 0.05846277 0.08540807 -0.00081689 0.00672779 0.00888341 0.08001901 0.07139652 -0.02452876 -0.0547075 -0.03638469 0.0164281 0.07786339 -0.03961813 0.01103904 -0.04069594 -0.03422907 0.00564998 0.08864151 -0.03315126 -0.05686312 -0.03099563 0.05522933 -0.06009656 0.00133873 -0.02345095 -0.07410811 0.01966154 -0.01590626 -0.01590626 0.03906215 -0.0730303 ]
#再举一个例子,机器学习中大名鼎鼎的iris数据集 iris = datasets.load_iris() iris_data = iris.data iris_target = iris.target print(iris_data.shape) print(iris_target.shape) # 如果想把特征数据和标签数据放在同一个array中,运行iris_ = np.hstack((iris_data, iris_target)),结果报错; # iris_ = np.hstack((iris_data, iris_target))#报错 # 报错信息:ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has # 2 dimension(s) and the array at index 1 has 1 dimension(s); #说我们输入的arrays的维度数不一样,这时我们的np.newaxis就可以派上用场了 print(iris_target[:, np.newaxis].shape) iris = np.hstack((iris_data, iris_target[:,np.newaxis])) print(iris.shape)# 成功了
#输出: (150, 4) (150,) (150, 1) (150, 5)